VncSharp: Quick Start Guide
The following is a guide to getting started using the VncSharp RemoteDesktop control. Instructions are provided for Visual Studio.NET users as well as for those using Command-Line compilers. You are encouraged to view the API documentation and source code for more detailed information.
UPDATE: I have also added an example client app in C# to demonstrate how to use the VncSharp.dll file in your project. You can download it here.
1a) Setup for Use with Visual Studio.NET
- Create a new C# or VB.NET Windows Application project.
- Right-click inside the Toolbox and choose Add/Remove Items...--do this under the General tab so as not to clutter your Windows Forms tab.
- Click Browse... and locate the VncSharp.dll file. Or, if you've registered VncSharp in the GAC, you can scroll down in the existing list.
- In either case, you now need to locate the RemoteDesktop control and make sure it is checked.
- Click OK, and you should see the RemoteDesktop icon in the Toolbox.
- Drag-and-drop the RemoteDesktop control to your Form. After resizing the control, you will see a standard Windows desktop. This image is only displayed at design time and will be replaced with the remote desktop image once the application is run.
- Change the RemoteDesktop control's Dock Style to Fill in the Properties Window.
- Press F7 to view the Code for your Form.
- Include the VncSharp Namespace at the top of your source file by adding the code: using VncSharp;
- Skip to Step 2 to complete the code necessary for using the RemoteDesktop control.
1b) Setup for Use with Command-Line Compilers (csc, mcs, etc.)
- You must add a run-time reference to the VncSharp.dll file by including the following switch in your compiler options: /res:\path\to\VncSharp.dll
- Include the VncSharp Namespace at the top of your source file by adding the code: using VncSharp;
- In a Windows Forms derived class, add a member variable of type VncSharp.RemoteDesktop and instantiate this object in the form's constructor.
2) Coding the RemoteDesktop Control
You must add code to tell the RemoteDesktop to connect to a VNC host. In your form's Constructor (or where ever you want the connection to occur), add the following code:
// assuming your RemoteDesktop control is named rd rd.ConnectComplete += new ConnectCompleteHandler(ConnectComplete); rd.ConnectionLost += new EventHandler(ConnectionLost); rd.Connect("host_name_or_ip");
The first two lines add Event Handlers for the two events raised by the RemoteDesktop control. The ConnectComplete event occurs when a connection has been made. Similarly, the ConnectionLost event indicates that the host has dropped the connection, or that the connection has otherwise been lost. Both events are useful for adjusting your form's UI, for example:
protected void ConnectComplete(object sender, ConnectEventArgs e) { // Update Form to match geometry of remote desktop ClientSize = new Size(e.DesktopWidth, e.DesktopHeight); // Change the Form's title to match desktop name Text = e.DesktopName; } protected void ConnectionLost(object sender, EventArgs e) { // Let the user know of lost connection MessageBox.Show("Lost Connection to Host!"); }
You connect to a remote VNC server like this:
rd.Connect("host_name_or_ip"); // Optionally you can specify a display number too: // rd.Connect("unix_host_listening_on_display_2", 2);
All GUI and Network updates are handled automatically by the RemoteDesktop control. However, you will sometimes need to interact with the server manually. For example, if your client screen gets corrupted or needs a complete refresh (i.e., most updates from the server are only partial to save bandwidth), you can ask for a Full Screen Update like this:
// NOTE: available in VncSharp v. 0.85 rd.FullScreenUpdate();
You might also want to send various key combos, for example, CTRL+ALT+DEL:
// NOTE: available in VncSharp v. 0.85 rd.SendSpecialKeys(SpecialKeys.CtrlAltDel);
When your user closes the form, or clicks a Disconnect menu option, you can close the connection:
protected override void OnClosing(CancelEventArgs e) { rd.Disconnect(); base.OnClosing (e); }