//根据鼠标位置找寻窗体平绘制边框 private void FoundAndDrawWindowRect() { MouseAndKeyHelper.LPPOINT pt = new MouseAndKeyHelper.LPPOINT(); pt.X = MousePosition.X; pt.Y = MousePosition.Y; IntPtr hWnd = MouseAndKeyHelper.ChildWindowFromPointEx(MouseAndKeyHelper.GetDesktopWindow(), pt, MouseAndKeyHelper.CWP_SKIPINVISIBL | MouseAndKeyHelper.CWP_SKIPDISABLED); if (hWnd != IntPtr.Zero) { IntPtr hTemp = hWnd; while (true) { MouseAndKeyHelper.ScreenToClient(hTemp, out pt); hTemp = MouseAndKeyHelper.ChildWindowFromPointEx(hWnd, pt, MouseAndKeyHelper.CWP_SKIPINVISIBL); if (hTemp == IntPtr.Zero || hTemp == hWnd) { break; } hWnd = hTemp; pt.X = MousePosition.X; pt.Y = MousePosition.Y; //坐标还原为屏幕坐标 } MouseAndKeyHelper.LPRECT rect = new MouseAndKeyHelper.LPRECT(); MouseAndKeyHelper.GetWindowRect(hWnd, out rect); imageProcessBox1.SetSelectRect(new Rectangle(rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top)); } }
/// <summary> /// 开启客户版客户端 /// </summary> public void Start() { httpProxy = new HttpProxy(); _MouseAndKeyHelper = new MouseAndKeyHelper(); _mCleint = new MessageClient(this._userName, this.Dgcode); _mCleint.OnMessage += webCient_OnMessage; _mCleint.OnNotice += _mClient_OnNotice; var isConnected = _mCleint.Connect(); _mCleint.isCloseing = false; cts = new CancellationTokenSource(); if (timer == null) { timer = new System.Windows.Forms.Timer(); } timer.Interval = 1000;//每秒查询一次鼠标和键盘休闲时间 timer.Tick += new EventHandler(timer_Event); }
/// <summary> /// 桌面共享、远程协助工具类 /// </summary> /// <param name="userName"></param> /// <param name="remote"></param> public RequestRemoteHelper(string userName) { _MouseAndKeyHelper = new MouseAndKeyHelper(); _mCleint = new MessageClient("wenlirdp_" + userName); _mCleint.OnMessage += webCient_OnMessage; }
/// <summary> /// 发送图像到远程 /// </summary> private void SendImage() { Task.Factory.StartNew(() => { Rectangle rectangle = new Rectangle(0, 0, Screen.AllScreens[0].Bounds.Width, Screen.AllScreens[0].Bounds.Height); while (!_isClose) { if (_isStartCapture) { using (Bitmap desktopImage = new Bitmap(rectangle.Width, rectangle.Height)) { using (Graphics g = Graphics.FromImage(desktopImage)) { g.CopyFromScreen(0, 0, 0, 0, desktopImage.Size); MouseAndKeyHelper.DrawMouse(g); using (MemoryStream ms = ImageHelper.GetLossyCompression(desktopImage, (int)_qualityLevel, "W", 800)) { if (desktopImage != null) { try { ms.Position = 0; _mCleint.SendFileAsync(_remote, ms.GetBuffer()); var sec = 1000; if (_fps > 0) { sec = 1000 / _fps; } else { sec = 2000; } Thread.Sleep(sec); } catch { } finally { ms.Dispose(); g.Dispose(); desktopImage.Dispose(); } } } } } } else { Thread.Sleep(100); } } }); }
private static void timer_Event(object sender, EventArgs e) { if (MouseAndKeyHelper.GetLastInputTime() >= 5) { _isSend = false; } else { _isSend = true; } }
//响应四个按键实现精确移动 protected override void OnKeyPress(KeyPressEventArgs e) { if (e.KeyChar == 'w') { MouseAndKeyHelper.SetCursorPos(MousePosition.X, MousePosition.Y - 1); } else if (e.KeyChar == 's') { MouseAndKeyHelper.SetCursorPos(MousePosition.X, MousePosition.Y + 1); } else if (e.KeyChar == 'a') { MouseAndKeyHelper.SetCursorPos(MousePosition.X - 1, MousePosition.Y); } else if (e.KeyChar == 'd') { MouseAndKeyHelper.SetCursorPos(MousePosition.X + 1, MousePosition.Y); } base.OnKeyPress(e); }
//在桌面绘制鼠标 public static Rectangle DrawCurToScreen() { //如果直接将捕获当的鼠标画在bmp上 光标不会反色 指针边框也很浓 也就是说 //尽管bmp上绘制了图像 绘制鼠标的时候还是以黑色作为鼠标的背景 然后在将混合好的鼠标绘制到图像 会很别扭 //所以 干脆直接在桌面把鼠标绘制出来再截取桌面 using (Graphics g = Graphics.FromHwnd(IntPtr.Zero)) { //传入0默认就是桌面 MouseAndKeyHelper.GetDesktopWindow()也可以 MouseAndKeyHelper.CURSORINFO pci; pci.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(typeof(MouseAndKeyHelper.CURSORINFO)); MouseAndKeyHelper.GetCursorInfo(out pci); if (pci.hCursor != IntPtr.Zero) { Cursor cur = new Cursor(pci.hCursor); Rectangle rect_cur = new Rectangle((Point)((Size)MousePosition - (Size)cur.HotSpot), cur.Size); g.CopyFromScreen(0, 0, 0, 0, Screen.PrimaryScreen.Bounds.Size); //g.CopyFromScreen(rect_cur.Location, rect_cur.Location, rect_cur.Size); //在桌面绘制鼠标前 先在桌面绘制一下当前的桌面图像 //如果不绘制当前桌面 那么cur.Draw的时候会是用历史桌面的快照 进行鼠标的混合 那么到时候混出现底色(测试中就是这样的) cur.Draw(g, rect_cur); return(rect_cur); } return(Rectangle.Empty); } }
/// <summary> /// 每5秒发送一张整图防错 /// </summary> public void SendImageKeep() { Task.Factory.StartNew(() => { Rectangle rectangle = new Rectangle(0, 0, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); while (!_isClose) { try { if (true) { using (Bitmap desktopImage = new Bitmap(rectangle.Width, rectangle.Height)) { using (Graphics g = Graphics.FromImage(desktopImage)) { try { g.CopyFromScreen(0, 0, 0, 0, desktopImage.Size); } catch (Exception ex) { continue; } MouseAndKeyHelper.DrawMouse(g); SendImageFile(desktopImage, g); Thread.Sleep(secKeep); } } } } catch (Exception ex) { continue; } } }, cts.Token); }
/// <summary> /// 发送图像到远程 /// </summary> private void SendImage() { Task.Factory.StartNew(() => { //Bitmap lastBitmap = null; Rectangle rectangle = new Rectangle(0, 0, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); while (!_isClose) { try { if (true) { using (Bitmap desktopImage = new Bitmap(rectangle.Width, rectangle.Height)) { using (Graphics g = Graphics.FromImage(desktopImage)) { try { g.CopyFromScreen(0, 0, 0, 0, desktopImage.Size); } catch (Exception ex) { continue; } MouseAndKeyHelper.DrawMouse(g); //比较此次截图与上一张截图的差异 if (lastBitmap != null) { List <Rectangle> rects = ImageComparer.Compare(lastBitmap, desktopImage); lastBitmap = (Bitmap)desktopImage.Clone(); if (rects.Count == 0)//无变化不发送 { Thread.Sleep(sec); } if (rects.Count <= 480)//差异小于7块则分段传输 { Dictionary <Rectangle, Bitmap> dic = new Dictionary <Rectangle, Bitmap>(); foreach (var rect in rects) { Bitmap bmSmall = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppRgb); using (Graphics grSmall = Graphics.FromImage(bmSmall)) { grSmall.DrawImage(desktopImage, 0, 0, rect, GraphicsUnit.Pixel); grSmall.Save(); grSmall.Dispose(); } dic.Add(rect, (Bitmap)bmSmall.Clone()); } if (dic.Count > 0) { _mCleint.SendFileSlice(_remote, CompressHelper.Compress(SerializeHelper.ByteSerialize(dic))); isBigChnage = false; Thread.Sleep(sec); } } else {//发送完整的图片 if (isBigChnage == false) { SendImageFile(desktopImage, g); lastBitmap = (Bitmap)desktopImage.Clone();//要在之前否则销毁掉了 Thread.Sleep(sec); } else { isBigChnage = false; } } } else { //第一张图片 lastBitmap = (Bitmap)desktopImage.Clone(); //要在之前否则销毁掉了 SendImageFile(desktopImage, g); Thread.Sleep(sec); } } } } else { Thread.Sleep(100); } } catch (Exception ex) { continue; } } }, cts.Token); }