zoukankan      html  css  js  c++  java
  • 检测系统范围内的鼠标事件

    下载源- 14.43 KB 更新版本MouseHunter项目与鼠标滚轮方向检测支持下载:下载MouseHunter_1.1.zip。介绍 也许有时候你需要跟踪系统广泛的鼠标事件从您的应用程序。但有可能偷鼠标事件的系统?应用程序也可以得到系统事件的通知事件发生?如果你想知道系统广泛的鼠标事件检测和事件的秘密窃取,下面这篇文章是给你的。 背景 我有很多样品对连接在互联网上。但我总是想要一个鼠标连接解决方案,将通知我OnMouseClick类型的事件在应用程序系统中如果MouseClick发生在任何地方。在这里我已经创建了一个。这是一个VB6 ActiveX DLL项目。它可以跟踪所有需要的鼠标事件您会感兴趣,也可以提高事件向您的应用程序使用图书馆系统中相应的事件发生。你可以得到窗口的标题(如果有的话)在你的鼠标,你可以得到窗口句柄(hWnd)在你的鼠标,鼠标的x和y坐标在你的电脑屏幕上。最后,一个更有趣的是,你将能够拦截任何鼠标事件的系统得到这个库的支持。这意味着,例如,您可以决定不通过“鼠标的右按钮下”事件的系统,而你将在应用程序中处理这个问题。 我叫MouseHunter DLL项目。它有一个类模块命名为示踪剂和一个名为modHook的通用模块。示踪剂类的代码如下所示:

    Option Explicit
      
    '* 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 system.
    Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" _
        (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, _
        ByVal dwThreadId As Long) As Long
    '* The UnhookWindowsHookEx function removes a hook procedure installed in a hook chain 
    'by the SetWindowsHookEx function.
    Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
    '* 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.
    Private Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, _
        ByVal nCode As Long, ByVal wParam As Long, lParam As Any) As Long
    
    '* The IsWindow function determines whether the specified window 
    'handle identifies an existing window.
    Private Declare Function IsWindow Lib "user32" (ByVal hwnd&) As Long
    
    '* The GetWindowText function copies the text of the specified window’s 
    'title bar (if it has one) into a buffer. If the specified window is a control, 
    'the text of the control is copied.
    Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
        (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    '* The WindowFromPoint function retrieves the handle of the window that contains 
    'the specified point.
    Private Declare Function WindowFromPoint Lib "user32" _
        (ByVal xPoint As Long, ByVal yPoint As Long) As Long
    '* The GetCursorPos function retrieves the cursor’s position, in screen coordinates.
    Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    
    '* Type to hold the mouse coordinates
    Private Type POINTAPI
        X As Long
        Y As Long
    End Type
    
    Private point As POINTAPI
    
    '******************************************************
    '* System messages for Mouse that we want to trace
    Private Const WM_MOUSEMOVE           As Long = &H200
    Private Const WM_MOUSEWHEEL          As Long = &H20A
    Private Const WM_LBUTTONDOWN         As Long = &H201
    Private Const WM_LBUTTONUP           As Long = &H202
    Private Const WM_RBUTTONDOWN         As Long = &H204
    Private Const WM_RBUTTONUP           As Long = &H205
    Private Const WM_MBUTTONDOWN         As Long = &H207
    Private Const WM_MBUTTONUP           As Long = &H208
    '******************************************************
    
    '* System hook for mouse that we'll use
    Private Const WH_MOUSE_LL            As Long = 14
    
    Private mlngMouseHook               As Long
    Private mstrWindowCaption           As String
    
    '******************************************************
    '* System events of Mouse that we can stop broadcasting
    '* to the System and make only traceable from the caller application
    Public Type EventThief
        LEFT_DOWN       As Boolean
        LEFT_UP         As Boolean
        RIGHT_DOWN      As Boolean
        RIGHT_UP        As Boolean
        MIDDLE_DOWN     As Boolean
        MIDDLE_UP       As Boolean
        WHEEL           As Boolean
        MOVE            As Boolean
    End Type
    
    Private mtypEventThief As EventThief
    
    Private Const HC_ACTION = 0
    
    '* Type to hold Mouse Hook information
    Private Type MOUSELLHOOKSTRUCT
        point As POINTAPI
        data As Long
        flags As Long
        time As Long
        extra As Long
    End Type
    
    Private mousedata As MOUSELLHOOKSTRUCT
    
    '******************************************************
    '* Events that will be fired to the caller application
    '* For different Mouse activities in the System
    Public Event OnSystemMouseMove()
    Public Event OnSystemMouseWheel()
    Public Event OnSystemMouseLeftDown()
    Public Event OnSystemMouseLeftUp()
    Public Event OnSystemMouseRightDown()
    Public Event OnSystemMouseRightUp()
    Public Event OnSystemMouseMiddleDown()
    Public Event OnSystemMouseMiddleUp()
    '******************************************************
    
    '* Get the mouse events that we want to steal from the System
    Public Property Get StealMouseEvents() As EventThief
        StealMouseEvents = mtypEventThief
    End Property
    
    '* Set the mouse events that we want to steal from the System
    Public Property Let StealMouseEvents(typEventThief As EventThief)
        mtypEventThief = typEventThief
    End Property
    
    '* Get the X coordinate of the Mouse in the current screen
    Public Property Get CoordinateX() As Long
        CoordinateX = point.X
    End Property
    
    '* Get the Y coordinate of the Mouse in the current screen
    Public Property Get CoordinateY() As Long
        CoordinateY = point.Y
    End Property
    
    '* Get the Caption (if any) of the window currently under the Mouse
    Public Property Get WindowTextUnderMouse() As String
        WindowTextUnderMouse = GetWindowTitle(point.X, point.Y)
    End Property
    
    '* Get the handle (hWnd) of the window currently under the Mouse
    Public Property Get WindowHandleUnderMouse() As Long
        WindowHandleUnderMouse = GetWindowHandle(point.X, point.Y)
    End Property
    
    '* Start tracing system wide mouse events
    Public Sub StartMouseTracing(ByVal hwnd As Long)
        If IsWindow(hwnd) Then
            glnghWnd = hwnd
            
            '* If there is already a hook by the application, don't hook again
            If GetProp(hwnd, "MouseHook") Then
                Exit Sub
            End If
            
            '* Try to set the Object instance of the Tracer class in memory
            '* If successful, then set the hook
            If SetProp(hwnd, ByVal "MouseHook", ObjPtr(Me)) Then
                mlngMouseHook = SetWindowsHookEx(WH_MOUSE_LL, AddressOf modHook.MouseProc, _
                App.hInstance, 0)
            End If
        End If
    End Sub
    
    '* Stop tracing system wide mouse events
    Public Sub StopMouseTracing()
        '* First check if the hook was established
        If mlngMouseHook <> 0 Then
            RemoveProp glnghWnd, "MouseHook"
            UnhookWindowsHookEx mlngMouseHook
        End If
    End Sub
    
    Private Sub Class_Terminate()
        '* Make sure the hook is removed after the class is terminated
        StopMouseTracing
    End Sub
    
    '* Bypassed MouseProc from the modHook's MouseProc.
    '* The trick is necessary here to raise the appropriate events to the caller
    '* for particular System events of the Mouse
    Friend Function MouseProc(ByVal nCode As Long, _
        ByVal wParam As Long, ByVal lParam As Long) As Long
        If (nCode = HC_ACTION) Then
            ' Mouse data not used in this example, but useful
            CopyMemory mousedata, ByVal lParam, Len(mousedata)
            Select Case wParam
                Case WM_MOUSEMOVE
                    '* Trace the coordinate of the Mouse whenever it moves
                    GetCursorPos point
                    '* Raise the event to the caller Window
                    RaiseEvent OnSystemMouseMove
                    '* Check if caller wanted this event as non traceable to the System
                    If mtypEventThief.MOVE Then
                        '* If so, steal the event from the system and make it only traceable
                        '* to the window of the caller application
                        MouseProc = -1
                    Else
                        '* If not, release the event to the System
                        MouseProc = CallNextHookEx(0, nCode, wParam, ByVal lParam)
                    End If
                Case WM_MOUSEWHEEL
                    '* Raise the event to the caller Window
                    RaiseEvent OnSystemMouseWheel
                    '* Check if caller wanted this event as non traceable to the System
                    If mtypEventThief.WHEEL Then
                        '* If so, steal the event from the system and make it only traceable
                        '* to the window of the caller application
                        MouseProc = -1
                    Else
                        '* If not, release the event to the System
                        MouseProc = CallNextHookEx(0, nCode, wParam, ByVal lParam)
                    End If
                Case WM_LBUTTONDOWN
                    '* Raise the event to the caller Window
                    RaiseEvent OnSystemMouseLeftDown
                    '* Check if caller wanted this event as non traceable to the System
                    If mtypEventThief.LEFT_DOWN Then
                        '* If so, steal the event from the system and make it only traceable
                        '* to the window of the caller application
                        MouseProc = -1
                    Else
                        '* If not, release the event to the System
                        MouseProc = CallNextHookEx(0, nCode, wParam, ByVal lParam)
                    End If
                Case WM_LBUTTONUP
                    '* Raise the event to the caller Window
                    RaiseEvent OnSystemMouseLeftUp
                    '* Check if caller wanted this event as non traceable to the System
                    If mtypEventThief.LEFT_UP Then
                        '* If so, steal the event from the system and make it only traceable
                        '* to the window of the caller application
                        MouseProc = -1
                    Else
                        '* If not, release the event to the System
                        MouseProc = CallNextHookEx(0, nCode, wParam, ByVal lParam)
                    End If
                Case WM_RBUTTONDOWN
                    '* Raise the event to the caller Window
                    RaiseEvent OnSystemMouseRightDown
                    '* Check if caller wanted this event as non traceable to the System
                    If mtypEventThief.RIGHT_DOWN Then
                        '* If so, steal the event from the system and make it only traceable
                        '* to the window of the caller application
                        MouseProc = -1
                    Else
                        '* If not, release the event to the System
                        MouseProc = CallNextHookEx(0, nCode, wParam, ByVal lParam)
                    End If
                Case WM_RBUTTONUP
                    '* Raise the event to the caller Window
                    RaiseEvent OnSystemMouseRightUp
                    '* Check if caller wanted this event as non traceable to the System
                    If mtypEventThief.RIGHT_UP Then
                        '* If so, steal the event from the system and make it only traceable
                        '* to the window of the caller application
                        MouseProc = -1
                    Else
                        '* If not, release the event to the System
                        MouseProc = CallNextHookEx(0, nCode, wParam, ByVal lParam)
                    End If
                Case WM_MBUTTONDOWN
                    '* Raise the event to the caller Window
                    RaiseEvent OnSystemMouseMiddleDown
                    '* Check if caller wanted this event as non traceable to the System
                    If mtypEventThief.MIDDLE_DOWN Then
                        '* If so, steal the event from the system and make it only traceable
                        '* to the window of the caller application
                        MouseProc = -1
                    Else
                        '* If not, release the event to the System
                        MouseProc = CallNextHookEx(0, nCode, wParam, ByVal lParam)
                    End If
                Case WM_MBUTTONUP
                    '* Raise the event to the caller Window
                    RaiseEvent OnSystemMouseMiddleUp
                    '* Check if caller wanted this event as non traceable to the System
                    If mtypEventThief.MIDDLE_UP Then
                        '* If so, steal the event from the system and make it only traceable
                        '* to the window of the caller application
                        MouseProc = -1
                    Else
                        '* If not, release the event to the System
                        MouseProc = CallNextHookEx(0, nCode, wParam, ByVal lParam)
                    End If
                Case Else
                    ' not implemented yet
            End Select
        End If
    End Function
    
    '* Returns the Window Caption (if any) under the mouse
    Private Function GetWindowTitle(CoordX As Long, CoordY As Long) As String
        Dim strTitle As String
        strTitle = String(255, Chr$(0))
        GetWindowText WindowFromPoint(CoordX, CoordY), strTitle, 255
        strTitle = Left$(strTitle, InStr(strTitle, Chr$(0)) - 1)
        GetWindowTitle = strTitle
    End Function
    
    '* Returns the Window Handle (hWnd) under the mouse
    Private Function GetWindowHandle(CoordX As Long, CoordY As Long) As Long
        GetWindowHandle = WindowFromPoint(CoordX, CoordY)
    End Function

    这是通用的代码模块modHook:

    Option Explicit
    
    '* The CopyMemory function copies a block of memory from one location to another.
    Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
        (Destination As Any, Source As Any, ByVal Length As Long)
    '* The GetProp function retrieves a data handle from the property list 
    'of the given window. The given character string identifies the handle 
    'to be retrieved. The string and handle must have been added to the 
    'property list by a previous call to the SetProp function.
    Public Declare Function GetProp Lib "user32" Alias "GetPropA" _
        (ByVal hwnd As Long, ByVal lpString As String) As Long
    '* The SetProp function adds a new entry or changes an existing entry 
    'in the property list of the specified window. The function adds a new entry 
    'to the list if the specified character string does not exist already in the list. 
    'The new entry contains the string and the handle. 
    'Otherwise, the function replaces the string’s current handle with the specified handle.
    Public Declare Function SetProp Lib "user32" Alias "SetPropA" _
        (ByVal hwnd As Long, ByVal lpString As String, ByVal hData As Long) As Long
    '* The RemoveProp function removes an entry from the property list 
    'of the specified window. The specified character string identifies 
    'the entry to be removed.
    Public Declare Function RemoveProp Lib "user32" Alias "RemovePropA" _
        (ByVal hwnd&, ByVal lpString$) As Long
    
    '* Global variable to hold the hWnd of the caller application's window
    Global glnghWnd   As Long
    
    '* Main procedure to trace the mouse events, but it is bypassed
    '* to the Tracer class's MouseProc method so that the events can be raised from there.
    '* As you know events can't be raised from a general module.
    Public Function MouseProc(ByVal nCode As Long, _
        ByVal wParam As Long, ByVal lParam As Long) As Long
        MouseProc = TracerFromMemory(glnghWnd).MouseProc(nCode, wParam, lParam)
    End Function
    
    '* This method retrieves the object of the tracer class created in the memory
    Private Function TracerFromMemory(ByVal hwnd As Long) As Tracer
        Dim MsgHookEx   As Tracer
        Dim ptrObject   As Long
        
        '* Getting the already created Object of the Tracer class
        '* from memory in ptrObjec
        ptrObject = GetProp(glnghWnd, ByVal "MouseHook")
        
        '* Setting the Tracer class object from ptrObject to MsgHookEx
        CopyMemory MsgHookEx, ptrObject, Len(ptrObject)
        
        '* return the Tracer class object
        Set TracerFromMemory = MsgHookEx
        
        '* Remove the reference of the Tracer class object from memory
        CopyMemory MsgHookEx, 0&, Len(ptrObject)
    End Function

    使用的代码 您可以使用高级语言从任何COM库兼容。我已经创建了一个示例VB6标准的EXE应用程序显示这个库的使用。只有一种形式命名frmMain和它有两个标签命名lblWindowTitle,lblEvents。只是一件事关于这个测试应用程序是,要记住不要关闭它的停止按钮VB IDE。相反,使用表单的十字图标来关闭应用程序。下面的代码:

    '********************************************************
    '* WARNING!!!
    '* PLEASE DO NOT STOP THIS APPLICATION FROM THE VB IDE
    '* CLICK THE FORM'S CROSS ICON IF YOU INTEND TO CLOSE
    '********************************************************
    
    Option Explicit
    
    '* Declaring the mouse tracer object using WithEvents so that the events
    '* can be traced
    Private WithEvents MyMouseHunter As MouseHunter.Tracer
    
    Private Sub Form_Load()
        '* EventStealingInfo will help to steal any particular mouse event you want.
        '* This will help you to grab the event just within your application
        '* And will not pass the event to the system
        Dim EventStealingInfo As EventThief
        
        lblWindowTitle.Caption = ""
        lblEvents.Caption = ""
        
        Set MyMouseHunter = New MouseHunter.Tracer
        
        '* Selecting the particular mouse events that you want to steal
        '* from the system
        '* Here, you'll steal the Mouse's Right Button's UP & Down events
        '* From the System, so you'll see your right button of the Mouse
        '* Is not working in anywhere in the System, But your application
        '* is still tracing the events!!
        With EventStealingInfo
            .RIGHT_DOWN = True
            .RIGHT_UP = True
        End With
        
        '* Passing the EventStealingInfo to the tracer class
        MyMouseHunter.StealMouseEvents = EventStealingInfo
        '* Start tracing the system wide mouse events
        MyMouseHunter.StartMouseTracing Me.hWnd
        
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
        '* make sure the system wide mouse tracing is stopped after the application
        '* is closed
        MyMouseHunter.StopMouseTracing
    End Sub
    
    '* Tracking the system wide mouse events
    Private Sub MyMouseHunter_OnSystemMouseMove()
        '* Choose anything you want to trace when your mouse moves
        '* You can trace the X and Y coordinate of your mouse position in the window
        '* Or the Window handle (hWnd) of the window under your mouse
        '* Or the Window title (Caption) under your mouse.
        
        'lblWindowTitle.Caption = MyMouseHunter.CoordinateX & "," _
                    & MyMouseHunter.CoordinateY
        'lblWindowTitle.Caption = MyMouseHunter.WindowHandleUnderMouse
        lblWindowTitle.Caption = MyMouseHunter.WindowTextUnderMouse
        
        lblEvents.Caption = "Moving..."
    End Sub
    
    Private Sub MyMouseHunter_OnSystemMouseLeftDown()
        lblEvents.Caption = "Left Down"
    End Sub
    
    Private Sub MyMouseHunter_OnSystemMouseLeftUp()
        lblEvents.Caption = "Left Up"
    End Sub
    
    Private Sub MyMouseHunter_OnSystemMouseRightDown()
        lblEvents.Caption = "Right Down"
    End Sub
    
    Private Sub MyMouseHunter_OnSystemMouseRightUp()
        lblEvents.Caption = "Right Up"
    End Sub
    
    Private Sub MyMouseHunter_OnSystemMouseMiddleDown()
        lblEvents.Caption = "Middle Down"
    End Sub
    
    Private Sub MyMouseHunter_OnSystemMouseMiddleUp()
        lblEvents.Caption = "Middle Up"
    End Sub
    
    Private Sub MyMouseHunter_OnSystemMouseWheel()
        lblEvents.Caption = "Wheel..."
    End Sub
    
    '* ENJOY!!!

    的兴趣点 使用这个库,系统广泛的鼠标事件跟踪将会非常容易。希望你能享受的代码。 历史 上传的贾基尔侯赛因在2008年9月27日,46点(格林尼治时间+ 6) 本文转载于:http://www.diyabc.com/frontweb/news2244.html

  • 相关阅读:
    SQL Server的全局变量
    SQL局部变量
    视图和表之间的区别和联系
    SQL Server中开发常用的数据类型
    Bootstrap 字体图标、下拉菜单、按钮组
    Bootstrap 按钮,图片,辅助类
    Bootstrap表单
    Bootstrap表格
    Bootstrap排版类
    Bootstrap网格系统
  • 原文地址:https://www.cnblogs.com/Dincat/p/13457372.html
Copyright © 2011-2022 走看看