Click here to Skip to main content
15,880,427 members
Articles / Programming Languages / C#

Screen Cast Server with Control

Rate me:
Please Sign up or sign in to vote.
4.66/5 (19 votes)
29 Sep 2009CPOL2 min read 49.8K   2.1K   44  
A screen cast server with mouse, keyboard, and clipboard control
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using ScreenHost;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
using System.Net;
using System.Runtime.Remoting.Messaging;
using System.Drawing.Imaging;
using System.Threading;

namespace ScreenCastClient
{
    public partial class Client : Form
    {
        #region Variables

        bool connected = false;

        int refreshRate = 1;
        private static MemoryStream ms;
        private static Image img;
        string hostIP = "";

        TcpChannel chan1 = new TcpChannel();
        ScreenHost.ScreenObject hostInstance;

        Point mouseXY = new Point();

        int imgFormat;

        #endregion

        public Client()
        {
            InitializeComponent();
        }

        #region Form Events

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Text = "Screen Cast Client v" + Application.ProductVersion;
            cmbCompression.SelectedIndex = 4;
            chan1 = new TcpChannel(8088);
            picCast.KeyDown += new KeyEventHandler(picCast_KeyDown);
            picCast.KeyUp += new KeyEventHandler(picCast_KeyUp);
        }

        private void tbRefreshRate_Scroll(object sender, EventArgs e)
        {
            refreshRate = tbRefreshRate.Value;
            castTimer.Interval = refreshRate;
            lblRefreshRate.Text = castTimer.Interval.ToString() + " ms";
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            if (connected == false) //Not Connected
            {
                Connect();
            }
            else
            {
                Disconnect();
            }
        }

        private void cmbCompression_SelectedIndexChanged(object sender, EventArgs e)
        {
            imgFormat = cmbCompression.SelectedIndex;
        }

        private void chkMouse_CheckedChanged(object sender, EventArgs e)
        {
            chkMouse.BackColor = (chkMouse.Checked) ? Color.Orange : Color.White;
        }

        private void chkKeyboard_CheckedChanged(object sender, EventArgs e)
        {
            chkKeyboard.BackColor = (chkKeyboard.Checked) ? Color.Orange : Color.White;
            picCast.Focus();
        }

        private void picCast_Click(object sender, EventArgs e)
        {
            picCast.Focus();
        }

        //Mouse Events
        private void picCast_MouseMove(object sender, MouseEventArgs e)
        {
            try
            {
                mouseXY = e.Location;

                if (connected)
                {
                    if (chkMouse.Checked)
                    {
                        hostInstance.SetMouseLocation(mouseXY);
                    }
                }
            }
            catch (Exception ex)
            {
                updateText(lblMessage, ex.Message);
            }

        }

        private void picCast_MouseDown(object sender, MouseEventArgs e)
        {
            if (chkMouse.Checked)
            {
                if (e.Button == MouseButtons.Left)
                {
                    hostInstance.Left_Click_Down();
                }
                if (e.Button == MouseButtons.Right)
                {
                    hostInstance.Right_Click_Down();
                }
            }
        }

        private void picCast_MouseUp(object sender, MouseEventArgs e)
        {
            if (chkMouse.Checked)
            {
                if (e.Button == MouseButtons.Left)
                {
                    hostInstance.Left_Click_Up();
                }
                if (e.Button == MouseButtons.Right)
                {
                    hostInstance.Right_Click_Up();
                }
            }
        }


        //Keyboard Events
        void picCast_KeyDown(object sender, KeyEventArgs e)
        {
            if (chkKeyboard.Checked)
            {
                byte[] keyPressed = BitConverter.GetBytes(e.KeyValue);
                updateText(lblInput, "Keyboard down event: " + e.KeyCode.ToString());
                hostInstance.keyboard_key_down(keyPressed[0]);
            }
        }

        void picCast_KeyUp(object sender, KeyEventArgs e)
        {
            if (chkKeyboard.Checked)
            {
                byte[] keyPressed = BitConverter.GetBytes(e.KeyValue);
                updateText(lblInput, "Keyboard up event: " + e.KeyCode.ToString());
                hostInstance.keyboard_key_up(keyPressed[0]);
            }
        }

