点击“打开”按钮的思路:
当跳出对话框的时候,程序被挂在,程序不能直接跑到点击代码里,需要开一个线程进行点击。在VB6里,可以用Timer控件进行定时执行点击动作,实现类似线程的功能。
以下代码在Win7 64位下测试可以实现。
在窗体上添加Timer1控件,设定Timer1.Interval 为 100
‘------------------------------------------------------------------------------------------------------
Private Sub Timer1_Timer()
ClickPopupWindowButton
If Timer1.Tag = "" Then Timer1.Tag = 0‘建议用变量,使用控件的属性效率不高
Timer1.Tag = Timer1.Tag + 1
If Timer1.Tag = 20 Then’2秒钟后关闭定时器Timer1
Timer1.Enabled = False
Timer1.Tag = 0
End If
End Sub
‘------------------------------------------------------------------------------------------------------
使用Microsoft Spy++找到对话框“文件下载”#32770,找到“打开(&O)”,点击右键选择“属性...”
Sub ClickPopupWindowButton()
Dim hwndDialog As LongDim hwndButton As Long
Dim retval As Long
'FileName = "D:\lanbing.txt"
hwndDialog = FindWindow("#32770", "文件下载")
If hwndDialog <> 0 Then
'hwndButton = FindWindowEx(hwndDialog, 0, "ComboBoxEx32", vbNullString)'抄来,本"文件下载"窗口不存在的,仅供参考学习
'hwndButton = FindWindowEx(hwndButton, 0, "ComboBox", vbNullString)'抄来,本"文件下载"窗口不存在的,仅供参考学习
'hwndButton = FindWindowEx(hwndButton, 0, "Edit", vbNullString)'抄来,本“文件下载”窗口不存在的,仅供参考学习
'SendMessage hwndButton, WM_SETTEXT, 0, ByVal FileName'抄来,本“文件下载”窗口不存在的,仅供参考学习
'hwndButton = FindWindowEx(hwndDialog, 0, "Button", "保存(&S)")
hwndButton = FindWindowEx(hwndDialog, 0, "Button", "打开(&O)")
If hwndButton <> 0 Then'找到按钮
'retval = SetActiveWindow(hwndDialog)‘激活窗口
retval = SetForegroundWindow(hwndDialog)’设置到前台,并且激活该窗口
'retval = SendMessage(hwndButton, BM_CLICK, ByVal CLng(0), ByVal CLng(0))‘发送点击消息,对这里试了不起作用
'retval = SendMessage(hwndButton, WM_LBUTTONDOWN, ByVal CLng(0), ByVal CLng(0))‘‘发鼠标左键按下消息
'Sleep 300
'retval = SendMessage(hwndButton, WM_LBUTTONUP, ByVal CLng(0), ByVal CLng(0)) '发鼠标左键抬起消息
'Sleep 300
'retval = SendMessage(hwndButton, WM_CHAR, 79, ByVal CLng(0)) '79-"O" ‘发O字,不起作用
’以上几个不起作用,因为焦点在“取消”按钮上,需要把焦点锁定到“打开”按钮上
Dim wMsg As Long, wParam As Long, lParam As Long, Rx As Long, xx As Integer, yy As Integerxx = 50 '点击的x坐标 按钮大小78x21,取中间位置(50,10)
yy = 10 '点击的y坐标
wMsg = WM_LBUTTONDOWN '左键按下消息
wParam = 1
lParam = yy * 65536 + xx
Call PostMessage(hwndButton, wMsg, wParam, lParam) '发送消息
Sleep 300
wMsg = WM_LBUTTONUP '左键抬起消息
wParam = 1
lParam = yy * 65536 + xx
Call PostMessage(hwndDialog, wMsg, wParam, lParam) '发送消息
Sleep 350’试了200不行,300可以,放大一点点,还没在不同电脑上测试
‘以上PostMessage发送的消息无法实现点击,但是可以把焦点锁定在“打开”按钮上
'Timer1.Enabled = False’关掉定时器
'Timer1.Tag = 0
End If
End If
End Sub
注意:在调试时代码时执行第一次可以成功点击“打开”,点选listbox的另外文件时,可能不执行点击的动作,生成.exe文件后每次都能成功点击“打开”按钮。