VB 控制其他程序
我想用VB控制 VB的编辑器,如点击菜单栏文件,然后在弹出的下拉菜单点击 打开工程 菜单栏的句柄我能捉到,模拟鼠标点击却总实现不了。
下面的程序对记事本有效,却对VB编辑器实现不了,难道getmenu有适用条件?
Option Explicit
Private Declare Function GetMenu Lib "user32.dll" (ByVal hwnd As Long) As Long
Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetMenuItemID Lib "user32.dll" (ByVal hMenu As Long, ByVal nPos As Long) As Long
Private Declare Function GetSubMenu Lib "user32.dll" (ByVal hMenu As Long, ByVal nPos As Long) As Long
Private Const WM_COMMAND As Long = &H111
Private Sub Command1_Click()
Dim h1 As Long, h2 As Long, id As Long
h1 = FindWindow(vbNullString, "无标题 - 记事本") '计事本的句柄
Debug.Print h1
h2 = GetMenu(h1)
h2 = GetSubMenu(h2, 0) '文件"菜单的句柄 改变后面的0,就可以
'得到不同菜单的句柄
id = GetMenuItemID(h2, 0) '子菜單"新增"的ID
SendMessage h1, WM_COMMAND, id, ByVal 0&
End Sub