        private void interface_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (chkKeyboard.Checked)
            {
                byte[] keyPressed = BitConverter.GetBytes(e.KeyValue);
                updateText(lblInput, "Keyboard event: " + e.KeyCode.ToString());
                hostInstance.keyboard_key_down(keyPressed[0]);
                hostInstance.keyboard_key_up(keyPressed[0]);
            }
        }

        //Clipboard Events
        private void btnClipboardGet_Click(object sender, EventArgs e)
        {
            string tmpStr = "";
            try
            {
                tmpStr = hostInstance.getClipboardText();
                Clipboard.SetText(tmpStr);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        [STAThread]
        private void btnClipboardSet_Click(object sender, EventArgs e)
        {
            string tmpStr = "";

            try
            {
                tmpStr = Clipboard.GetText();
                hostInstance.setClipboardText(tmpStr);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #endregion

        #region Methods

        private void Connect()
        {
            try 
	        {	    
                //Set the Host IP
                hostIP = txtIPAddress.Text;

                //Register and Initialize the TCP Channel
                InitializeRemoteServer();
                hostInstance = new ScreenObject();

                castTimer.Enabled = true;
                connected = true;
                btnConnect.ImageIndex = 1;
                btnConnect.Text = "Disconnect";
                updatePanel(pnlBottom, true);
                updateText(lblMessage, "Connected to " + hostIP + " - " + DateTime.Now);
	        }
	        catch (Exception exc)
	        {
                updateText(lblMessage, exc.Message);
	        }
        }

        private void Disconnect()
        {
            try
            {
                //Unregister the TCP Channel
                ChannelServices.UnregisterChannel(chan1);

                castTimer.Enabled = false;
                bgWorker1.CancelAsync();
                connected = false;
                btnConnect.ImageIndex = 0;
                btnConnect.Text = "Connect";
                updatePanel(pnlBottom, false);
            }
            catch (Exception exc)
            {
                updateText(lblMessage, exc.Message);
                MessageBox.Show(exc.Message);
            }
        }

        private void InitializeRemoteServer()
        {
            try
            {
                
                ChannelServices.RegisterChannel(chan1, false);
                RemotingConfiguration.CustomErrorsMode = CustomErrorsModes.Off;
                RemotingConfiguration.RegisterWellKnownClientType(typeof(ScreenObject), "tcp://" + hostIP + ":8082/ScreenObject");
            }
            catch (Exception ex)
            {
                updateText(lblMessage, ex.Message);
            }
        }

        #endregion

        #region Timers and BackgroundWorkers

        private void castTimer_Tick(object sender, EventArgs e)
        {
            if (!bgWorker1.IsBusy)
            {
                bgWorker1.RunWorkerAsync();
            }
        }

        private void bgWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                if (hostInstance != null)
                {
                    ms = hostInstance.CastScreen(imgFormat,true);
                    img = Image.FromStream(ms);
                    updatePictureBox(picCast, img);
                    updateText(lblMessage, "Screen Updated - " + DateTime.Now);
                }
            }
            catch (Exception exc)
            {
                if (bgWorker1.IsBusy)
                { bgWorker1.CancelAsync(); }
                updateText(lblMessage, exc.Message);
            }
        }

        #endregion

        #region Delegate Methods

        private delegate void cmbDelegate(ComboBox cmb, string value, bool add);
        private void updateComboBox(ComboBox cmb, string value, bool add)
        {
            if (cmb.InvokeRequired)
            { cmb.Invoke(new cmbDelegate(updateComboBox), new object[] { cmb, value, add }); }
            else
            {
                if (add)
                { cmb.Items.Add(value); }
                else
                { cmb.Items.Clear(); }
            }
        }

        private delegate void picDelegate(PictureBox pic, Image value);
        private void updatePictureBox(PictureBox pic, Image value)
        {
            if (pic.InvokeRequired)
            { pic.Invoke(new picDelegate(updatePictureBox), new object[] { pic, value }); }
            else
            {
                pic.BackgroundImage = value;
            }
        }

        private delegate void panelDelegate(Panel pnl, bool value);
        private void updatePanel(Panel pnl, bool value)
        {
            if (pnl.InvokeRequired)
            { pnl.Invoke(new panelDelegate(updatePanel), new object[] { pnl, value }); }
            else
            {
                pnl.Visible = value;
            }
        }

        private delegate void textDelegate(Control cntrl, string value);
        private void updateText(Control cntrl, string value)
        {
            if (cntrl.InvokeRequired)
            { cntrl.Invoke(new textDelegate(updateText), new object[] { cntrl, value }); }
            else
            {
                cntrl.Text = value;
            }
        }

        #endregion

        #region Context Menu

        private void showHideMenuToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pnlTopMenu.Visible = !pnlTopMenu.Visible;
        }

       

        #endregion

        private void btnInfo_Click(object sender, EventArgs e)
        {
            //Dont be a Doosh.. keep giving credit!
            MessageBox.Show("Screen Cast Client\n\rVersion: " + Application.ProductVersion + "\n\rby Pieter Myburgh","About",MessageBoxButtons.OK,MessageBoxIcon.Information);
        }

        private void btnShowHide_Click(object sender, EventArgs e)
        {
            pnlTopMenu.Visible = !pnlTopMenu.Visible;
        }

        

       

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Backbase
Netherlands Netherlands
Senior Solutions Architect.

I reject your code and substitute my own Smile | :) !

Comments and Discussions