private void ConnectButton_Click(object sender, EventArgs e) { //check if client is connected if (client == null) { //create picturebox to receive remote screen PictureBox picture = new PictureBox(); picture.Anchor = ImageHolderPanel.Anchor; picture.Size = ImageHolderPanel.Size; picture.SizeMode = PictureBoxSizeMode.StretchImage; //add input handlers picture.MouseDown += HandleScreenClick; picture.MouseUp += HandleScreenUnclick; picture.MouseMove += HandleScreenMove; picture.MouseEnter += delegate { if (MouseInputBox.Checked) { Cursor.Hide(); } }; picture.MouseLeave += delegate { Cursor.Show(); }; //add picturebox to panel ImageHolderPanel.Controls.Clear(); ImageHolderPanel.Controls.Add(picture); picture.Focus(); try { //create client and connect client = new ScreenShareClient(picture); client.Connect(IPAddress.Parse(IPBox.Text)); } catch (Exception ex) { //show error on screen and remove picture box MessageBox.Show("Cannot connect: " + ex.Message); client = null; ImageHolderPanel.Controls.Clear(); return; } //add disconnect handler client.ClientDisconnected += HandleDisconnected; ConnectButton.Text = "Disconnect"; IPBox.Enabled = false; ShareScreenButton.Enabled = false; } else { //close connection client.Close(); client = null; //remove picture and change connect button ImageHolderPanel.Controls.Clear(); ConnectButton.Text = "Connect"; IPBox.Enabled = true; ShareScreenButton.Enabled = true; } }
private void HandleDisconnected() { //remove picture and change connect button client = null; Invoke(new Action(() => { ImageHolderPanel.Controls.Clear(); ConnectButton.Text = "Connect"; IPBox.Enabled = true; ShareScreenButton.Enabled = true; })); }