程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 用鉤子(hook)實現C#的屏幕鍵盤效果(4)

用鉤子(hook)實現C#的屏幕鍵盤效果(4)

編輯:關於C語言

使用完鉤子後,要進行卸載,這個可以寫在析構函數中。

1
2  public void Stop() {
3    this.Stop(true, true, true);
4  }
5  
6  public void Stop(bool uninstallMouseHook, bool uninstallKeyboardHook,
7    bool throwExceptions) {
8    // if mouse hook set and must be uninstalled
9    if (hMouseHook != IntPtr.Zero && uninstallMouseHook) {
10      // uninstall hook
11      bool retMouse = UnhookWindowsHookEx(hMouseHook);
12      // reset invalid handle
13      hMouseHook = IntPtr.Zero;
14      // if failed and exception must be thrown
15      if (retMouse == false && throwExceptions) {
16        // Returns the error code returned by the last unmanaged function
17        // called using platform invoke that has the DllImportAttribute.
18        // SetLastError flag set.
19        int errorCode = Marshal.GetLastWin32Error();
20        // Initializes and throws a new instance of the Win32Exception class
21        // with the specifIEd error.
22        throw new Win32Exception(errorCode);
23      }
24    }
25
26    // if keyboard hook set and must be uninstalled
27    if (hKeyboardHook != IntPtr.Zero && uninstallKeyboardHook) {
28      // uninstall hook
29      bool retKeyboard = UnhookWindowsHookEx(hKeyboardHook);
30      // reset invalid handle
31      hKeyboardHook = IntPtr.Zero;
32      // if failed and exception must be thrown
33      if (retKeyboard == false && throwExceptions) {
34        // Returns the error code returned by the last unmanaged function
35        // called using platform invoke that has the DllImportAttribute.
36        // SetLastError flag set.
37        int errorCode = Marshal.GetLastWin32Error();
38        // Initializes and throws a new instance of the Win32Exception class
39        // with the specifIEd error.
40        throw new Win32Exception(errorCode);
41      }
42    }
43  }
44

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved