多语言展示
当前在线:245今日阅读:167今日分享:16

delp如何载入网页

下面是一些常用功能的事例代码.1. 打开某个页面:web.Navigate(ExtractFilePath(Application.ExeName) + 'Template/login.html');2. 取出页面中某个HtmlElement的Value属性值:function GetValueByElementName(web: TWebBrowser; elementName: string; index: integer): string;beginresult := (((web.Document as IHTMLDocument2).body.all asIHTMLElementCollection).item(elementName, index) as IHTMLInputElement).valueend;3. 给HtmlElement设置Value属性procedure SetValueTextAreaName(web: TWebBrowser; elementName, value: string;index: integer);begin(((web.Document as IHTMLDocument2).body.all asIHTMLElementCollection).item(elementName, index) as IHTMLTextAreaElement ).value := value;end;
方法/步骤
1

4. 判断页面执行结果是否成功因为Web应用中如果出错的一般是采用错误页面的方式呈现给最终用户,所以我们也无法抓到Http错误,只能通过在webBeforeNavigate2事件中将URL参数记录到全局变量中, 然后在webDocumentComplete事件中根据URL参数和全局变量中的URL参数来判断执行结果是否正确.当然,这样需要将页面地址编码到代码中,降低了灵活性,但是这也是我能想到的唯一的方法,如果大家有什么好的方法,请告诉我哦.5. 屏蔽鼠标右键和某些快捷键本功能需要在webBrowser的窗口中加入ApplicationEvents组件,设置它的OnMessage事件代码如下即可.procedure TwebAdapterForm.ApplicationEvents1Message(var Msg: tagMSG;var Handled: Boolean);const_KeyPressMask = $80000000;begin//禁用右键with Msg dobeginif not IsChild(web.Handle, hWnd) then Exit;Handled := (message = WM_RBUTTONDOWN) or (message = WM_RBUTTONUP) or (message = WM_CONTEXTMENU);end;//禁止Ctrl + N//禁止Ctrl + F//禁止Ctrl + Aif Msg.message = WM_KEYDOWN thenbeginif ((Msg.lParam and _KeyPressMask) = 0) and(GetKeyState(VK_Control) <0) and ((Msg.wParam = Ord('N'))or (Msg.wParam = Ord('F')) or (Msg.wParam = Ord('A'))) thenbeginHandled := True;end;end;end;

2

6. 在页面关闭的时候,同时关掉包含页面的VCL Form.(仅限 InternetExplorer 6.0) 本功能需要卸载掉Delphi自带的 WebBrowser组件,安装ActionX组件(Microsoft Internet Controls V1.1),而且以后的程序只能运行在安装有Internet Explorer 6.0 的环境下.具体方法如下:在WebBrowser组件的OnWindowClosing事件中,输入self.close; 代码即可.如果需要阻止窗口的关闭, 设置CanClose参数的值为Flase.7. 如何将页面中超链接新开的页面窗口包到指定的VCL窗口中.procedure TForm1.webNewWindow2(Sender: TObject; var ppDisp: IDispatch;var Cancel: WordBool);varform : TForm1;beginform := TForm1.Create(nil);ppDisp := form.web.DefaultDispatch;form.Show;end;

3

