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 Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) 'Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
'模拟键盘输入声明 Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal Scan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Const WM_COMMAND As Long = &H111
Private Sub Command1_Click()
Dim h1 As Long, h2 As Long, id As Long
Dim h3 As Long, OK As Long
h1 = FindWindow(vbNullString, "无标题 - 记事本")
'记事本的句柄
Debug.Print h1
h2 = GetMenu(h1)
h2 = GetSubMenu(h2, 0)
'文件"菜单的句柄 改变后面的0,就可以得到不同菜单的句柄
id = GetMenuItemID(h2, 3)
'子菜單"另保存""的ID
SendMessage h1, WM_COMMAND, id, ByVal 0&
'延迟3秒 Call Sleep(3000)
'控制弹出窗口
'h3 = FindWindow(vbNullString, "记事本")
'记事本弹出窗口的句柄
'OK = GetMenuItemID(h3, 0)
'SendMessage h3, WM_COMMAND, OK, ByVal 0&
h3 = FindWindow(vbNullString, "另存为")
'记事本弹出窗口“另存为”的句柄 '
SendMessage h3, "1", , ByVal 0& Const VK_Tab = &H9
Const VK_Return = &H13
Call keybd_event(VK_Tab, 0, 0, 0)
'模拟键盘输入Tab Sleep 50 '两次相同按键的模拟输入间要人工延迟
Call keybd_event(VK_Tab, 0, 0, 0)
'模拟键盘输入Tab
Call keybd_event(VK_Return, 0, 0, 0)
'模拟键盘输入Enter End Sub 我用上面这段代码,希望实现一下功能:(在已经手动打开一个未命名的记事本文档的前提下) 1.通过单击Command1按钮能够自动选中“未命名-记事本”程序的“程序”菜单的“另存为”选项;(已实现) 2.在弹出的“另存为”窗口中自动填入某一文件名(可以是流水号)然后保存(仍未完成);请问:在VB中该如何控制其它程序的弹出窗口呢?
你用到了FindWindow,你再试试下面这个函数【
VB声明】 Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
【别名】 FindWindowExA
【说明】 在窗口列表中寻找与指定条件相符的第一个子窗口
【返回值】 Long,找到的窗口的句柄。如未找到相符窗口,则返回零。会设置GetLastError
【参数表】 hWnd1 ---------- Long,在其中查找子的父窗口。如设为零,表示使用桌面窗口(通常说的顶级窗口都被认为是桌面的子窗口,所以也会对它们进行查找) hWnd2 ---------- Long,从这个窗口后开始查找。这样便可利用对FindWindowEx的多次调用找到符合条件的所有子窗口。如设为零,表示从第一个子窗口开始搜索 lpsz1 ---------- String,欲搜索的类名。零表示忽略 lpsz2 ---------- String,欲搜索的类名。零表示忽略
|