Summary of the use techniques of WebBrowser in Delphi

Source: Internet
Author: User
Tags ole

1> calls to known objects in Web pages

SRC: = WebBrowser1.OleObject.document.getElementByIdx (' id1′). SRC is actually a function of getElementById in JavaScript.

2> to get a variable value in a Web page

Code in HTML: <script> var userid=123</script> this call ID in the Delphi program: = The Form1.WebBrowser1.OleObject.Document.script.userID UserID variable can be either JavaScript-defined or VBScript-defined. If the variable is not found in Webbrowser1, the call triggers an exception event, where the variable userid does not exist

3> calling a function in a Web page

Srun: = ' UserID = Getnextid (userid) ' + #13 # #; Form1.WebBrowser1.OleObject.Document.parentWindow.execScript (Srun, ' JavaScript '); The method of calling a function is the Execscript interface. If the function does not exist, or if a run error triggers a script error exception

4> Get all the frames in the page

Gets an array of frame objects frames FRAMES:=WB. OleObject.document.frames; For i:=0 to Frames.length do Memo1.lines.Add (frames[i].document.body.innerhtml);

5> If there is an IFRAME in the page, how to tell if the page is completely downloaded

Note: The DocumentComplete event is triggered when each iframe is downloaded, so a page may be triggered many times before the actual download is complete! Procedure Tform1.webbrowser1documentcomplete (sender:tobject; const Pdisp:idispatch; var url:olevariant); Begin IF Webbrowser1.application = Pdisp then ShowMessage (' page has all been downloaded ') end;

the common properties and methods of 6>webbrowser are mainly

GoBack: Method, go back to the previous page. GoForward: Method, advance to the next page. GoHome: Method, call the default home page, the page is set in IE options. Gosearch: Method, call the default search page, the page is set in IE options. Navigate (const url:widestring; var Flags, Targetframename, PostData, Headers:olevariant): method, invokes the specified page, with the following specific parameters: URL: Specifies the URL of the page. Flags:word type, the effect is not clear, can be set to 0. Targetframename:widestring, opens the frame where the page is located, opens in the current frame as an empty string, and opens in the frame when the specified frame exists targetframename; Targetframename A new window opens when the specified frame does not exist, it is equivalent to invoking an external IE browser. Postdata:boolean, whether data is allowed to be sent. Headers:widestring, the header data to be sent for the URL request. Refresh: Method, refreshes the current page. Stop: Method that stops calling or opening the current page. LocationName: Property (widestring), the name of the current location. Locationurl: Property (widestring), the URL of the current location. Busy: Property (Boolean), whether it is busy. Visible: Property (Boolean), whether the browser window is visible. (The following properties are new in Twebbrowser, not in TWEBBROWSER_V1, their role remains to be explored.) StatusBar: Property (Boolean), whether the status bar is displayed. StatusText: Properties (widestring), status bar contents. ToolBar: Properties (sysint), contents of the toolbar. MenuBar: Property (Boolean), whether the menu bar is displayed. Fullscreen: Property (Boolean), whether it is displayed in full screen. Offline: Property (Boolean), whether to browse offline. Addressbar: Property (Boolean), whether the address bar is displayed. The common events of Twebbrowser include: Onstatustextchange = procedure (sender:tobject; Const text:wideString) of object; Occurs when the status bar prompts for information, the parameter text is the current status bar prompt information, we can update our own status bar information or other transactions according to this information. Onprogresschange = procedure (sender:tobject;   Progress, Progressmax:integer) of object; Occurs when the progress of the page is opened, the parameter progress is the current progress, Progressmax is the total progress, we can update our own status bar prompt information or handle other transactions according to these two parameters. Oncommandstatechange = procedure (sender:tobject; Command:integer;   Enable:wordbool) of object; Occurs when a new command is executed, the command is an identity of the commands, and enable is allowed to execute the command.   Ontitlechange = procedure (sender:tobject; const text:widestring) of object; Occurs when the title of the page changes, text is the current caption.   Onpropertychange = procedure (sender:tobject; const property_: widestring) of object; Occurs when the properties of a page change, Property_ is the property name ondownloadcomplete:tnotifyevent occurs after the download page finishes. Ondownloadbegin:tnotifyevent occurs before the download page begins.

7> displaying Dynamic HTML code in a WebBrowser control

Add the ActiveX var strstream:tstringstream in uses; setnotestr:string; Begin SETNOTESTR: = ' <body bgcolor=222222 align=center><br><p align=center><font size=+2 color=# ffffff> Google http://www.google.com</font></p> '; Setnotestr: =setnotestr+ ' <br><p align=center><font size=+2 color= #FFFFFF > Implement code </font></p> ' to display Dynamic HTML in the WebBrowser control; Strstream:=tstringstream.create (SETNOTESTR); WebBrowser1.Navigate (' About:blank '); Try Strstream.position:=0; (Webbrowser1.document as IPersistStreamInit). Load (Tstreamadapter.create (Strstream)); Finally Strstream.free; End End

8> Other

Initialization and finalization (initialization & finalization)

You may have encountered an error such as "attempting to activate an unregistered lost target" or "OLE Object not registered" in a Twebbrowser method to perform the desired operation, such as EXECWB, etc., or there is no error but no desired result. For example, you cannot copy the selected page contents to the Clipboard. When I used to program it, I found that EXECWB sometimes works but sometimes not, adding twebbrowser to the default Project main window generated by Delphi, and running without an "OLE Object not registered" error. It is also an accidental opportunity for me to know that OLE objects need to be initialized and terminated (there is too little to know).

Initialization oleinitialize (nil);

Finalization

Try

OleUninitialize;

Except

End

How to remove the scroll bar: Core code: webbrowser1.oleobject.document.body.scroll:= ' No '; Using this code to remove the scrollbar is the premise that the WebBrowser must have an open Web page, that is, after the page has finished loading and then remove the scroll bar. So first of all to determine whether the page is loaded, if the load is complete, execute the above statement to remove the scroll bar.

The first step: Place a flag in the Webbrowser1documentcomplete event Tag:=1 (the delegate is loaded) code as follows:

Procedure Tform1.webbrowser1documentcomplete (Sender:tobject;

Const Pdisp:idispatch;

var url:olevariant);

Begin

Tag:=1; Remove the flag from the Webbrowser1 scroll bar

End

Step Two:

Procedure Tform1.speedbutton1click (Sender:tobject);

var Doc:ihtmldocument2;

Begin

Tag: = 0; Remove the flag from the Webbrowser1 scroll bar

Webbrowser1.navigate2 (' http://www.163.com ');

while (tag=0)

Do application.processmessages;

WebBrowser1.oleobject.Document.body.Scroll: = ' no ';

End

Note: The mshtml must be added to the uses before use;

==========================//How do I turn the page after I remove the scroll bar? Use the following code

var Doc:ihtmldocument2;

Begin

Doc: =webbrowser1.document as IHTMLDocument2;

Doc.Get_ParentWindow.Scroll (x, y);

End ^^ ^ where you want to scroll

WebBrowser do not eject the error box

Set this property: Webbrowser1.silent: =true

Allow links in WebBrowser to open in their own window when clicked

To set the code in the WebBrowser NewWindow2 event:

Procedure Tform1.webbrowsernewwindow2 (Sender:tobject; var ppdisp:idispatch;

var cancel:wordbool);

Begin//Open the new window to itself

Ppdisp: = webbrowser.application;

End

Screen Webbrower Right-click menu

Put a applicationevents control, set the following code in the ApplicationEvents event OnMessage: (ApplicationEvents control is found on the Additional tab in Delphi)

Procedure Tform1.applicationevents1message (Var msg:tagmsg; var handled:boolean);

Begin//Screen page right-click

If msg.message = Wm_rbuttondown Then

Begin

If you remove the following line is the screen right-click menu, now for the custom right-click menu//

Popupmenu1. Popup (mouse.cursorpos.x, MOUSE.CURSORPOS.Y);

Handled: = True;

End

End

Transferred from: http://julyzergcn.iteye.com/blog/1462417

Summary of the use techniques of WebBrowser in Delphi

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.