| 发表于:2007-01-05 12:10:15 楼主 |
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.text; using system.windows.forms; using system.runtime.interopservices; using system.reflection; namespace a4 { public partial class form1 : form { private const int wm_mousemove = 0x200; //鼠标移动 private const int wm_lbuttondown = 0x201; //鼠标左键按下 private const int wm_rbuttondown = 0x204; //鼠标右键按下 private const int wm_mbuttondown = 0x207; //鼠标滚轴按下 private const int wm_lbuttonup = 0x202; //鼠标左键抬起 private const int wm_rbuttonup = 0x205; //鼠标右键抬起 private const int wm_mbuttonup = 0x208; //鼠标滚轴抬起 private const int wm_lbuttondblclk = 0x203; //鼠标左键双击 private const int wm_rbuttondblclk = 0x206; //鼠标右键双击 private const int wm_mbuttondblclk = 0x209; //鼠标滚轴双击 [structlayout(layoutkind.sequential)] //声明一个封送类型 public class mousehookstruct { public point pt; public int hwnd; public int whittestcode; public int dwextrainfo; } int hhook=0; private delegate int mouse_key_delegate( int ncode,int wparam,intptr lparam); [dllimport( "kernel32.dll " , charset = charset.auto , callingconvention = callingconvention.stdcall, setlasterror = true , entrypoint = "getcurrentthreadid ")] private static extern int getcurrentthreadid(); [dllimport( "user32.dll ", charset = charset.auto , callingconvention = callingconvention.stdcall, setlasterror = true , entrypoint = "setwindowshookexa ")] private static extern int setwindowshookex(int hhook, mouse_key_delegate mkdelegate, intptr wparam, int lparam); [dllimport( "user32.dll " , charset = charset.auto, callingconvention = callingconvention.stdcall, setlasterror = true , entrypoint = "unhookwindowshookex ")] private static extern bool unhookwindowshookex(int hhook); [dllimport( "user32.dll ", charset = charset.auto, callingconvention = callingconvention.stdcall, setlasterror = true , entrypoint = "callnexthookex ")] private static extern int callnexthookex(int hhook, int ncode, int wparam, intptr lparam); public form1() { initializecomponent(); } public form1(int hhook) { this.hhook = hhook; this.initializecomponent(); } private int mouse_key_proc(int ncode, int wparam, intptr lparam) { mousehookstruct mymousehookstruct = (mousehookstruct)marshal.ptrtostructure(lparam, typeof(mousehookstruct)); if (ncode > 0 && wparam == wm_lbuttondown) { this.toolstripstatuslabel1.text = " x= " + mymousehookstruct.pt.x + ", y= " + mymousehookstruct.pt.y; return 1; } return callnexthookex(hhook, ncode, wparam, lparam); } private bool start() { //hhook = setwindowshookex(hhook, new mouse_key_delegate(mouse_key_proc), intptr.zero, getcurrentthreadid());//hhook=2好用 hhook = setwindowshookex(hhook, new mouse_key_delegate(mouse_key_proc), marshal.gethinstance(assembly.getEXECutingassembly().getmodules()[0]), 0);//这句为什么不好用 messagebox.show( "hhook= " + hhook); if (hhook != 0) return true; return false; } private bool stop() { if (hhook == 0) { unhookwindowshookex(hhook); return true; } return false; } private void form1_load(object sender, eventargs e) { hhook = 14; start(); } } } 请帮我看看全局钩子为什么不好用?????????? |
|
|
|
|