EmptyParam是一个公用的Variant空变量,不要对它赋值 var t:OleVariant; begin webbrowser1.Navigate(edit1.text,t,t,t,t); {这句可以写成webbrowser1.Navigate(edit1.text,EmptyParam,EmptyParam,EmptyParam,EmptyParam);} end;
3.命令操作
常用的命令操作用ExecWB方法即可完成 procedure ExecWB(cmdID: OLECMDID; cmdexecopt: OLECMDEXECOPT); overload; procedure ExecWB(cmdID: OLECMDID; cmdexecopt: OLECMDEXECOPT; var pvaIn: OleVariant); overload; procedure ExecWB(cmdID: rOLECMDID; cmdexecopt: OLECMDEXECOPT; var pvaIn: OleVariant; var pvaOut: OleVariant); overload;
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;
方法一: if WebBrowser1.Document <> nil then IHTMLWindow2(IHTMLDocument2(WebBrowser1.Document).ParentWindow).focus;
方法二: procedure TForm1.SetFocusToDoc; begin if WebBrowser1.Document <> nil then with WebBrowser1.Application as IOleobject do DoVerb(OLEIVERB_UIACTIVATE, nil, WebBrowser1, 0, Handle, GetClientRect); end;
B.在OnMessage事件中将接受到的键盘消息传递给WebBrowser。 procedure TMainForm.ApplicationEvents1Message(var Msg: TagMSG; 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 (GetKeyState(VK_Control) < 0) or (wParam in ExtKeys) and ((lParam and fExtended) = fExtended)) then try if IsChild((PCT.ActivePage as TTabs).FMIE.handle, hWnd) then begin with (PCT.ActivePage as TTabs).FMIE.Application as IOleInPlaceActiveObject do Handled := (TranslateAccelerator(Msg) = S_OK); if not Handled then begin Handled := True; TranslateMessage(Msg); DispatchMessage(Msg); end; end; except end; end;
var Doc: IHtmlDocument2; TxtRange: IHtmlTxtRange; begin Doc :=WebBrowser1.Document as IHtmlDocument2; webbrowser1.ExecWB(OLECMDID_SELECTALL,OLECMDEXECOPT_DODEFAULT); TxtRange :=Doc.Selection.CreateRange as IHtmlTxtRange; TxtRange.findText('Text to be searched',0,0); TxtRange.Select; end;
9.提取网页中所有链接
procedure TMainForm.GetLinks; var doc: IHTMLDocument2; all: IHTMLElementCollection; len, i: Integer; item: OleVariant; begin FormLink.lstLink.Clear; try doc := ((PCT.ActivePage as TTabs).FMIE.Document) as IHTMLDocument2; except Exit; end; all := doc.Links; len := all.Length; for i := 0 to len - 1 do begin item := all.item(i, varempty); //EmpryParam亦可 FormLink.lstLink.Items.Add(item); end; FormLink.Show; end;
10.设置TWebBrowser的编码
procedure SetCharSet(AWebBrowser: TWebBrowser; ACharSet:String); var RefreshLevel: OleVariant; Begin IHTMLDocument2(AWebBrowser.Document).Set_CharSet(ACharSet); RefreshLevel :=7; //这个7应该从注册表来,帮助有Bug。 AWebBrowser.Refresh2(RefreshLevel); End;
11.转入IHTMLDocument接口
uses mshtml; var doc:IHTMLDocument2; doc:=webbrowser1.document as IHTMLDocument2;