Trapping mouse events

The following example demonstrates how to use Akemi Spy Library to trap Mouse events in another application.

To create this example from scratch, create a standard exe project with one form.  Open zip file

The form has the following controls:

lblmDescription Label.  Displays a description of the application.
lblmMousePositionLabel Label.  Displays the words "Mouse Position"
lblmMousePosition Label.  Displays the coordinates of the mouse whenever it is over a Notepad window (or Notepad generated dialog).  Coordinates are relative to the top-left corner of the window or control that the mouse is over.
lblmControlTextLabel Label.  Displays the words "Control Text"
lblmControlText Label.  Displays the caption of any control that the mouse is over, as long as it is a control owned by the instance of Notepad being monitored.
lblmButtonClickedLabel Label.  Displays the words "Button Clicked"
lblmButtonClicked Label.  Displays the caption of the last button to be clicked (the last button owned by the instance of Notepad being monitored, that is.
btnmStartNotepad CommandButton.  Starts an instance of Notepad.exe or switches to an already open instance.

The code for this form is below:

Option Explicit

'ShellExecute API is used to launch Notepad.
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
'Constant used in call to ShellExecute
Private Const SW_SHOWNORMAL = 1

'SetActiveWindow API is used to activate an already opened
'instance of Notepad.
Private Declare Function SetActiveWindow Lib "user32" (ByVal hwnd As Long) As Long

'AttachThreadInput API is necessary in order for
'SetActiveWindow API to work.
Private Declare Function AttachThreadInput Lib "user32" (ByVal idAttach As Long, ByVal idAttachTo As Long, ByVal fAttach As Long) As Long



'Object to receive mouse events must be declared at
'class or form level.
Private WithEvents objmMouseEvents As MouseEvents

'Number of seconds to wait before giving up
'in Function WaitForWindow
Private lmSeconds As Long

Private Sub btnmStartNotepad_Click()
    Dim objNotepadWindow As Window

    'Find the first instance of Notepad to come to hand.
    'This example does not account for the possibility of multiple
    'instances of Notepad being open - only the first one to be
    'found will be monitored.
    Set objNotepadWindow = GetTopLevelWindows.FindWindow("* Notepad", "Notepad")

    'If there is no instance of Notepad already running, then
    'start one.
    If objNotepadWindow Is Nothing Then
        If ShellExecute(Me.hwnd, "Open", "Notepad.exe", "", "", SW_SHOWNORMAL) <= 32 Then
            MsgBox "Unable to start Notepad."
            Exit Sub
        Else
            Set objNotepadWindow = WaitForWindow("* Notepad", "Notepad")
            If objNotepadWindow Is Nothing Then
                MsgBox "Timeout expired, waiting for Notepad to open."
            Exit Sub
            End If
        End If
    Else
        'There is already an instance of Notepad running,
        'so switch to that instance.
        AttachThreadInput App.ThreadID, objNotepadWindow.Thread, True
        SetActiveWindow objNotepadWindow
    End If

    'Start monitoring Mouse Events
    Set objmMouseEvents = objNotepadWindow.Thread.MouseEvents
End Sub

Private Function WaitForWindow(ByVal sCaption As String, ByVal sClassName As String) As Window
    Dim objWindow As Window

    'Wait up to 5 seconds for the specified window to be opened.
    lmSeconds = 5
    While objWindow Is Nothing And lmSeconds > 0
        'DoEvents gives Timer chance to fire its event.
        DoEvents
        Set objWindow = GetTopLevelWindows.FindWindow(sCaption, sClassName)
    Wend

    'If the specified window was not opened, then the return
    'value will be Nothing.
    Set WaitForWindow = objWindow
End Function

Private Sub tmrm_Timer()
    lmSeconds = lmSeconds - 1
End Sub

Private Sub objmMouseEvents_MouseEnter(Window As AkemiSpyLibrary.Window)
    'As the mouse pointer enters a window, display
    'the window's caption.
    lblmControlText.Caption = Window.Caption
End Sub

Private Sub objmMouseEvents_MouseLeave(Window As AkemiSpyLibrary.Window)
    'As the mouse pointer leaves a window, clear the
    'control text label.
    lblmControlText.Caption = ""
End Sub

Private Sub objmMouseEvents_MouseMove(Window As AkemiSpyLibrary.Window, ByVal Shift As Boolean, ByVal Control As Boolean, ByVal x As Long, ByVal y As Long)
    'Display the mouse position. Coordinates are relative
    'to the top-left corner of the window in which the mouse is moving.
    lblmMousePosition.Caption = Format$(x) & ", " & Format$(y)
End Sub

Private Sub objmMouseEvents_Click(Window As AkemiSpyLibrary.Window, ByVal Button As MouseButtonConstants, ByVal Shift As Boolean, ByVal Control As Boolean, ByVal x As Long, ByVal y As Long)
    'If the user clicks on a control, display the caption of that control.
    'Note that if the caption contains an ampersand (&), the letter
    'after the ampersand will be underlined to indicate a keyboard
    'accelerator.
    lblmButtonClicked.Caption = Window.Caption
End Sub

 

See also:  KeyboardEvents Object, MouseEvents Property

 

Home Copyright and Disclaimer