| Cách thiết lập Windows hook trong Visual C# .NET |
|
|
| 03/08/2007 | |
|
Bài viết này mô tả làm sao thiết lập một móc nối đối với một luồng và tới một thủ tục móc bằng cách sử dụng cách móc nối chuột như một ví dụ. Bạn có thể sử dụng những cái móc để theo dõi những kiểu sự kiện nhất định. Bạn có thể liên tưởng những sự kiện này với một luồng đặc biệt hay với mọi luồng trong cùng máy để bàn như một luồng gọi tới, và sau đây là cách thực hiện. Cách thức chi tiết Để 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 làm theo những bước 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. 1. Khởi động Microsoft Visual Studio .NET 2. Chọn FILE từ menu, chọn New, va click Project. 3. Trong New Project , click Visual C# Project va chọn Windown Application . 4. Sau đó add using System.Runtime.InteropServices; vào Form1.cs 5. Thêm mã sau đây trong lớp Form1 public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam); //Declare the hook handle as an int. static int hHook = 0; //Declare the mouse hook constant. //For other hook types, you can obtain these values from Winuser.h in the Microsoft SDK. public const int WH_MOUSE = 7; private System.Windows.Forms.Button button1; //Declare MouseHookProcedure as a HookProc type. HookProc MouseHookProcedure; //Declare the wrapper managed POINT class. [StructLayout(LayoutKind.Sequential)] public class POINT { public int x; public int y; } //Declare the wrapper managed MouseHookStruct class. [StructLayout(LayoutKind.Sequential)] public class MouseHookStruct { public POINT pt; public int hwnd; public int wHitTestCode; public int dwExtraInfo; } //This is the Import for the SetWindowsHookEx function. //Use this function to install a thread-specific hook. [DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)] public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId); //This is the Import for the UnhookWindowsHookEx function. //Call this function to uninstall the hook. [DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)] public static extern bool UnhookWindowsHookEx(int idHook); //This is the Import for the CallNextHookEx function. //Use this function to pass the hook information to the next hook procedure in chain. [DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)] public static extern int CallNextHookEx(int idHook, int nCode, IntPtr wParam, IntPtr lParam); 6. Kéo thêm một button lên form và thêm mã sau đây trong thủ tục Button1_click private void button1_Click(object sender, System.EventArgs e) { if(hHook == 0) { // Create an instance of HookProc. MouseHookProcedure = new HookProc(Form1.MouseHookProc); hHook = SetWindowsHookEx(WH_MOUSE, MouseHookProcedure, (IntPtr)0, AppDomain.GetCurrentThreadId()); //If the SetWindowsHookEx function fails. if(hHook == 0 ) { MessageBox.Show("SetWindowsHookEx Failed"); return; } button1.Text = "UnHook Windows Hook"; } else { bool ret = UnhookWindowsHookEx(hHook); //If the UnhookWindowsHookEx function fails. if(ret == false ) { MessageBox.Show("UnhookWindowsHookEx Failed"); return; } hHook = 0; button1.Text = "Set Windows Hook"; this.Text = "Mouse Hook"; } } 7. Thêm mã sau cho hàm MouseHookProc trong class Form1 public static int MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam) { //Marshall the data from the callback. MouseHookStruct MyMouseHookStruct = (MouseHookStruct) Marshal.PtrToStructure(lParam, typeof(MouseHookStruct)); if (nCode < 0) { return CallNextHookEx(hHook, nCode, wParam, lParam); } else { //Create a string variable that shows the current mouse coordinates. String strCaption = "x = " + MyMouseHookStruct.pt.x.ToString("d") + " y = " + MyMouseHookStruct.pt.y.ToString("d"); //You must get the active form because it is a static function. Form tempForm = Form.ActiveForm; //Set the caption of the form. tempForm.Text = strCaption; return CallNextHookEx(hHook, nCode, wParam, lParam); } } 8. Nhấn F5 để chạy chương trình . Kích vào button trên form để đặt móc nối. Những tọa độ chuột xuất hiện trên thanh chắn đấu form khi con trỏ di chuyển trên form. Kích button lần nữa để loại bỏ móc nối... |
| < Trước | Tiếp > |
|---|
| Hôm đó, Hỏa tinh sẽ nằm gần như trên cùng một đường thẳng nối mặt trời và trái đất, đồng thời khoảng cách từ nó đến chúng ta cũng là gần nhất trong một chu kỳ kéo dài gần 60.000 năm. | |
| Đọc tiếp... |
Tin công nghệ, thông tin cập nhật liên tục trong ngày từ các website tin tức & khoa học trong và ngoài nước