六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 174|回复: 0

C#利用钩子控制鼠标【月儿原创】

[复制链接]

升级  38.7%

580

主题

580

主题

580

主题

探花

Rank: 6Rank: 6

积分
1774
 楼主| 发表于 2013-1-27 04:34:59 | 显示全部楼层 |阅读模式
C#利用钩子控制鼠标
作者:清清月儿
主页:http://blog.csdn.net/21aspnet/           时间:2007.5.11
工作中有这样的需求,某个控件panel的子控件textbox要实现只留鼠标右键copy,注意同时还不能影响其它panel的子控件textbox,怎么办?
答案是只有用钩子,在codeporject上找到这么一个钩子。
如图所示,第一个文本框只有copy功能。


UserActivityHook.cs
<strong />

<div style="padding-right: 5.4pt; padding-left: 5.4pt; background: #e6e6e6; padding-bottom: 4px; width: 95%; padding-top: 4px;">using System;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Threading;
using System.Windows.Forms;
using System.ComponentModel;

namespace gma.System.Windows
...{
 
/**//// <summary>
 
/// This class allows you to tap keyboard and mouse and / or to detect their activity even when an 
 
/// application runes in background or does not have any user interface at all. This class raises 
 
/// common .NET events with KeyEventArgs and MouseEventArgs so you can easily retrieve any information you need.
 
/// </summary>
 public class UserActivityHook
 
...{
  
Windows structure definitions<span style="display: none;">#region Windows structure definitions

  
/**//// <summary>
  
/// The POINT structure defines the x- and y- coordinates of a point. 
  
/// </summary>
  
/// <remarks>
  
/// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/rectangl_0tiq.asp
  
/// </remarks>
  [StructLayout(LayoutKind.Sequential)]
   
private class POINT
  
...{
   
/**//// <summary>
   
/// Specifies the x-coordinate of the point. 
   
/// </summary>
   public int x;
   
/**//// <summary>
   
/// Specifies the y-coordinate of the point. 
   
/// </summary>
   public int y;
  }


  
/**//// <summary>
  
/// The MOUSEHOOKSTRUCT structure contains information about a mouse event passed to a WH_MOUSE hook procedure, MouseProc. 
  
/// </summary>
  
/// <remarks>
  
/// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/hooks/hookreference/hookstructures/cwpstruct.asp
  
/// </remarks>
  [StructLayout(LayoutKind.Sequential)]
   
private class MouseHookStruct
  
...{
   
/**//// <summary>
   
/// Specifies a POINT structure that contains the x- and y-coordinates of the cursor, in screen coordinates. 
   
/// </summary>
   public POINT pt;
   
/**//// <summary>
   
/// Handle to the window that will receive the mouse message corresponding to the mouse event. 
   
/// </summary>
   public int hwnd;
   
/**//// <summary>
   
/// Specifies the hit-test value. For a list of hit-test values, see the description of the WM_NCHITTEST message. 
   
/// </summary>
   public int wHitTestCode;
   
/**//// <summary>
   
/// Specifies extra information associated with the message. 
   
/// </summary>
   public int dwExtraInfo;
  }


  
/**//// <summary>
  
/// The MSLLHOOKSTRUCT structure contains information about a low-level keyboard input event. 
  
/// </summary>
  [StructLayout(LayoutKind.Sequential)]
   
private class MouseLLHookStruct
  
...{
   
/**//// <summary>
   
/// Specifies a POINT structure that contains the x- and y-coordinates of the cursor, in screen coordinates. 
   
/// </summary>
   public POINT pt;
   
/**//// <summary>
   
/// If the message is WM_MOUSEWHEEL, the high-order word of this member is the wheel delta. 
   
/// The low-order word is reserved. A positive value indicates that the wheel was rotated forward, 
   
/// away from the user; a negative value indicates that the wheel was rotated backward, toward the user. 
   
/// One wheel click is defined as WHEEL_DELTA, which is 120. 
   
///If the message is WM_XBUTTONDOWN, WM_XBUTTONUP, WM_XBUTTONDBLCLK, WM_NCXBUTTONDOWN, WM_NCXBUTTONUP,
   
/// or WM_NCXBUTTONDBLCLK, the high-order word specifies which X button was pressed or released, 
   
/// and the low-order word is reserved. This value can be one or more of the following values. Otherwise, mouseData is not used. 
   
///XBUTTON1
   
///The first X button was pressed or released.
   
///XBUTTON2
   
///The second X button was pressed or released.
   
/// </summary>
   public int mouseData;
   
/**//// <summary>
   
/// Specifies the event-injected flag. An application can use the following value to test the mouse flags. Value Purpose 
   
///LLMHF_INJECTED Test the event-injected flag.  
   
///0
   
///Specifies whether the event was injected. The value is 1 if the event was injected; otherwise, it is 0.
   
///1-15
   
///Reserved.
   
/// </summary>
   public int flags;
   
/**//// <summary>
   
/// Specifies the time stamp for this message.
   
/// </summary>
   public int time;
   
/**//// <summary>
   
/// Specifies extra information associated with the message. 
   
/// </summary>
   public int dwExtraInfo;
  }



  
/**//// <summary>
  
/// The KBDLLHOOKSTRUCT structure contains information about a low-level keyboard input event. 
  
/// </summary>
  
/// <remarks>
  
/// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/hooks/hookreference/hookstructures/cwpstruct.asp
  
/// </remarks>
  [StructLayout(LayoutKind.Sequential)]
   
private class KeyboardHookStruct
  
...{
   
/**//// <summary>
   
/// Specifies a virtual-key code. The code must be a value in the range 1 to 254. 
   
/// </summary>
   public int vkCode;
   
/**//// <summary>
   
/// Specifies a hardware scan code for the key. 
   
/// </summary>
   public int scanCode;
   
/**//// <summary>
   
/// Specifies the extended-key flag, event-injected flag, context code, and transition-state flag.
   
/// </summary>
   public int flags;
   
/**//// <summary>
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表