Hook部分的代码:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;using System.Diagnostics;
using System.Runtime.InteropServices;namespace AutoDrawRect
{
public class MouseHook
{
//好吧这个没有用到
private bool isSet;
public bool IsSet {
get { return isSet; }
}
//这个也没有用到
private int handleOfHook;
public int HandleOfHook {
get { return handleOfHook; }
}
//这个还是没有用到、、、淡定!
private bool isStopMsg;
public bool IsStopMsg {
get { return isStopMsg; }
set { isStopMsg = value; }
}
//自己定义了一个事件 放到Hook里面去
public delegate void MEventhandler(object sender, MouseInfoEventArys e);
public event MEventhandler HooKMouseEvent; [DllImport("user32.dll")]//设置钩子 第二个参数为回调函数指针
public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hmod, int dwThreadid);
[DllImport("user32.dll")]//传递到下一个钩子
public static extern int CallNextHookEx(int hHook, int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]//卸载钩子
public static extern bool UnhookWindowsHookEx(int hHook);
[DllImport("kernel32.dll")]//获取模块句柄
public static extern IntPtr GetModuleHandle(string lpModuleName);
public const int WH_MOUSE_LL = 14;//全局鼠标Hook 7是局部的 13全局键盘 2局部键盘
public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);//话说c#里面委托就是函数指针?、 private const int WM_LBUTTONDOWN = 0x201; //在Hook里面判断是否左键点下
private const int WM_RBUTTONUP = 0x205; //在Hook里面判断是否右键抬起 public struct POINT {//鼠标位置的结构体
public int x;
public int y;
}
public struct MouseLLInfo {//全局鼠标Hook的结构体
public POINT pt; //其实这里可以用Point只是这个新建的类里面没有应用System.Windows.Forms(应该是这个)
public int mouseData;
public int flags;
public int time;
public int dwExtraInfo;
} GCHandle gc;//好吧 话说就是应为这个东西害得我研究了两天 没有这个的话 程序运行一会儿就提示崩溃了
//因为垃圾回收期把我的回调函数当垃圾收了 所以运行程序的时候 一会儿就提示我 一个垃圾的回调导致程序崩溃
//在非托管调用托管的时候 必须保持托管代码的或活动性 大概就这个意思 反正就是被收废品的收了、害的我用.net3.5用其他方式设置Hook public int MouseHookProcedure(int nCode, IntPtr wParam, IntPtr lParam) {//这个就是回调函数了
if (nCode >= 0 && HooKMouseEvent != null) {//先判断是否事件被绑定(感觉有点多余的判断 丫的我不在上面绑定 我写Hook干嘛)
//话说是把内存的什么什么转换成结构体
MouseLLInfo mouseInfo = (MouseLLInfo)Marshal.PtrToStructure(lParam, typeof(MouseLLInfo));
Btn btn = Btn.None; //自己定义的一个枚举 里面只有三个值
if (wParam == (IntPtr)WM_LBUTTONDOWN) { //如果左键被点下
btn = Btn.LeftDowm;
} else if (wParam == (IntPtr)WM_RBUTTONUP) { //如果右键被抬起
btn = Btn.RightUp;
}
//好吧 我就不知道当时我怎么想的 在Hook里面获取的坐标 有负数的现象 所以在那边 我没用这个坐标
MouseInfoEventArys e = new MouseInfoEventArys(btn, mouseInfo.pt.x, mouseInfo.pt.y);
HooKMouseEvent(this, e);//触发绑定到这个上面的事件
}
return CallNextHookEx(handleOfHook, nCode, wParam, lParam);//继续下一个钩子
}
public bool SetMouseHook() { //设置Hook
if (isSet) {//如果已经设置了 就不要设置啦、、、
return false;
}
HookProc MouseCallBack = new HookProc(MouseHookProcedure);
handleOfHook = SetWindowsHookEx(WH_MOUSE_LL, MouseCallBack,
GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName), 0);
if (handleOfHook != 0) {//如果设置成功、、
gc = GCHandle.Alloc(MouseCallBack);//这个就是那个什么什么、、然后我的回调就不会被收废品的拣去了
isSet = true;
return true;
}
return false;
}
public bool UnLoadMouseHook() {
if (!isSet) {//如果装都没有装那么久不要卸载啦、、
return false;
}
if (UnhookWindowsHookEx(handleOfHook)) {
gc.Free();//将回调释放掉、、
isSet = false;
return true;
}
return false;
} } public enum Btn//我只感觉到这三个有用、(应该是两个 左键点下 右键抬起)
{
LeftDowm, RightUp, None
}
public class MouseInfoEventArys {//话说定义事件的时候都是这么写的 所以我也弄一个内出来保存事件参数
private int x;//坐标 多余的后来才发现 鼠标慢慢贴近屏幕边缘的时候 3 2 1 0 -1 、、丫的 负数都出来了
public int X {
get { return x; }
} private int y;//坐标
public int Y {
get { return y; }
} private Btn mBtn;
public Btn MBtn {
get { return mBtn; }//鼠标的情况
}
public MouseInfoEventArys(Btn btn,int x,int y) {//构造器
mBtn = btn;
this.x = x;
this.y = y;
} }
}
using System.Collections.Generic;
using System.Linq;
using System.Text;using System.Diagnostics;
using System.Runtime.InteropServices;namespace AutoDrawRect
{
public class MouseHook
{
//好吧这个没有用到
private bool isSet;
public bool IsSet {
get { return isSet; }
}
//这个也没有用到
private int handleOfHook;
public int HandleOfHook {
get { return handleOfHook; }
}
//这个还是没有用到、、、淡定!
private bool isStopMsg;
public bool IsStopMsg {
get { return isStopMsg; }
set { isStopMsg = value; }
}
//自己定义了一个事件 放到Hook里面去
public delegate void MEventhandler(object sender, MouseInfoEventArys e);
public event MEventhandler HooKMouseEvent; [DllImport("user32.dll")]//设置钩子 第二个参数为回调函数指针
public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hmod, int dwThreadid);
[DllImport("user32.dll")]//传递到下一个钩子
public static extern int CallNextHookEx(int hHook, int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]//卸载钩子
public static extern bool UnhookWindowsHookEx(int hHook);
[DllImport("kernel32.dll")]//获取模块句柄
public static extern IntPtr GetModuleHandle(string lpModuleName);
public const int WH_MOUSE_LL = 14;//全局鼠标Hook 7是局部的 13全局键盘 2局部键盘
public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);//话说c#里面委托就是函数指针?、 private const int WM_LBUTTONDOWN = 0x201; //在Hook里面判断是否左键点下
private const int WM_RBUTTONUP = 0x205; //在Hook里面判断是否右键抬起 public struct POINT {//鼠标位置的结构体
public int x;
public int y;
}
public struct MouseLLInfo {//全局鼠标Hook的结构体
public POINT pt; //其实这里可以用Point只是这个新建的类里面没有应用System.Windows.Forms(应该是这个)
public int mouseData;
public int flags;
public int time;
public int dwExtraInfo;
} GCHandle gc;//好吧 话说就是应为这个东西害得我研究了两天 没有这个的话 程序运行一会儿就提示崩溃了
//因为垃圾回收期把我的回调函数当垃圾收了 所以运行程序的时候 一会儿就提示我 一个垃圾的回调导致程序崩溃
//在非托管调用托管的时候 必须保持托管代码的或活动性 大概就这个意思 反正就是被收废品的收了、害的我用.net3.5用其他方式设置Hook public int MouseHookProcedure(int nCode, IntPtr wParam, IntPtr lParam) {//这个就是回调函数了
if (nCode >= 0 && HooKMouseEvent != null) {//先判断是否事件被绑定(感觉有点多余的判断 丫的我不在上面绑定 我写Hook干嘛)
//话说是把内存的什么什么转换成结构体
MouseLLInfo mouseInfo = (MouseLLInfo)Marshal.PtrToStructure(lParam, typeof(MouseLLInfo));
Btn btn = Btn.None; //自己定义的一个枚举 里面只有三个值
if (wParam == (IntPtr)WM_LBUTTONDOWN) { //如果左键被点下
btn = Btn.LeftDowm;
} else if (wParam == (IntPtr)WM_RBUTTONUP) { //如果右键被抬起
btn = Btn.RightUp;
}
//好吧 我就不知道当时我怎么想的 在Hook里面获取的坐标 有负数的现象 所以在那边 我没用这个坐标
MouseInfoEventArys e = new MouseInfoEventArys(btn, mouseInfo.pt.x, mouseInfo.pt.y);
HooKMouseEvent(this, e);//触发绑定到这个上面的事件
}
return CallNextHookEx(handleOfHook, nCode, wParam, lParam);//继续下一个钩子
}
public bool SetMouseHook() { //设置Hook
if (isSet) {//如果已经设置了 就不要设置啦、、、
return false;
}
HookProc MouseCallBack = new HookProc(MouseHookProcedure);
handleOfHook = SetWindowsHookEx(WH_MOUSE_LL, MouseCallBack,
GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName), 0);
if (handleOfHook != 0) {//如果设置成功、、
gc = GCHandle.Alloc(MouseCallBack);//这个就是那个什么什么、、然后我的回调就不会被收废品的拣去了
isSet = true;
return true;
}
return false;
}
public bool UnLoadMouseHook() {
if (!isSet) {//如果装都没有装那么久不要卸载啦、、
return false;
}
if (UnhookWindowsHookEx(handleOfHook)) {
gc.Free();//将回调释放掉、、
isSet = false;
return true;
}
return false;
} } public enum Btn//我只感觉到这三个有用、(应该是两个 左键点下 右键抬起)
{
LeftDowm, RightUp, None
}
public class MouseInfoEventArys {//话说定义事件的时候都是这么写的 所以我也弄一个内出来保存事件参数
private int x;//坐标 多余的后来才发现 鼠标慢慢贴近屏幕边缘的时候 3 2 1 0 -1 、、丫的 负数都出来了
public int X {
get { return x; }
} private int y;//坐标
public int Y {
get { return y; }
} private Btn mBtn;
public Btn MBtn {
get { return mBtn; }//鼠标的情况
}
public MouseInfoEventArys(Btn btn,int x,int y) {//构造器
mBtn = btn;
this.x = x;
this.y = y;
} }
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;using System.Threading;
using System.Runtime.InteropServices;namespace AutoDrawRect
{
public partial class Form2 : Form
{
[DllImport("user32.dll")]//获取桌面的句柄
public static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll")]//在桌面找寻子窗体
public static extern IntPtr ChildWindowFromPointEx(IntPtr pHwnd, Point pt, uint uFlgs);
private const int CWP_SKIPDISABLED = 0x2; //忽略不可用窗体
private const int CWP_SKIPINVISIBL = 0x1; //忽略隐藏的窗体
private const int CWP_All = 0x0; //一个都不忽略
[DllImport("user32.dll")]//获得句柄对象的位置
public static extern bool GetWindowRect(IntPtr hWnd, out LPRECT lpRect);
public struct LPRECT {//位置信息的结构体
public int left;
public int top;
public int right;
public int bottom;
} [DllImport("user32.dll")]//进行坐标转换 (再窗体内部进行查找)
public static extern bool ScreenToClient(IntPtr hWnd, out LPPOINT lpPoint);
public struct LPPOINT {//要转换的坐标信息的结构体
public int x;
public int y;
} public Form2() {
InitializeComponent();
this.FormBorderStyle = FormBorderStyle.None;
this.Location = new Point(0, 0);
this.Size = new Size(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height);
pictureBox1.Dock = DockStyle.Fill;
} ~Form2() {//窗体关闭时卸载Hook
mHook.UnLoadMouseHook();
} Bitmap screenBmp; //保存全屏的图像
bool isDrawed; //是否已经画出了一个区域
bool isDraw; //是否允许绘制
IntPtr hWnd; //保存相应对象的句柄
int sx, sy; //鼠标点下时的鼠标坐标
MouseHook mHook = new MouseHook();//创建一个Hook
private void Form2_Load(object sender, EventArgs e) {
screenBmp = GetScreen();
pictureBox1.Image = GetScreen();
using (Graphics g = Graphics.FromImage(pictureBox1.Image)) {
SolidBrush sb = new SolidBrush(Color.FromArgb(125, 0, 0, 0));
g.FillRectangle(sb, 0, 0, this.Width, this.Height);
sb.Dispose();
}
this.Enabled = false; //禁用此窗体(这样在查找的时候才能把当前窗体忽略)
mHook.HooKMouseEvent +=new MouseHook.MEventhandler(mHook_HooKMouseEvent);
mHook.SetMouseHook(); //启用Hook
} public Bitmap GetScreen() {//获得全屏图像
Bitmap bmp = new Bitmap(this.Width, this.Height);
using (Graphics g = Graphics.FromImage(bmp)) {
g.CopyFromScreen(0, 0, 0, 0, this.Size);
}
return bmp;
}
private void mHook_HooKMouseEvent(object sender,MouseInfoEventArys e) {//绑定到Hook上面的事件
if (e.MBtn == Btn.LeftDowm) {//判断左键是否点下
this.Enabled = true; //点下的时候恢复窗体的禁用
sx = MousePosition.X; //记录下当前鼠标位置
sy = MousePosition.Y;
DrawRect("Normal"); //然后把当前区域绘制出来
isDrawed = true; //已经绘制
isDraw = true; //允许绘制
return;//返回
} else if(e.MBtn == Btn.RightUp) {//如果右键抬起
if (isDrawed) { //判断是否已经绘制 已经绘制取消绘制
hWnd = IntPtr.Zero; //句柄清空 重新找寻
isDrawed = false; //没有绘制
this.Enabled = false; //继续禁用窗体 (查找时忽略禁用的窗体)
} else {
this.Enabled = true; //如果没有绘制 那关闭窗体 先恢复窗体
new Thread(new ThreadStart(ThreadCloseForm)).Start();
/*起一个线程来关闭窗体 目的是为了让当前窗体 接受到这个右键信息
不然这个现在关闭了 那么这个右键信息就传递到下面去了
*/
}
}
if (isDrawed) { //如果已经绘制了 就不用继续自动框选区域了
return;
}
FoundRectToDraw(); //找寻框选区域并且绘制
}
public void FoundRectToDraw() {
//在桌面根据鼠标位置去找寻鼠标下面的控件句柄
IntPtr thWnd = ChildWindowFromPointEx(GetDesktopWindow(),
MousePosition, CWP_SKIPDISABLED | CWP_SKIPINVISIBL);//忽略桌面上隐藏或者禁用的窗体
//thWnd是找到的桌面的窗体的句柄 再次 根据鼠标坐标在此窗体里面找寻控件
LPPOINT lp = new LPPOINT(); //注意这里的鼠标不是屏幕坐标 而是 窗体内部坐标
lp.x = MousePosition.X; lp.y = MousePosition.Y;
ScreenToClient(thWnd, out lp); //所以把屏幕坐标转换为客户区内部坐标
//再次用刚才的thWnd句柄去找寻 这次一个都不忽略
IntPtr temphWnd = ChildWindowFromPointEx(thWnd, new Point(lp.x, lp.y),CWP_All);
//判断这个句柄是否为空
if (temphWnd == IntPtr.Zero) { //如果没有找到 就用刚才那个句柄(这里我只查找了两层)
temphWnd = thWnd;
}
if (temphWnd == hWnd) { //如果这个句柄 和 上一个区域的句柄没有变化 那么久返回吧
return;
} else {
hWnd = temphWnd; //不然就把这个新找到的句柄赋值给它
}
DrawRect("Normal"); //然后在这个新的句柄外面绘制一圈
}
public void DrawRect(string type) {//绘制区域
using (Graphics g = pictureBox1.CreateGraphics()) {//建立画布
using (Pen p = new Pen(Color.FromArgb(255, 24, 219, 255), 2)) {//画笔
pictureBox1.Refresh(); //绘制前先清空画布
switch (type) {
case "Normal"://绘制找到的区域
LPRECT rect = new LPRECT();
GetWindowRect(hWnd, out rect);
g.DrawRectangle(p, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
Rectangle drawRect = new Rectangle(new Point(rect.left, rect.top),
new Size(rect.right - rect.left, rect.bottom - rect.top));
g.DrawImage(screenBmp, drawRect, drawRect, GraphicsUnit.Pixel);
break;
case "Rectangle"://鼠标点下并且拖动的时候 绘制矩形(也就是自定义区域)
g.DrawRectangle(p, sx, sy, MousePosition.X - sx + 1, MousePosition.Y - sy + 1);
break;
case "MouseUp"://鼠标抬起的时候 把相应区域的图片也画上去(正常的不是黑色的图)
Rectangle imgRect = new Rectangle(new Point(sx, sy),
new Size(MousePosition.X - sx + 1, MousePosition.Y - sy + 1));
g.DrawRectangle(p, sx, sy, MousePosition.X - sx + 1, MousePosition.Y - sy + 1);
g.DrawImage(screenBmp, imgRect, imgRect, GraphicsUnit.Pixel);
break;
}
}
}
}
public delegate void CloseFormDel();//创建委托 因为要跨线程操作
//Form.CheckForIllegalCrossThreadCalls = false;这样也可以就可以直接跨线程操作了 不过 话说不好
public void ThreadCloseForm() {
Thread.Sleep(100);//暂停100毫秒再关闭窗体 (让改窗体接受到鼠标)
try {//好吧 我承认 这里我也不知道为什么 有时候提示改 不能再该句柄创建前调用Invoke(我很郁闷我的窗体不是创建了么)
this.Invoke(new CloseFormDel(CloseForm));
} catch {
this.Close();
}
}
public void CloseForm() {//关闭窗体
this.Close();
} private void pictureBox1_MouseMove(object sender, MouseEventArgs e) {
//判断鼠标是否点下 如果点下没有松开 那么移动的时候就绘制区域
if (isDraw) {
//因为窗体在恢复禁用的时候就会马上出发Move事件 所以判断一下 如果鼠标在点下的时候 位置没用动那么什么都不做
if (sx == MousePosition.X && sy == MousePosition.Y) {
return;
}
DrawRect("Rectangle");//还有自己判断 反方向拖动鼠标的情况 - -!、、这里就不写了 代码太多看着麻烦
}
} private void pictureBox1_MouseUp(object sender, MouseEventArgs e) {
isDraw = false;//取消鼠标移动的绘制
//因为第一次点下鼠标 默认是 绘制自动框选的区域 所以 坐标没有变的话 什么都不做
if (sx == MousePosition.X && sy == MousePosition.Y) {
return;
}
DrawRect("MouseUp");
}
}
}
没有LZ那么复杂。
http://blog.csdn.net/linjimu/article/details/2942379