Option Explicit Dim WithEvents sh As MouseHook.cSystemHook Dim WithEvents Wb As Excel.Workbook '= Dim WithEvents Wb As Workbook Private Sub sh_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single) On Error Resume Next Application.Caption = "X:" & x & ";" & "Y:" & y & " " & ActiveWindow.RangeFromPoint(x, y).Address ActiveWindow.RangeFromPoint(x, y).Activate End Sub Private Sub sh_KeyDown(KeyCode As Integer, Shift As Integer) Debug.Print "KeyDown:" & KeyCode & "; " & Shift; "" End Sub Private Sub sh_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single) On Error Resume Next If Button = 1 Then Debug.Print "你按了左键" End If If Button = 2 Then Debug.Print "你按了右键" End If ' If Button = 4 Then Debug.Print "你按了中键" End If End Sub '写入Workbook事件中相当不稳定. Sub StartHook() Set sh = New cSystemHook sh.SetHook End Sub Sub StopHook() sh.RemoveHook Set sh = Nothing End Sub Private Sub sh_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single) Debug.Print "Mouse Up:" & Button & "; " & Shift; "" End Sub Private Sub sh_WheelMoved(Button As Integer, Shift As Integer, x As Single, y As Single) Debug.Print "WheelMoved:" & Button & "; " & Shift; "" End Sub Private Sub Workbook_BeforeClose(Cancel As Boolean) On Error Resume Next sh.RemoveHook Set sh = Nothing End Sub Private Sub Workbook_Open() Set sh = New cSystemHook sh.SetHook End Sub
|