Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report