因为项目需要,我已用WebBrowser模拟了浏览器的大多数功能,但目前有个问题一直没有解决,特求高手指点一二! 就是:
怎么实现在WebBrowser里模拟Javascript功能,比如window.close();子窗口、父窗口之间传值、控制(如:opener.document.……),谢谢!
怎么实现在WebBrowser里模拟Javascript功能,比如window.close();子窗口、父窗口之间传值、控制(如:opener.document.……),谢谢!
调试欢乐多
interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, OleCtrls, SHDocVw;type
TForm1 = class(TForm)
WebBrowser1: TWebBrowser;
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
var
HtmlPath: string;
begin
WebBrowser1.Left := -WebBrowser1.Width; {隐藏 WebBrowser1}
HtmlPath := ExtractFilePath(ParamStr(0)) + 'Test.htm';
WebBrowser1.Navigate(HtmlPath);
end;procedure TForm1.Button1Click(Sender: TObject);
var
str: Variant;
begin
str := WebBrowser1.OleObject.document.parentWindow.JsStr2('12345');
ShowMessage(str);
end;procedure TForm1.Button2Click(Sender: TObject);
var
num: Variant;
begin
num := WebBrowser1.OleObject.document.parentWindow.JsNum2(4);
ShowMessage(num);
end;procedure TForm1.Button3Click(Sender: TObject);
begin
WebBrowser1.OleObject.document.parentWindow.JsMsg('ABC'); {也可以这样:}
WebBrowser1.OleObject.document.parentWindow.execScript('JsMsg("ABCDEFG")', 'JavaScript');
end;end.
--------------------------------------------------------------------------------窗体文件:
--------------------------------------------------------------------------------object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 167
ClientWidth = 280
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
Position = poDesktopCenter
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
object WebBrowser1: TWebBrowser
Left = 64
Top = 48
Width = 154
Height = 97
TabOrder = 0
ControlData = {
4C000000EB0F0000060A00000000000000000000000000000000000000000000
000000004C000000000000000000000001000000E0D057007335CF11AE690800
2B2E126208000000000000004C0000000114020000000000C000000000000046
8000000000000000000000000000000000000000000000000000000000000000
00000000000000000100000000000000000000000000000000000000}
end
object Button1: TButton
Left = 104
Top = 40
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 1
OnClick = Button1Click
end
object Button2: TButton
Left = 104
Top = 81
Width = 75
Height = 25
Caption = 'Button2'
TabOrder = 2
OnClick = Button2Click
end
object Button3: TButton
Left = 104
Top = 120
Width = 75
Height = 25
Caption = 'Button3'
TabOrder = 3
OnClick = Button3Click
end
end
打印、打印预览和页面设置: 调用“打印”、“打印预览”和“页面设置”对话框(IE5.5及以上版本才支持打 印预览,故实现应该检查此命令是否可用)。 ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_DODEFAULT, EmptyParam, EmptyParam); if QueryStatusWB(OLECMDID_PRINTPREVIEW)=3 then ExecWB(OLECMDID_PRINTPREVIEW, OLECMDEXECOPT_DODEFAULT, EmptyParam,EmptyParam); ExecWB(OLECMDID_PAGESETUP, OLECMDEXECOPT_DODEFAULT, EmptyParam, EmptyParam);
剪切、复制、粘贴、全选: 功能无须多说,需要注意的是:剪切和粘贴不仅对编辑框文字,而且对网页上的非编辑框文字同样有效,用得好的话,也许可以做出功能特殊的东东。获得其命令使能状态和执行命令的方法有两种(以复制为例,剪切、粘贴和全选分别将各自的关键字替 换即可,分别为CUT,PASTE和SELECTALL): A、用TWebBrowser的QueryStatusWB方法。 if(QueryStatusWB(OLECMDID_COPY)=OLECMDF_ENABLED) or OLECMDF_SUPPORTED) then ExecWB(OLECMDID_COPY, OLECMDEXECOPT_DODEFAULT, EmptyParam, EmptyParam); B、用IHTMLDocument2的QueryCommandEnabled方法。 var Doc: IHTMLDocument2; begin Doc :=WebBrowser1.Document as IHTMLDocument2; if Doc.QueryCommandEnabled('Copy') then Doc.ExecCommand('Copy',false,EmptyParam); end; 查找: 参考第九条“查找”功能。--------------------------------------------------------------------------------------------------------4、字体大小 类似“字体”菜单上的从“最大”到“最小”五项(对应整数0~4,Largest等假设为五个菜单项的名字,Tag 属性分别设为0~4)。 A、读取当前页面字体大小。 var t: OleVariant; Begin WebBrowser1.ExecWB(OLECMDID_ZOOM, OLECMDEXECOPT_DONTPROMPTUSER, EmptyParam,t); case t of 4: Largest.Checked :=true; 3: Larger.Checked :=true; 2: Middle.Checked :=true; 1: Small.Checked :=true; 0: Smallest.Checked :=true; end; end; B、设置页面字体大小。 Largest.Checked :=false; Larger.Checked :=false; Middle.Checked :=false; Small.Checked :=false; Smallest.Checked :=false; TMenuItem(Sender).Checked :=true; t :=TMenuItem(Sender).Tag; WebBrowser1.ExecWB(OLECMDID_ZOOM, OLECMDEXECOPT_DONTPROMPTUSER, t,t);--------------------------------------------------------------------------------------------------------5、添加到收藏夹和整理收藏夹 const CLSID_ShellUIHelper: TGUID = '{64AB4BB7-111E-11D1-8F79-00C04FC2FBE1}'; var p:procedure(Handle: THandle; Path: PChar); stdcall; procedure TForm1.OrganizeFavorite(Sender: Tobject); var H: HWnd; begin H := LoadLibrary(PChar('shdocvw.dll')); if H <> 0 then begin p := GetProcAddress(H, PChar('DoOrganizeFavDlg')); if Assigned(p) then p(Application.Handle, PChar(FavFolder)); end; FreeLibrary(h); end; procedure TForm1.AddFavorite(Sender: TObject); var ShellUIHelper: ISHellUIHelper; url, title: Olevariant; begin Title := Webbrowser1.LocationName; Url := Webbrowser1.LocationUrl; if Url <> '' then begin ShellUIHelper := CreateComObject(CLSID_SHELLUIHELPER) as IShellUIHelper; ShellUIHelper.AddFavorite(url, title); end; end;--------------------------------------------------------------------------------------------------------6、使WebBrowser获得焦点 TWebBrowser非常特殊,它从TWinControl继承来的SetFocus方法并不能使得它所包含的文档获得焦点,从而不能立即使用Internet Explorer本身具有得快捷键,解决方法如下:< procedure TForm1.SetFocusToDoc; begin if WebBrowser1.Document <> nil then with WebBrowser1.Application as IOleobject do DoVerb(OLEIVERB_UIACTIVATE, nil, WebBrowser1, 0, Handle, GetClientRect); end; 除此之外,我还找到一种更简单的方法,这里一并列出: if WebBrowser1.Document <> nil then IHTMLWindow2(IHTMLDocument2(WebBrowser1.Document).ParentWindow).focus 刚找到了更简单的方法,也许是最简单的: if WebBrowser1.Document <> nil then IHTMLWindow4(WebBrowser1.Document).focus 还有,需要判断文档是否获得焦点这样来做: if IHTMLWindow4(WebBrowser1.Document).hasfocus then--------------------------------------------------------------------------------------------------------7、点击“提交”按钮 如同程序里每个窗体上有一个“缺省”按钮一样,Web页面上的每个Form也有一个“缺省”按钮——即属性为“Submit”的按钮,当用户按下回车键时就相当于鼠标单击了“Submit”。但是TWebBrowser似乎并不响应回车键,并且,即使把包含TWebBrowser的窗体的KeyPreview设为True,在窗体的KeyPress事件里还是不能截获用户向TWebBrowser发出的按键。 我的解决办法是用ApplicatinEvents构件或者自己编写TApplication对象的OnMessage事件,在其中判断消息类型,对键盘消息做出响应。至于点击“提交”按钮,可以通过分析网页源代码的方法来实现,不过我找到了更为简单快捷的方法,有两种,第一种是我自己想出来的,另一种是别人写的代码,这里都提供给大家,以做参考。 A、用SendKeys函数向WebBrowser发送回车键 在Delphi 5光盘上的Info\Extras\SendKeys目录下有一个SndKey32.pas文件,其中包含了两个函数SendKeys和AppActivate,我们可以用SendKeys函数来向WebBrowser发送回车键,我现在用的就是这个方法,使用很简单,在WebBrowser获得焦点的情况下(不要求WebBrowser所包含的文档获得焦点),用一条语句即可: Sendkeys('~',true);// press RETURN key SendKeys函数的详细参数说明等,均包含在SndKey32.pas文件中。 B、在OnMessage事件中将接受到的键盘消息传递给WebBrowser。 procedure TForm1.ApplicationEvents1Message(var Msg: TMsg; var Handled: Boolean); {fixes the malfunction of some keys within webbrowser control} const StdKeys = [VK_TAB, VK_RETURN]; { standard keys } ExtKeys = [VK_DELETE, VK_BACK, VK_LEFT, VK_RIGHT]; { extended keys } fExtended = $01000000; { extended key flag } begin Handled := False; with Msg do if ((Message >= WM_KEYFIRST) and (Message <= WM_KEYLAST)) and ((wParam in StdKeys) or {$IFDEF VER120}(GetKeyState(VK_CONTROL) < 0) or {$ENDIF} (wParam in ExtKeys) and ((lParam and fExtended) = fExtended)) then try if IsChild(Handle, hWnd) then { handles all browser related messages } begin with {$IFDEF VER120}Application_{$ELSE}Application{$ENDIF} as IOleInPlaceActiveObject do Handled := TranslateAccelerator(Msg) = S_OK; if not Handled then begin Handled := True; TranslateMessage(Msg); DispatchMessage(Msg); end; end; except end; end; // MessageHandler
------------------------------------------------------------------------------------11、设置TWebBrowser的编码 为什么我总是错过很多机会?其实早就该想到的,但是一念之差,便即天壤之别。当时我要是肯再多考虑一下,多试验一下,这就不会排到第11条了。下面给出一个函数,搞定,难以想象的简单。 procedure SetCharSet(AWebBrowser: TWebBrowser; ACharSet: String); var RefreshLevel: OleVariant; Begin IHTMLDocument2(AWebBrowser.Document).Set_CharSet(ACharSet); RefreshLevel :=7; //这个7应该从注册表来,帮助有Bug。 AWebBrowser.Refresh2(RefreshLevel); End;
delphi WebBrowser控件实现对IE浏览器的各种控制2010-07-19 21:24在不是js打开的页面上按window.close(),会有提示框,很烦,现在可以不用了,没有提示框直接关闭窗口。试试下面代码:<object id="WebBrowser" width=0 height=0 classid="CLSID:8856F961_340A_11D0_A96B_00C04FD705A2">
</object>
<input type="button" name="Button" value="关闭窗口" onClick="document.all.WebBrowser.ExecWB(45,1)">试着改变参数会得到其他一些功能:WebBrowser.ExecWB(1,1) 打开
WebBrowser.ExecWB(2,1) 关闭现在所有的IE窗口,并打开一个新窗口
WebBrowser.ExecWB(4,1) 保存网页
WebBrowser.ExecWB(6,1) 打印
WebBrowser.ExecWB(7,1) 打印预览
WebBrowser.ExecWB(8,1) 打印页面设置
WebBrowser.ExecWB(10,1) 查看页面属性
WebBrowser.ExecWB(15,1) 好像是撤销,有待确认
WebBrowser.ExecWB(17,1) 全选
WebBrowser.ExecWB(22,1) 刷新
WebBrowser.ExecWB(45,1) 关闭窗体无提示这些只对IE5.5以上版本有效,我是在IE6下测试的,通过的。代码:--------------------------------------------------------------------------------■打开■
<input name=Button onClick=document.all.WebBrowser.ExecWB(1,1) type=button value=打开>
<OBJECT classid=CLSID:8856F961_340A_11D0_A96B_00C04FD705A2 height=0 id=WebBrowser width=0></OBJECT>
■另存为■
<input name=Button onClick=document.all.WebBrowser.ExecWB(4,1) type=button value=另存为><OBJECT classid=CLSID:8856F961_340A_11D0_A96B_00C04FD705A2 height=0 id=WebBrowser width=0></OBJECT>
■属性■
<input name=Button onClick=document.all.WebBrowser.ExecWB(10,1) type=button value=属性><OBJECT classid=CLSID:8856F961_340A_11D0_A96B_00C04FD705A2 height=0 id=WebBrowser width=0></OBJECT>
■打印■
<input name=Button onClick=document.all.WebBrowser.ExecWB(6,1) type=button value=打印><OBJECT classid=CLSID:8856F961_340A_11D0_A96B_00C04FD705A2 height=0 id=WebBrowser width=0></OBJECT>
■页面设置■
<input name=Button onClick=document.all.WebBrowser.ExecWB(8,1) type=button value=页面设置><OBJECT classid=CLSID:8856F961_340A_11D0_A96B_00C04FD705A2 height=0 id=WebBrowser width=0></OBJECT>
■刷新■
<input type=button value=刷新 name=refresh onclick="window.location.reload()">
■导入收藏■
<input type="button" name="Button" value="导入收藏夹" onClick=window.external.ImportExportFavorites(true,);>
■导出收藏■
<input type="button" name="Button3" value="导出收藏夹" onClick=window.external.ImportExportFavorites(false,);>
■加入收藏■
<INPUT name=Button2 onclick="window.external.AddFavorite(location.href, document.title)" type=button value=加入收藏夹>
■整理收藏夹■
<INPUT name=Submit2 onclick="window.external.ShowBrowserUI(OrganizeFavorites, null)" type=button value=整理收藏夹>
■查看原文件■
<INPUT name=Button onclick=window.location = "view-source:" + window.location.href type=button value=查看源文件>
■语言设置■
<INPUT name=Button onclick="window.external.ShowBrowserUI(LanguageDialog, null)" type=button value=语言设置>
■前进■
<INPUT name=Submit onclick=history.go(1) type=submit value=前进>
■后退■
opener.document.all.id.value = …………也许你没明白我的意思,也许我没说清楚。