public bool Connect(VncConfig a_config) { // Connect Vnc m_client?.Dispose(); m_client = new VncClient(a_config); m_client.DisconnectedEvent += (s, e) => { if (InvokeRequired) { Invoke((MethodInvoker) delegate { DisconnectedEvent?.Invoke(s, e); }); } else { DisconnectedEvent?.Invoke(s, e); } }; bool retVal = m_client.ConnectVnc(); if (!retVal) { m_client.Dispose(); m_client = null; return(false); } // Draw the whole for the first time. m_needsRedraw = true; // Initialize zoom ratio for mouseEvent. m_xZoom = new Fraction(this.Width, m_client.ServerInitBody.FramebufferWidth, 10); m_yZoom = new Fraction(this.Height, m_client.ServerInitBody.FramebufferHeight, 10); // Create new image to draw OpenCv Mat. m_image?.Dispose(); m_image = new Bitmap(m_client.ServerInitBody.FramebufferWidth, m_client.ServerInitBody.FramebufferHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb); // Create read thread m_cancelTokenSource?.Dispose(); m_cancelTokenSource = new CancellationTokenSource(); var token = m_cancelTokenSource.Token; m_readTask?.Dispose(); m_readTask = Task.Factory.StartNew(() => { while (true) { if (m_client != null && m_client.Connected) { // If Read is failed, it returns null. var body = m_client.ReadServerMessage(); if (body == null) { // Disconnect m_client?.DisconnectVnc(); // Execute to draw this control black. // Without this, the screen will not be updated and the VNC image will remain. NativeMethods.InvalidateRect(m_handle, (IntPtr)0, false); return; } if (body.MessageType == VncEnum.MessageTypeServerToClient.FramebufferUpdate) { NativeMethods.InvalidateRect(m_handle, (IntPtr)0, false); lock (m_client.CanvasLock) { m_encodeList.Add(body.EncodeList); } } if (token.IsCancellationRequested) { return; } } } }, m_cancelTokenSource.Token); return(retVal); }
private void c_connectButton_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(c_addressTextBox.Text) || string.IsNullOrEmpty(c_passwordTextBox.Text)) { return; } if (m_connectMode) { // Protocol Version VncEnum.Version version = VncEnum.Version.None; if (Properties.Settings.Default.Protocol33) { version = VncEnum.Version.Version33; } else if (Properties.Settings.Default.Protocol37) { version = VncEnum.Version.Version37; } else if (Properties.Settings.Default.Protocol38) { version = VncEnum.Version.Version38; } // Encodings var useEncodingList = new List <VncEnum.EncodeType>(); foreach (var v in Properties.Settings.Default.EncodingList) { useEncodingList.Add((VncEnum.EncodeType)(int.Parse(v))); } // Colour bool isColourSpecified = !Properties.Settings.Default.ColourLevelReceived; PixelFormat specifiedColor = PixelFormat.PixelFormatDepth24; if (Properties.Settings.Default.ColourLevelDepth16) { specifiedColor = PixelFormat.PixelFormatDepth16; } else if (Properties.Settings.Default.ColourLevelDepth8Colour64) { specifiedColor = PixelFormat.PixelFormatDepth8Colour64; } else if (Properties.Settings.Default.ColourLevelDepth8Colour8) { specifiedColor = PixelFormat.PixelFormatDepth8Colour8; } var config = new VncConfig(c_addressTextBox.Text, 5900, c_passwordTextBox.Text, version, useEncodingList.ToArray(), isColourSpecified, specifiedColor, Properties.Settings.Default.SendKeyboard, Properties.Settings.Default.SendPointer); bool result = c_vncControl.Connect(config); if (result) { c_connectButton.Text = "Disconnect"; m_connectMode = false; } } else { c_vncControl.Disconnect(); c_connectButton.Text = "Connect"; m_connectMode = true; } }