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

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

編輯:關於C語言

(2)WH_MOUSE_LL Hook

WH_MOUSE_LL Hook監視輸入到線程消息隊列中的鼠標消息。

下面的 class 把 API 調用封裝起來以便調用。

1 // NativeMethods.cs
2 using system;
3 using system.Runtime.InteropServices;
4 using system.Drawing;
5
6 namespace CnBlogs.Youzai.ScreenKeyboard {
7  [StructLayout(LayoutKind.Sequential)]
8  internal struct MOUSEINPUT {
9    public int dx;
10    public int dy;
11    public int mouseData;
12    public int dwFlags;
13    public int time;
14    public IntPtr dwExtraInfo;
15  }
16
17  [StructLayout(LayoutKind.Sequential)]
18  internal struct KEYBDINPUT {
19    public short wVk;
20    public short wScan;
21    public int dwFlags;
22    public int time;
23    public IntPtr dwExtraInfo;
24  }
25
26  [StructLayout(LayoutKind.Explicit)]
27  internal struct Input {
28    [FIEldOffset(0)]
29    public int type;
30    [FIEldOffset(4)]
31    public MOUSEINPUT mi;
32    [FIEldOffset(4)]
33    public KEYBDINPUT ki;
34    [FIEldOffset(4)]
35    public HARDWAREINPUT hi;
36  }
37
38  [StructLayout(LayoutKind.Sequential)]
39  internal struct HARDWAREINPUT {
40    public int uMsg;
41    public short wParamL;
42    public short wParamH;
43  }
44
45  internal class INPUT {
46    public const int MOUSE = 0;
47    public const int KEYBOARD = 1;
48    public const int HARDWARE = 2;
49  }
50
51  internal static class NativeMethods {
52    [DllImport("User32.dll", CharSet = CharSet.Auto, SetLastError = false)]
53    internal static extern IntPtr GetWindowLong(IntPtr hWnd, int nIndex);
54
55    [DllImport("User32.dll", CharSet = CharSet.Auto, SetLastError = false)]
56    internal static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
57
58    [DllImport("User32.dll", EntryPoint = "SendInput", CharSet = CharSet.Auto)]
59    internal static extern UInt32 SendInput(UInt32 nInputs, Input[] pInputs, Int32 cbSize);
60
61    [DllImport("Kernel32.dll", EntryPoint = "GetTickCount", CharSet = CharSet.Auto)]
62    internal static extern int GetTickCount();
63
64    [DllImport("User32.dll", EntryPoint = "GetKeyState", CharSet = CharSet.Auto)]
65    internal static extern short GetKeyState(int nVirtKey);
66
67    [DllImport("User32.dll", EntryPoint = "SendMessage", CharSet = CharSet.Auto)]
68    internal static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam,
  
IntPtr lParam);
69  }
70}

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