8. 在WebBrowser加载html页面完成后,在页面顶端插入HTML代码, 下面两种方式斗可以.{1. ----------------------------------------------------------------}procedure TForm1.Button1Click(Sender: TObject);varRange: IHTMLTxtRange;beginRange := ((WebBrowser1.Document as IHTMLDocument2).body asIHTMLBodyElement).createTextRange;Range.collapse(False);Range.pasteHTML('
Hello!');end;{2. ----------------------------------------------------------------}procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject;const pDisp: IDispatch; var URL: OleVariant);varWebDoc: HTMLDocument;WebBody: HTMLBody;beginWebDoc := WebBrowser1.Document as HTMLDocument;WebBody := WebDoc.body as HTMLBody;WebBody.insertAdjacentHTML('BeforeEnd', '

Hello World!

');end;

4

9. 将页面中显示的内容全部选中,然后粘贴到Word文档中.WebBrowser1.ExecWB(OLECMDID_SELECTALL, OLECMDEXECOPT_DODEFAULT);//全选网页  WebBrowser1.ExecWB(OLECMDID_COPY, OLECMDEXECOPT_DODEFAULT); //复制网页WordDocu.Range.Paste; //word文档粘贴WebBrowser1.ExecWB(OLECMDID_UNDO, OLECMDEXECOPT_DODEFAULT); //取消全选注:WebBrowser的Document属性值和WordDocument的Document属性值必须都不为nil.10. 如何解决网页不响应回车事件public{ Public declarations }procedure MsgHandle(var Msg :TMsg; var Handled :Boolean);end;varForm1: TForm1;FOleInPlaceActiveObject :IOleInPlaceActiveObject;implementation{$R *.DFM}procedure TForm1.MsgHandle(var Msg :TMsg; var Handled :Boolean);variOIPAO :IOleInPlaceActiveObject;Dispatch :IDispatch;beginif WebBrowser1 =nil thenbeginHandled :=False;Exit;end;Handled :=(IsDialogMessage(WebBrowser1.Handle, Msg) =True);if (Handled) and (not WebBrowser1.Busy) thenbeginif FOleInPlaceActiveObject =nil thenbeginDispatch :=WebBrowser1.Application;if Dispatch <>nil thenbeginDispatch.QueryInterface(IOleInPlaceActiveObject, iOIPAO);if iOIPAO <>nil thenFOleInPlaceActiveObject :=iOIPAO;end;end;end;if FOleInPlaceActiveObject <>nil thenif ((Msg.message =WM_KEYDOWN) or (Msg.Message =WM_KEYUP)) and ((Msg.wParam =VK_BACK) or (Msg.wParam =VK_LEFT) or (Msg.wParam =VK_RIGHT)) thenelseFOleInPlaceActiveObject.TranslateAccelerator(Msg);end;procedure TForm1.FormCreate(Sender: TObject);beginApplication.OnMessage :=MsgHandle;end;procedure TForm1.FormDestroy(Sender: TObject);beginFOleInPlaceActiveObject :=nil;end;

5

11. 如何在WebBrowser中调用当前页面中的javascript函数SayHello()WebBrowser1.OleObject.Document.parentWindow.execScript('SayHello()', 'javascript');//or(WebBrowser1.Document as IHTMLDocument2).parentWindow.execScript('SayHello()', 'javascript')//orwebrowser1.document.script.SayHello();delphi中Webbrowser的使用技巧1.获取网页中变量的值例如:htm中程序中可以这样调用 id := Form1.WebBrowser1.OleObject.Document.script.currIDS备注:变量可以是javascript定义的,也可以是vbscript定义的,如果Webbrowser1中找不到该变量,调用会触发一个异常事件,即变量currIDS不存在。2.执行网页中的函数tmpf := 'currID = getNextID(currID)'+#13#10;      Form1.WebBrowser1.OleObject.Document.parentWindow.execScript(tmpf,'JavaScript');调用函数的方法就是execScript接口,同样,如果函数不存在,或者运行错误也会触发脚本错误异常3.设置网页背景背景图片 WebBrowser1.OleObject.Document.body.background     := 'http://seelearn.com/bg.gif' ;背景颜色 WebBrowser1.OleObject.Document.body.bgcolor     := '#eeeeee'4.调用网页中已知对象src := WebBrowser1.OleObject.Document.getElementByID('img1').src该方法其实就是javascript中的 getElementByID5.获取页面中所有的frame

6

使用DHTML。frames:=wb.OleObject.document.frames;for i:=0 to frames.length domemo1.lines.Add(frames[i].document.body.innerHTML);6.BorderStyle=bsNone后Webbrowser会被重新初始化这是一个让人很意外的一个问题,Delphi在窗口控件的控制方面做得非常好,很少出现这种BUG根据分析,出现这个现象有很多情况 改变FormStyle也会出现 ; 如果     webbrowser.parent     由panel1     改到panel2.也会导致webbrowser重新初始化。7.直接向Webbrowser中写入html代码,不需要Navigate到实际存在的文件varStrStream:TStringStream;SetNoteStr: string;      beginSetNoteStr :='

点点博客 http://seelearn.com

';SetNoteStr :=SetNoteStr+'

点击左边按钮可查看对应图片

';StrStream:=TStringStream.Create(SetNoteStr);WebBrowser1.Navigate('about:blank');END

推荐信息