void Init()
        {
            //创建socket
            IPEndPoint ipEnd = new IPEndPoint(this.ip, this.port);

            socket = new Socket(AddressFamily.InterNetwork,
                                SocketType.Stream, ProtocolType.Tcp);
            try
            {
                socket.Connect(ipEnd);
                socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);
            }
            catch (SocketException e)
            {
                SetMessage("链接远端失败 " + e.Message);
                return;
            }


            context = ControlRemote.NewContext(this.ctrl_pic.Handle, socket.Handle,
                                               this.startCall, this.disconnectCall, this.errorMessageCall);
            if (context == System.IntPtr.Zero)
            {
                SetMessage("创建上下文失败!");
                return;
            }

            SetMessage("链接远端成功…… ");

            ControlRemote.StartWork(context);
        }
Beispiel #2
0
 static void Main()
 {
     ControlRemote.DLLInit();
     Application.EnableVisualStyles();
     Application.SetCompatibleTextRenderingDefault(false);
     Application.Run(new Main());
 }
 //拦截最小化事件
 protected override void WndProc(ref Message m)
 {
     if (m.Msg == WM_SYSCOMMAND)
     {
         if (m.WParam.ToInt32() == SC_RESTORE)
         {
             if (is_start && context != System.IntPtr.Zero)
             {
                 ControlRemote.EnableDraw(context, 1);//启用绘制
             }
             //窗口还原!
         }
         if (m.WParam.ToInt32() == SC_MINIMIZE)
         {
             if (is_start && context != System.IntPtr.Zero)
             {
                 ControlRemote.EnableDraw(context, 0);//取消绘制,让线程不阻塞,第二次还原窗口画面不延迟
             }
             //"点击最小化按钮!");
         }
         if (m.WParam.ToInt32() == SC_MAXIMIZE)
         {
             //"点击最大化按钮!");
         }
         if (m.WParam.ToInt32() == SC_CLOSE)
         {
             //点击窗口关闭按钮!");
         }
     }
     base.WndProc(ref m);
 }
        private void Ctrl_pic_MouseWheel(object sender, MouseEventArgs e)
        {
            if (!is_start)
            {
                return;
            }

            if (context == System.IntPtr.Zero)
            {
                return;
            }

            ControlRemote.SendMouseEvent(context, ControlRemote.WM_MOUSEWHEEL, e.Delta);
        }
        void Re()
        {
            //重连步骤
            //启动一个非UI线程
            System.Threading.Tasks.Task.Factory.StartNew(() =>
            {
                is_start = false;
                socket.Close();
                socket.Dispose();

                if (context != System.IntPtr.Zero)
                {
                    //用非ui线程去关闭DeleteStopContext
                    ControlRemote.DeleteStopContext(context);
                    context = System.IntPtr.Zero;
                }


                //ui线程 然后用this.Invoke 去创建   this.ctrl_pic
                this.Invoke(new Action(() => {
                    //加一句
                    this.ctrl_pic.Dispose();

                    this.ctrl_pic = new PictureBox();
                    this.Controls.Add(this.ctrl_pic);
                    //设置ctrl_panel外观位置
                    this.ctrl_pic.BackColor = System.Drawing.SystemColors.Desktop;
                    this.ctrl_pic.Location  = new System.Drawing.Point(0, 0);
                    this.ctrl_pic.Margin    = new System.Windows.Forms.Padding(0);

                    //鼠标事件
                    this.ctrl_pic.MouseWheel += Ctrl_pic_MouseWheel;

                    this.ctrl_pic.MouseDown += Ctrl_pic_MouseDown;
                    this.ctrl_pic.MouseUp   += Ctrl_pic_MouseUp;

                    this.ctrl_pic.MouseMove += Ctrl_pic_MouseMove;

                    this.ctrl_pic.MouseEnter += Ctrl_pic_MouseEnter;
                    this.ctrl_pic.MouseLeave += Ctrl_pic_MouseLeave;
                }));

                //最后init 去链接
                Init();
            });
        }
        private void Ctrl_pic_MouseMove(object sender, MouseEventArgs e)
        {
            if (!is_start)
            {
                return;
            }

            if (context == System.IntPtr.Zero)
            {
                return;
            }

            ControlRemote.SendCursorPos(context, e.X, e.Y);

            SetMessage("MouseMove " + e.X + " x " + e.Y);

            //  Debug.WriteLine("MouseMove " + e.X + " x " + e.Y);
        }
        private void RemoteDesktop_KeyDown(object sender, KeyEventArgs e)
        {
            if (!is_start)
            {
                return;
            }

            if (context == System.IntPtr.Zero)
            {
                return;
            }

            ControlRemote.SendKeyEvent(context, (int)e.KeyCode, true);

            SetMessage("KeyDown " + e.KeyCode.ToString() + "(" + (int)e.KeyCode + ")");

            // Debug.WriteLine("KeyDown " + e.KeyCode.ToString() + "(" + (int)e.KeyCode + ")");

            e.Handled = true;
        }
        private void Ctrl_pic_MouseDown(object sender, MouseEventArgs e)
        {
            if (!is_start)
            {
                return;
            }

            if (context == System.IntPtr.Zero)
            {
                return;
            }

            switch (e.Button)
            {
            case MouseButtons.Left:
                ControlRemote.SendMouseEvent(context, ControlRemote.WM_LBUTTONDOWN, 0);
                break;

            case MouseButtons.None:
                break;

            case MouseButtons.Right:
                ControlRemote.SendMouseEvent(context, ControlRemote.WM_RBUTTONDOWN, 0);
                break;

            case MouseButtons.Middle:
                ControlRemote.SendMouseEvent(context, ControlRemote.WM_MBUTTONDOWN, 0);
                break;

            case MouseButtons.XButton1:
                break;

            case MouseButtons.XButton2:
                break;

            default:
                break;
            }
        }
        private void RemoteDesktop_FormClosing(object sender, FormClosingEventArgs e)
        {
            socket.Close();
            socket.Dispose();


            Task.Factory.StartNew(() =>
            {
                lock (lock_object)
                {
                    if (context != System.IntPtr.Zero)
                    {
                        ControlRemote.DeleteStopContext(context);
                        context = System.IntPtr.Zero;
                    }
                    is_close = true;
                }
            });

            if (!is_close)
            {
                e.Cancel = true;
            }
        }