'利用mouse_event實現 Option ExplicitPrivate Type POINTAPI x As Long y As Long End Type Private Declare Function SetCursorPos Lib "user32.dll" (ByVal x As Long, ByVal y As Long) As Long Private Declare Function ClientToScreen Lib "user32.dll" (ByVal hwnd As Long, ByRef lpPoint As POINTAPI) As Long Private Declare Function GetCursorPos Lib "user32.dll" (ByRef lpPoint As POINTAPI) As Long Private Declare Sub mouse_event Lib "user32.dll" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long) Private Const MOUSEEVENTF_LEFTDOWN As Long = &H2 Private Const MOUSEEVENTF_LEFTUP As Long = &H4Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer) Dim pt As POINTAPI, oldPt As POINTAPI
Option ExplicitPrivate Type POINTAPI
x As Long
y As Long
End Type
Private Declare Function SetCursorPos Lib "user32.dll" (ByVal x As Long, ByVal y As Long) As Long
Private Declare Function ClientToScreen Lib "user32.dll" (ByVal hwnd As Long, ByRef lpPoint As POINTAPI) As Long
Private Declare Function GetCursorPos Lib "user32.dll" (ByRef lpPoint As POINTAPI) As Long
Private Declare Sub mouse_event Lib "user32.dll" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Private Const MOUSEEVENTF_LEFTDOWN As Long = &H2
Private Const MOUSEEVENTF_LEFTUP As Long = &H4Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Dim pt As POINTAPI, oldPt As POINTAPI
If KeyCode = vbKeyDown Then
GetCursorPos oldPt
pt.x = (Toolbar1.Buttons(1).Left + Toolbar1.Buttons(1).Width - 100) / Screen.TwipsPerPixelX
pt.y = (Toolbar1.Buttons(1).Top + Toolbar1.Buttons(1).Height / 2) / Screen.TwipsPerPixelY
'將Toolbar坐標轉換成屏幕坐標
ClientToScreen Toolbar1.hwnd, pt
'將鼠標位置稱至Toolbar
SetCursorPos pt.x, pt.y
mouse_event MOUSEEVENTF_LEFTDOWN, pt.x, pt.y, 0, 0
mouse_event MOUSEEVENTF_LEFTUP, pt.x, pt.y, 0, 0
'還原鼠標位置
SetCursorPos oldPt.x, oldPt.y
End If
End SubPrivate Sub Form_Load()
Toolbar1.Buttons.Add , , "Press Down", tbrDropdown
Toolbar1.Buttons(1).ButtonMenus.Add , , "item1"
Toolbar1.Buttons(1).ButtonMenus.Add , , "item2"
Me.Show
End Sub