- WebBrowser1.GoHome;
- WebBrowser1.Refresh;
- WebBrowser1.GoBack;
- WebBrowser1.GoForward;
- WebBrowser1.Navigate('...');
- WebBrowser1.Navigate('about:blank');
-
- WebBrowser1.Navigate('about:<head><title>标题</title><body>页面内容</body>');
-
- procedure TForm1.Button1Click(Sender: TObject);
- var
- s: string;
- i: Integer;
- begin
- s := WebBrowser1.OleObject.document.Script.str;
- i := WebBrowser1.OleObject.document.Script.num;
- ShowMessage(s);
- ShowMessage(IntToStr(i));
-
- s := WebBrowser1.OleObject.document.parentWindow.str;
- i := WebBrowser1.OleObject.document.parentWindow.num;
- ShowMessage(s);
- ShowMessage(IntToStr(i));
- end;
-
- procedure TForm1.Button1Click(Sender: TObject);
- begin
- WebBrowser1.OleObject.document.parentWindow.MB();
-
- WebBrowser1.OleObject.document.parentWindow.execScript('MB()','JavaScript');
- end;
-
-
- procedure TForm1.WebBrowser1DocumentComplete(ASender: TObject;
- const pDisp: IDispatch; var URL: OleVariant);
- begin
- if not(<span style="font-family: Arial, Helvetica, sans-serif;">WebBrowser1</span>.Busy) then
- ShowMessage('下载完毕');
- end;
-
- WebBrowser1.OleObject.document.body.bgcolor := '#FF0000';
- WebBrowser1.OleObject.document.body.background := '...图片地址';
- var
- s: string;
- begin
- s := WebBrowser1.OleObject.document.getElementByIdx_x('span1').innerText;
- ShowMessage(s);
-
- s := WebBrowser1.OleObject.document.parentWindow.span1.innerText;
- ShowMessage(s);
-
- WebBrowser1.OleObject.document.parentWindow.span1.style.display := 'none';
- end;
- var
- s: string;
- begin
- s := WebBrowser1.OleObject.document.body.innerHTML;
- s := WebBrowser1.OleObject.document.body.outerHTML;
- s := WebBrowser1.OleObject.document.documentElement.innerHTML;
- end;
- </pre><pre name="code" class="delphi"><span style="color: rgb(73, 73, 73); font-family: simsun; font-size: 14px; line-height: 21px;">Memo1.Lines.Add(IHtmlDocument2(WebBrowser1.Document).Body.OuterHtml);</span>
-
- uses ActiveX;
- var
- ms: TMemoryStream;
- begin
- if not Assigned(WebBrowser1.Document) then Exit;
- ms := TMemoryStream.Create;
- (WebBrowser1.Document as IPersistStreamInit).Save(TStreamAdapter.Create(ms), True);
- ms.Position := 0;
- Memo1.Lines.LoadFromStream(ms, TEncoding.UTF8);
- ms.Free;
- end;
-
-
- procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean);
- begin
- with Msg do
- begin
- if not IsChild(WebBrowser1.Handle, hWnd) then Exit;
- Handled := (message = WM_RBUTTONDOWN) or (message = WM_RBUTTONUP) or (message = WM_CONTEXTMENU);
- end;
- end;
-
- procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean);
- var mPoint: TPoint;
- begin
- if IsChild(WebBrowser1.Handle, Msg.Hwnd) and
- ((Msg.Message = WM_RBUTTONDOWN) or (Msg.Message = WM_RBUTTONUP)) then
- begin
- GetCursorPos(mPoint);
- PopupMenu1.Popup(mPoint.X, mPoint.Y);
- Handled:=True;
- end;
- end;
- begin
- WebBrowser1.Navigate('about:blank');
- WebBrowser1.OleObject.Document.Writeln('ok');
- end;
-
- var
- ms: TMemoryStream;
- begin
- ms := TMemoryStream.Create;
- Memo1.Lines.SaveToStream(ms);
- ms.Position := 0;
- (WebBrowser1.Document as IPersistStreamInit).Load(TStreamAdapter.Create(ms));
- ms.Free;
- end;
-
- procedure TForm1.WebBrowser1NavigateComplete2(ASender: TObject;
- const pDisp: IDispatch; var URL: OleVariant);
- begin
- WebBrowser1.Silent := True;
- end;
- </pre><pre name="code" class="delphi">方法二
- TWebBrowser的OnDownloadComplete事件里面执行
- (WebBrowser1.Document as IHTMLDocument2).parentWindow.execScript('window.onerror=function(){return true}','JavaScript');
-
- </pre><pre name="code" class="delphi">
- procedure TForm1.WebBrowser1NewWindow2(ASender: TObject; var ppDisp: IDispatch;
- var Cancel: WordBool);
- begin
- Cancel := True;
- end;
- </pre><pre name="code" class="delphi">
- TWebBrowser非常特殊,它从TWinControl继承来的SetFocus方法并不能使得它所twebbrows setfocu 方法包含的文档获得焦点,从而不能立即使用Internet Explorer本身具有得internet explor 使用快捷键,解决方法如下:<
- 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
- </pre><pre name="code" class="delphi">
- 查找对话框可以在文档获得焦点的时候通过按键Ctrl-F对话框焦点按键来调出,程序中则调用IOleCommandTarget对象的成员函数Exec执行OLECMDID_FIND操作olecmdid 操作执行来调用,下面给出的方法是如何在程序中用代码来做出文下面方法如何字选择,即你可以自己设计查找对话框。
- Var
- Doc: IHtmlDocument2;
- TxtRange: IHtmlTxtRange;
- begin
- Doc :=WebBrowser1.Document as IHtmlDocument2;
- Doc.SelectAll;
-
-
-
- TxtRange :=Doc.Selection.CreateRange as IHtmlTxtRange;
- TxtRange.FindText('Text to be searched',0.0);
- TxtRange.Select;
- end;
- 还有,从Txt.Get_text可以得到当前选中的文字内容,某些得到文字当前时候是有用的。
-
- </pre><pre name="code" class="delphi">
- 如同程序里每个窗体上有一个“缺省”按钮一样,Web一样按钮每个页面上的每个Form也有一个“缺省”按钮——即属性为“Submitsubmit form 按钮”的按钮,当用户按下回车键时就相当于鼠标单击了“Submitsubmit 回车键相当”。但是TWebBrowser似乎并不响应回车键,并且,即使把包含TWebBrowser的twebbrows 回车键似乎窗体的KeyPreview设为True,在窗体的KeyPress事件里还是不能截获用户向keypreview keypress 事件TWebBrowser发出的按键。
- 我的解决办法是用ApplicatinEvents构件或者自己编写Tapplication对象的OnMessage事onmessag tapplic 构件件,在其中判断消息类型,对键盘消息做出响应。至于点至于响应判断击“提交”按钮,可以通过分析网页源代码的方法来实现源代码网页方法,不过我找到了更为简单快捷的方法,有两种,第一种是更为不过方法我自己想出来的,另一种是别人写的代码,这里都提供给自己这里出来大家,以做参考。
- A、用SendKeys函数向WebBrowser发送回车键
- 在Delphi5光盘上的Info/Extras/SendKeys目录下有一个SndKey32.pas文件,sendkei delphi sndkei其中包含了两个函数SendKeys和AppActivate,我们可以用SendKeys函数来向WebBrowser发appactiv webbrows sendkei送回车键,我现在用的就是这个方法,使用很简单,在WebBrowserwebbrows 回车键使用获得焦点的情况下(不要求WebBrowser所包含的文档获得焦点),webbrows 焦点包含用一条语句即可:
- Sendkeys('~',true);
- SendKeys函数的详细参数说明等,均包含在SndKey32.pas文件中sendkei sndkei 参数。
- B、在OnMessage事件中将接受到的键盘消息传递给WebBrowser。
- Procedure TForm1.ApplicationEvents1Message(var Msg: TMsg; var Handled: Boolean);
-
- const
- StdKeys = [VK_TAB, VK_RETURN];
- ExtKeys = [VK_DELETE, VK_BACK, VK_LEFT, VK_RIGHT];
- fExtended = $01000000;
- 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
- 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;
- </pre><pre name="code" class="delphi">
- </pre><pre name="code" class="delphi">10、提取网页中所有链接
- 这个方法来自大富翁论坛hopfield朋友的对一个问题的回答hopfield 自大问题,我本想自己试验,但总是没成功。
- Var
- doc:IHTMLDocument2;
- all:IHTMLElementCollection;
- len,I:integer;
- item:OleVariant;
- begin
- doc:=WebBrowser1 .Document as IHTMLDocument2;
- all:=doc.Get_links;
- len:=all.length;
- for I:=0 to len-1 do begin
- item:=all.item(I,varempty);
- memo1.lines.add(item.href);
- end;
- end;
- -----------------------------------------------------------------------------------
- 11、设置TWebBrowser的编码
- 为什么我总是错过很多机会?其实早就该想到的,但为什么错过想到是一念之差,便即天壤之别。当时我要是肯再多考虑一下一念之差天壤之别当时,多试验一下,这就不会排到第11条了。下面给出一个下面试验一个函数,搞定,难以想象的简单。
- Procedure SetCharSet(AWebBrowser: TWebBrowser; ACharSet: String);
- var
- RefreshLevel: OleVariant;
- Begin
- IHTMLDocument2(AWebBrowser.Document).Set_CharSet(ACharSet);
- RefreshLevel :=7;
- AWebBrowser.Refresh2(RefreshLevel);
- End;
-
- </pre><pre name="code" class="delphi">9.将页面中显示的内容全部选中,然后粘贴到Word文档中.
- WebBrowser1.ExecWB(OLECMDID_SELECTALL, OLECMDEXECOPT_DODEFAULT);
- WebBrowser1.ExecWB(OLECMDID_COPY, OLECMDEXECOPT_DODEFAULT);
- WordDocu.Range.Paste;
- WebBrowser1.ExecWB(OLECMDID_UNDO, OLECMDEXECOPT_DODEFAULT);
- 注:WebBrowser的Document属性值和WordDocument的Document属性值必须都不为nil.
- 10.如何解决网页不响应回车事件
- public
-
- procedure MsgHandle(var Msg :TMsg; var Handled :Boolean);
- end;
- var
- Form1: TForm1;
- FOleInPlaceActiveObject :IOleInPlaceActiveObject;
- implementation
- {$R *.DFM}
- procedure TForm1.MsgHandle(var Msg :TMsg; var Handled :Boolean);
- var
- iOIPAO :IOleInPlaceActiveObject;
- Dispatch :IDispatch;
- begin
- if WebBrowser1 =nil then
- begin
- Handled :=False;
- Exit;
- end;
- Handled :=(IsDialogMessage(WebBrowser1.Handle, Msg) =True);
- if (Handled) and (not WebBrowser1.Busy) then
- begin
- if FOleInPlaceActiveObject =nil then
- begin
- Dispatch :=WebBrowser1.Application;
- if Dispatch <>nil then
- begin
- Dispatch.QueryInterface(IOleInPlaceActiveObject, iOIPAO);
- if iOIPAO <>nil then
- FOleInPlaceActiveObject :=iOIPAO;
- end;
- end;
- end;
- if FOleInPlaceActiveObject <>nil then
- if ((Msg.message =WM_KEYDOWN) or (Msg.Message =WM_KEYUP)) and ((Msg.wParam =VK_BACK) or (Msg.wParam =VK_LEFT) or (Msg.wParam =VK_RIGHT)) then
- else
- FOleInPlaceActiveObject.TranslateAccelerator(Msg);
- end;
- procedure TForm1.FormCreate(Sender: TObject);
- begin
- Application.OnMessage :=MsgHandle;
- end;
- procedure TForm1.FormDestroy(Sender: TObject);
- begin
- FOleInPlaceActiveObject :=nil;
- end;
-
- </pre><pre name="code" class="delphi">
- procedure TForm1.Button1Click(Sender: TObject);
- var
- Range: IHTMLTxtRange;
- begin
- Range := ((WebBrowser1.Document as IHTMLDocument2).body as
- IHTMLBodyElement).createTextRange;
- Range.collapse(False);
- Range.pasteHTML('<br/><b>Hello!</b>');
- end;
- <p>
-
-
-
- </p>