C#利用钩子控制鼠标【月儿原创】
C#利用钩子控制鼠标作者:清清月儿
主页:http://blog.csdn.net/21aspnet/ 时间:2007.5.11
工作中有这样的需求,某个控件panel的子控件textbox要实现只留鼠标右键copy,注意同时还不能影响其它panel的子控件textbox,怎么办?
答案是只有用钩子,在codeporject上找到这么一个钩子。
如图所示,第一个文本框只有copy功能。
http://p.blog.csdn.net/images/p_blog_csdn_net/21aspnet/right.GIF
UserActivityHook.cs
<strong />
<div style="padding-right: 5.4pt; padding-left: 5.4pt; background: #e6e6e6; padding-bottom: 4px; width: 95%; padding-top: 4px;">http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifusing System;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifusing System.Runtime.InteropServices;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifusing System.Reflection;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifusing System.Threading;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifusing System.Windows.Forms;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifusing System.ComponentModel;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifnamespace gma.System.Windows
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif /**//// <summary>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif /// This class allows you to tap keyboard and mouse and / or to detect their activity even when an
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif /// application runes in background or does not have any user interface at all. This class raises
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif /// common .NET events with KeyEventArgs and MouseEventArgs so you can easily retrieve any information you need.
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif /// </summary>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif public class UserActivityHook
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif Windows structure definitions<span style="display: none;">#region Windows structure definitions
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif /**//// <summary>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif /// The POINT structure defines the x- and y- coordinates of a point.
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif /// </summary>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif /// <remarks>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif /// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/rectangl_0tiq.asp
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif /// </remarks>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif private class POINT
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif /**//// <summary>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif /// Specifies the x-coordinate of the point.
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif /// </summary>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif public int x;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif /**//// <summary>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif /// Specifies the y-coordinate of the point.
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif /// </summary>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif public int y;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif /**//// <summary>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif /// The MOUSEHOOKSTRUCT structure contains information about a mouse event passed to a WH_MOUSE hook procedure, MouseProc.
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif /// </summary>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif /// <remarks>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif /// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/hooks/hookreference/hookstructures/cwpstruct.asp
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif /// </remarks>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif private class MouseHookStruct
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif /**//// <summary>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif /// Specifies a POINT structure that contains the x- and y-coordinates of the cursor, in screen coordinates.
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif /// </summary>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif public POINT pt;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif /**//// <summary>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif /// Handle to the window that will receive the mouse message corresponding to the mouse event.
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif /// </summary>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif public int hwnd;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif /**//// <summary>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif /// Specifies the hit-test value. For a list of hit-test values, see the description of the WM_NCHITTEST message.
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif /// </summary>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif public int wHitTestCode;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif /**//// <summary>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif /// Specifies extra information associated with the message.
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif /// </summary>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif public int dwExtraInfo;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif /**//// <summary>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif /// The MSLLHOOKSTRUCT structure contains information about a low-level keyboard input event.
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif /// </summary>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif private class MouseLLHookStruct
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif /**//// <summary>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif /// Specifies a POINT structure that contains the x- and y-coordinates of the cursor, in screen coordinates.
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif /// </summary>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif public POINT pt;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif /**//// <summary>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif /// If the message is WM_MOUSEWHEEL, the high-order word of this member is the wheel delta.
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif /// The low-order word is reserved. A positive value indicates that the wheel was rotated forward,
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif /// away from the user; a negative value indicates that the wheel was rotated backward, toward the user.
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif /// One wheel click is defined as WHEEL_DELTA, which is 120.
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif ///If the message is WM_XBUTTONDOWN, WM_XBUTTONUP, WM_XBUTTONDBLCLK, WM_NCXBUTTONDOWN, WM_NCXBUTTONUP,
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif /// or WM_NCXBUTTONDBLCLK, the high-order word specifies which X button was pressed or released,
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif /// and the low-order word is reserved. This value can be one or more of the following values. Otherwise, mouseData is not used.
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif ///XBUTTON1
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif ///The first X button was pressed or released.
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif ///XBUTTON2
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif ///The second X button was pressed or released.
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif /// </summary>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif public int mouseData;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif /**//// <summary>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif /// Specifies the event-injected flag. An application can use the following value to test the mouse flags. Value Purpose
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif ///LLMHF_INJECTED Test the event-injected flag.
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif ///0
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif ///Specifies whether the event was injected. The value is 1 if the event was injected; otherwise, it is 0.
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif ///1-15
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif ///Reserved.
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif /// </summary>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif public int flags;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif /**//// <summary>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif /// Specifies the time stamp for this message.
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif /// </summary>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif public int time;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif /**//// <summary>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif /// Specifies extra information associated with the message.
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif /// </summary>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif public int dwExtraInfo;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif /**//// <summary>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif /// The KBDLLHOOKSTRUCT structure contains information about a low-level keyboard input event.
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif /// </summary>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif /// <remarks>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif /// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/hooks/hookreference/hookstructures/cwpstruct.asp
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif /// </remarks>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif private class KeyboardHookStruct
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif /**//// <summary>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif /// Specifies a virtual-key code. The code must be a value in the range 1 to 254.
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif /// </summary>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif public int vkCode;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif /**//// <summary>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif /// Specifies a hardware scan code for the key.
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif /// </summary>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif public int scanCode;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif /**//// <summary>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif /// Specifies the extended-key flag, event-injected flag, context code, and transition-state flag.
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif /// </summary>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif public int flags;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif /**//// <summary>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
页:
[1]