Thứ Tư, 1 tháng 4, 2009

Thiết lập Windows Hook trong VC#.NET

Bài viết này sẽ hướng dẫn cho bạn cách làm thế nào để thiết lập cách móc nối chuột và tới sự kiện chuột trên màn hình. Cách thực hiện gồm các bước như sau: Để thiết lập một móc nối ta gọi tới hàm SetWindowsHookEx từ User32.dll . Hàm này thiết đặt một...

Bài viết này sẽ hướng dẫn cho bạn cách làm thế nào để thiết lập cách móc nối chuột và tới sự kiện chuột trên màn hình. Cách thực hiện gồm các bước như sau:

Để thiết lập một móc nối ta gọi tới hàm SetWindowsHookEx từ User32.dll. Hàm này thiết đặt một thủ tục móc được định nghĩa bởi ứng dụng trong hệ thống móc nối.

Bước 1:

Khởi động Microsoft Visual Studio .NET

Bước 2:

Chọn File từ menu, chọn New, và click Project.

Bước 3:

Trong New Project , click Visual C# Project và chọn Windown Application .

Bước 4:

Sau đó thêm dòng sau vào Form1.cs
  1. using System.Runtime.InteropServices;

Bước 5:

Thêm mã sau đây trong lớp Form1
  1. public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);
  2. //Declare the hook handle as an int.
  3. static int hHook = 0;
  4. //Declare the mouse hook constant.
  5. //For other hook types, you can obtain these values from Winuser.h in the Microsoft SDK.
  6. public const int WH_MOUSE = 7;
  7. private System.Windows.Forms.Button button1;
  8. //Declare MouseHookProcedure as a HookProc type.
  9. HookProc MouseHookProcedure;
  10. //Declare the wrapper managed POINT class.
  11. [StructLayout(LayoutKind.Sequential)]
  12. public class POINT
  13. {
  14. public int x;
  15. public int y;
  16. }
  17. //Declare the wrapper managed MouseHookStruct class.
  18. [StructLayout(LayoutKind.Sequential)]
  19. public class MouseHookStruct
  20. {
  21. public POINT pt;
  22. public int hwnd;
  23. public int wHitTestCode;
  24. public int dwExtraInfo;
  25. }
  26. //This is the Import for the SetWindowsHookEx function.
  27. //Use this function to install a thread-specific hook.
  28. [DllImport("user32.dll",CharSet=CharSet.Auto,
  29. CallingConvention=CallingConvention.StdCall)]
  30. public static extern int SetWindowsHookEx(int idHook, HookProc lpfn,
  31. IntPtr hInstance, int threadId);
  32. //This is the Import for the UnhookWindowsHookEx function.
  33. //Call this function to uninstall the hook.
  34. [DllImport("user32.dll",CharSet=CharSet.Auto,
  35. CallingConvention=CallingConvention.StdCall)]
  36. public static extern bool UnhookWindowsHookEx(int idHook);
  37. //This is the Import for the CallNextHookEx function.
  38. //Use this function to pass the hook information to the next hook procedure in chain.
  39. [DllImport("user32.dll",CharSet=CharSet.Auto,
  40. CallingConvention=CallingConvention.StdCall)]
  41. public static extern int CallNextHookEx(int idHook, int nCode,
  42. IntPtr wParam, IntPtr lParam);

Bước 6:

Kéo thêm một button lên form và thêm mã sau đây trong thủ tục Button1_click
  1. private void button1_Click(object sender, System.EventArgs e)
  2. {
  3. if(hHook == 0)
  4. {
  5. // Create an instance of HookProc.
  6. MouseHookProcedure = new HookProc(Form1.MouseHookProc);
  7. hHook = SetWindowsHookEx(WH_MOUSE,
  8. MouseHookProcedure,
  9. (IntPtr)0,
  10. AppDomain.GetCurrentThreadId());
  11. //If the SetWindowsHookEx function fails.
  12. if(hHook == 0 )
  13. {
  14. MessageBox.Show("SetWindowsHookEx Failed");
  15. return;
  16. }
  17. button1.Text = "UnHook Windows Hook";
  18. }
  19. else
  20. {
  21. bool ret = UnhookWindowsHookEx(hHook);
  22. //If the UnhookWindowsHookEx function fails.
  23. if(ret == false )
  24. {
  25. MessageBox.Show("UnhookWindowsHookEx Failed");
  26. return;
  27. }
  28. hHook = 0;
  29. button1.Text = "Set Windows Hook";
  30. this.Text = "Mouse Hook";
  31. }
  32. }

Bước 7:

Thêm mã sau cho hàm MouseHookProc trong class Form1
  1. public static int MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam)
  2. {
  3. //Marshall the data from the callback.
  4. MouseHookStruct MyMouseHookStruct = (MouseHookStruct) Marshal.PtrToStructure(lParam, typeof(MouseHookStruct));
  5. if (nCode <>
  6. {
  7. return CallNextHookEx(hHook, nCode, wParam, lParam);
  8. }
  9. else
  10. {
  11. //Create a string variable that shows the current mouse coordinates.
  12. String strCaption = "x = " +
  13. MyMouseHookStruct.pt.x.ToString("d") + " y = " +
  14. MyMouseHookStruct.pt.y.ToString("d");
  15. //You must get the active form because it is a static function.
  16. Form tempForm = Form.ActiveForm;
  17. //Set the caption of the form.
  18. tempForm.Text = strCaption;
  19. return CallNextHookEx(hHook, nCode, wParam, lParam);
  20. }
  21. }