C 利用鉤子控制滑鼠【月兒原創】
C#利用鉤子控制滑鼠
作者:清清月兒
主頁:http://blog.csdn.net/21aspnet/ 時間:2007.5.11
工作中有這樣的需求,某個控制元件panel的子控制元件textbox要實現只留滑鼠右鍵copy,注意同時還不能影響其它panel的子控制元件textbox,怎麼辦?
答案是隻有用鉤子,在codeporject上找到這麼一個鉤子。
如圖所示,第一個文字框只有copy功能。
UserActivityHook.cs
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
/// </summary>
public class UserActivityHook
...{
Windows structure definitions#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>
/// Specifies the time stamp for this message.
/// </summary>
public int time;
/**//// <summary>
/// Specifies extra information associated with the message.
/// </summary>
public int dwExtraInfo;
}
#endregion
Windows function imports#region Windows function imports
/**//// <summary>
/// The SetWindowsHookEx function installs an application-defined hook procedure into a hook chain.
/// You would install a hook procedure to monitor the system for certain types of events. These events
/// are associated either with a specific thread or with all threads in the same desktop as the calling thread.
/// </summary>
/// <param name="idHook">
/// [in] Specifies the type of hook procedure to be installed. This parameter can be one of the following values.
/// </param>
/// <param name="lpfn">
/// [in] Pointer to the hook procedure. If the dwThreadId parameter is zero or specifies the identifier of a
/// thread created by a different process, the lpfn parameter must point to a hook procedure in a dynamic-link
/// library (DLL). Otherwise, lpfn can point to a hook procedure in the code associated with the current process.
/// </param>
/// <param name="hMod">
/// [in] Handle to the DLL containing the hook procedure pointed to by the lpfn parameter.
/// The hMod parameter must be set to NULL if the dwThreadId parameter specifies a thread created by
/// the current process and if the hook procedure is within the code associated with the current process.
/// </param>
/// <param name="dwThreadId">
/// [in] Specifies the identifier of the thread with which the hook procedure is to be associated.
/// If this parameter is zero, the hook procedure is associated with all existing threads running in the
/// same desktop as the calling thread.
/// </param>
/// <returns>
/// If the function succeeds, the return value is the handle to the hook procedure.
/// If the function fails, the return value is NULL. To get extended error information, call GetLastError.
/// </returns>
/// <remarks>
/// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/hooks/hookreference/hookfunctions/setwindowshookex.asp
/// </remarks>
[DllImport("user32.dll", CharSet = CharSet.Auto,
CallingConvention = CallingConvention.StdCall, SetLastError = true)]
private static extern int SetWindowsHookEx(
int idHook,
HookProc lpfn,
IntPtr hMod,
int dwThreadId);
/**//// <summary>
/// The UnhookWindowsHookEx function removes a hook procedure installed in a hook chain by the SetWindowsHookEx function.
/// </summary>
/// <param name="idHook">
/// [in] Handle to the hook to be removed. This parameter is a hook handle obtained by a previous call to SetWindowsHookEx.
/// </param>
/// <returns>
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
/// </returns>
/// <remarks>
/// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/hooks/hookreference/hookfunctions/setwindowshookex.asp
/// </remarks>
[DllImport("user32.dll", CharSet = CharSet.Auto,
CallingConvention = CallingConvention.StdCall, SetLastError = true)]
private static extern int UnhookWindowsHookEx(int idHook);
/**//// <summary>
/// The CallNextHookEx function passes the hook information to the next hook procedure in the current hook chain.
/// A hook procedure can call this function either before or after processing the hook information.
/// </summary>
/// <param name="idHook">Ignored.</param>
/// <param name="nCode">
相關推薦
C#利用鉤子控制滑鼠【月兒原創】
///<summary> /// The ToAscii function translates the specified virtual-key code and keyboard /// state to the corresponding character or character
C 利用鉤子控制滑鼠【月兒原創】
C#利用鉤子控制滑鼠 作者:清清月兒 主頁:http://blog.csdn.net/21aspnet/ 時間:2007.5.11 工作中有這樣的需求,某個控制元件pan
C#精髓【月兒原創】第三講 C#泛型有什麼好處
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!  
C#好書盤點【月兒原創】
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!  
C 精髓【月兒原創】第三講 C 泛型有什麼好處
說明:準備出一個系列,所謂精髓講C#語言要點。這個系列沒有先後順序,不過儘量做到精。可能會不斷增刪整理,本系列最原始出處是csdn部落格,謝謝關注。 C#精髓 第三講 C#泛型有什麼好處 作者:清清月兒 主頁:http://blog.csdn.net/21aspnet/ &n
C 中判斷空字串的3種方法效能分析【月兒原創】
C#中判斷空字串的3種方法效能分析 作者:清清月兒 主頁:http://blog.csdn.net/21aspnet/ 時間:2007.4.28 3種方法分別
C#中判斷空字串的3種方法效能分析【月兒原創】
C#中判斷空字串的3種方法效能分析作者:清清月兒 3種方法分別是:string a="";1.if(a=="")2.if(a==String.Empty)3.if(a.Length==0)3種方法都是等效的,那麼究竟那一種方法效能最高呢?本人用實驗說明問題。建立3個aspx頁
C 精髓【月兒原創】第二講 WMI完美秀出CPU編號廠商主頻百分比等全部資訊
說明:準備出一個系列,所謂精髓講C#語言要點。這個系列沒有先後順序,不過儘量做到精。可能會不斷增刪整理,本系列最原始出處是csdn部落格,謝謝關注。C#精髓第二講 WMI完美秀出CPU編號廠商主頻電壓等全部資訊作者:清清月兒 關於WMI MSDN有詳細說明。 本文列舉數例算拋磚
C 對Microsoft VisualBasic My對象蘭臺妙選【月兒原創】
操作系統 physical lln gb2 大小 clas partial edit device C#對Microsoft.VisualBasic My對象蘭臺妙選 作者:清清月兒 主頁:http://blog.csdn.net/21aspnet/
asp.net2.0學習歷程 菜鳥到中級程式設計師的飛躍【月兒原創】
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!  
ASP NET2 0圖片格式轉換【月兒原創】
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!  
ASP NET2 0 文字編輯器FCKeditor的冰冷之心 【月兒原創】
ASP.NET2.0 文字編輯器FCKeditor的冰冷之心 作者:清清月兒 主頁:http://blog.csdn.net/21aspnet/ 時間:2007.
asp net2 0匯出pdf檔案完美解決方案【月兒原創】
asp.net2.0匯出pdf檔案完美解決方案 作者:清清月兒 主頁:http://blog.csdn.net/21aspnet/ 時間:2007.5.28 PDF簡介:
asp.net2.0匯出pdf檔案完美解決方案【月兒原創】
{ Document document =new Document(); PdfWriter.getInstance(document, new FileStream(Server.MapPath("Chap0101.pdf"), FileMode.Create))
ASP.NET2.0 遍歷資料夾下所有圖片【月兒原創】
ASP.NET2.0 遍歷資料夾下所有圖片 作者:清清月兒 1.以下目錄有若干圖片(都是大圖片) 2.在頁面展現效果圖 3.程式碼 後臺程式碼using System;using System.Data;using System.Configuration;using
C++利用WebBrowser控制元件建立自己的瀏覽器
是不是很早就想擁有一個很有個性的瀏覽器?本文將介紹如何利用MicroSoft的 WebBrowser控制元件在VC中建立一個屬於自己的瀏覽器。過程如下: ①新建一個基於對話方塊的工程,命名為test,然後在對話方塊上單擊右鍵,選擇 Insert ActiveX Control…在彈出
c# 利用webbrower控制元件列印html——實現列印頁面設定
領導分配了一個任務,利用c#連線印表機,列印一個html頁面,對於一個Java本專業還不太熟練的我,之前從未接觸過c#,挑戰還是很大的。但是沒辦法,做唄!!!首先找一個寫c#的工具唄,所以下載了vs2015,實現方法如下:1、新建一個form專案2、form中整合一個webb
C#--利用Timer控制元件設計一個電子時鐘。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin
【HAVENT原創】Australian Business Number (ABN) 驗證
exception lis mat ber java pri bnl javascrip style PHP 代碼如下: // ValidateABN // Checks ABN for validity using the published //
【HAVENT原創】Salesforce 給字段加上鏈接
target name 保存 link lan 編輯 text clas logs 1、首先在創建字段時候選擇類型 Formula 2、第二步字段類型我選擇了 Text 3、編輯公式: HYPERLINK(url, friendly_name [,target]) H