思路:
使用Windows API函数遍历窗口,查找指定标题的窗口,然后从该窗口查找确定按钮,向该按钮发送鼠标消息进行模拟点击。由于IE8由Alert弹出的网页对话框的标题是“来自网页的消息”,而IE6由Alert弹出的网页对话框的标题是“Microsoft Internet Explorer”,所以本文没有按查找窗口标题方法获取窗口句柄。注意到不管IE哪个版本,IE窗口的类名都是“IEFrame”,而网页对话框的父窗口是IE窗口,所以本文是根据父窗口的类名是不是IEFrame来判断窗口是不是网页对话框,以下代码点击网页弹出的Alert对话框和Confirm对话框的“确定”按钮,并用Label1控件显示网页对话框的消息内容:
实现:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
procedure TForm1 . Button1Click(Sender: TObject); var hIE, hDlg, hBtn, hStatic:HWND; Text: array [ 0..255 ] of char ; begin hDlg := GetWindow(Handle, GW_HWNDFIRST); While hDlg <> 0 do begin hIE := GetParent(hDlg); if GetClassName(hIE, Text, 255 ) > 0 then //获取父窗口类名 if Text = ‘IEFrame‘ then //父窗口为IE窗口 begin hBtn := FindWindowEx(hDlg, 0 , nil , ‘确定‘ ); //查看确定按钮 if hBtn <> 0 then begin hStatic := FindWindowEx(hDlg, 0 , ‘Static‘ , nil ); hStatic := FindWindowEx(hDlg, hStatic, ‘Static‘ , nil ); if GetWindowText(hStatic, Text, 255 ) > 0 then Label1 . Caption := Text; Sleep( 100 ); SendMessage(hBtn, $00F5 , 0 , 0 ); //第一次点击使确定按钮获取焦点 Sleep( 100 ); SendMessage(hBtn, $00F5 , 0 , 0 ); //第二次点击击中确定按钮 end ; end ; hDlg:=GetWindow(hDlg,GW_HWNDNEXT); end ; end ; |