public void UnHook() { Win32Api.UnhookWindowsHookEx(hHook); }
private void mh_MouseMoveEvent(object sender, MouseEventArgs e) { this.label1.Location = new Point(e.X + 10, e.Y - 10); this.label1.Text = "Position:" + e.X + "," + e.Y; this.label1.Visible = true; if (CatchStart) { this.timer1.Enabled = false; //Bitmap destBmp = (Bitmap)originBmp.Clone(); Point newPoint = new Point(DownPoint.X, DownPoint.Y); Point p2 = new Point(e.X, e.Y); Bitmap destBmp = CapRect(DownPoint, p2); if (destBmp == null) { return; } //Bitmap destBmp = CapActvieScreen(); //Bitmap destBmp = new Bitmap(GetMouseScreen().Bounds.Width, GetMouseScreen().Bounds.Height); System.IntPtr DesktopHandle = Win32Api.GetDC(System.IntPtr.Zero); Graphics g = Graphics.FromHdc(DesktopHandle); destBmp.Save(@"d:\destbmp.png"); Pen pen = new Pen(Color.Blue, 4); //Brush brush = new Brush(Color.LightGray,); int width = Math.Abs(e.X - DownPoint.X); int height = Math.Abs(e.Y - DownPoint.Y); //判断矩形的起始坐标 if (e.X < DownPoint.X) { newPoint.X = e.X; } if (e.Y < DownPoint.Y) { newPoint.Y = e.Y; } //保存矩形 CatchRect = new Rectangle(DownPoint, new Size(width, height)); //this.Opacity = 0; //将矩形画在这个画板上 g.DrawRectangle(pen, CatchRect); //释放这个画板 g.Dispose(); //重新创建一个Graphics类 Graphics g1 = this.CreateGraphics(); //如果之前那个画板不释放,而直接g = this.CreateGraphics()这样的话无法释放掉第一次创建的g,因为只是把地址转到新的g了,如同string一样。 //将刚才所画的图片画到这个窗体上 g1.DrawRectangle(pen, CatchRect); Clipboard.SetData(DataFormats.Bitmap, destBmp); destBmp.Save(@"d:\destbmp.png"); //这个也可以属于二次缓冲技术,如果直接将矩形画在窗体上,会造成图片抖动并且会有无数个矩形 //释放这个画板 g1.Dispose(); ////释放掉Bmp对象。 k destBmp.Dispose(); } }
public int SetHook() { hProc = new Win32Api.HookProc(MouseHookProc); hHook = Win32Api.SetWindowsHookEx(WH_MOUSE_LL, hProc, IntPtr.Zero, 0); return(hHook); }