Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

The C # mouse hook takes you out of the area restrictions of Form

Shulou Source: shulou.com Published: 2022-06-03 04:11:19 10月03日 Update

Remember the use of mouse hooks

Inertia, I haven't written for a long time, but today this thing is too important, so I am determined to write it down. You can take a look at it later.

Mouse hook (MouseHock), a function that is rarely used, occasionally avoids parallel imports because of various interfaces involving the bottom of the operating system. Then again, interest is the best teacher. This time I Get this skill, but because this function is too powerful, it belongs to unmanaged resources. Please release the hook immediately after use. Otherwise, the things sent by your QQ enter can be transmitted to the XXXAPI you debugged.

As the name implies, around the operation of the mouse, attach the relevant Intptra value of the mouse in advance:

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; hexadecimal value, note conversion between decimal and decimal

Hook source code on the Internet a lot of do not knock, the process of use is briefly described:

Create object

Install hook

Bind event

Uninstall hooks (release resources)

There is a code in the hook that says the hole that exists when you use it:

Private int MouseHookProc (int nCode, IntPtr wParam, IntPtr lParam)

{

Win32Api.MouseHookStruct MyMouseHookStruct = (Win32Api.MouseHookStruct) Marshal.PtrToStructure (lParam, typeof (Win32Api.MouseHookStruct))

If (nCode < 0)

{

Return Win32Api.CallNextHookEx (hHook, nCode, wParam, lParam)

}

Else

{

/ / determine whether it is a mouse click to evade the mouse movement function

If (wParam.ToString ("X2") = = "2011" | | wParam.ToString (" X2 ") = =" 204")

{

MouseButtons button = MouseButtons.Left

Var e = new MouseEventArgs (button, 1, point.X, point.Y, 0)

MouseClickEvent?.Invoke (this, e)

This.Point = new Point (MyMouseHookStruct.pt.x, MyMouseHookStruct.pt.y)

/ / return Win32Api.CallNextHookEx (hHook, nCode, wParam, lParam)

}

Return default (int)

}

}

Note: the marking area is because my task requires the mouse to perform a click operation, rather than a stereotyped transmission of all the operations of the hook grab, on the one hand, the resource overhead is high, on the other hand, it is really not needed, the place to judge is very Low, directly compare the attached hexadecimal value must pay attention to release the hook!

Tags: Hook mouse thing resource party function hexadecimal region powerful important stereotyped as the name implies operating system inter-event code task interest determination function Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Shulou Technology MySQL Huawei Xiaomi