Delphi in WebBrowser

Source: Internet
Author: User

1. Get the value of the variable in the page

HTM in <script> var currid=123</script>

You can call this in the program ID: = Form1.WebBrowser1.OleObject.Document.script.currID

It is worth noting that the variable can be either JavaScript-defined or VBScript-defined, and if the variable is not found in Webbrowser1, the call triggers an exception event, that is, the variable Currid does not exist

2. Perform functions in the Web page

TMP: = ' Currid = Getnextid (currid) ' + #13 # #;
Form1.WebBrowser1.OleObject.Document.parentWindow.execScript (tmp, ' JavaScript ');

The method of calling the function is the Execscript interface, again, if the function does not exist, or if a run error triggers a script error exception

3. Setting the page background

Background image WebBrowser1.OleObject.Document.body.background: = ' http://seelearn.com/bg.gif '

Background color WebBrowser1.OleObject.Document.body.bgcolor: = ' #eeeeee '

4. Calling a known object in a Web page

SRC: = WebBrowser1.OleObject.Document.getElementByID (' img1 '). src

This method is actually a getElementById in JavaScript.

5. Get all the frames in the page

Use DHTML.
FRAMES:=WB. OleObject.document.frames;
For i:=0 to Frames.length do
MEMO1.LINES.ADD (frames[i].document.body.innerhtml);

WebBrowser will be re-initialized after 6.borderstyle=bsnone

This is a very surprising problem, Delphi in the control of the window control is very good, very few such bugs

According to the analysis, there are many changes in the phenomenon of Formstyle will also appear; If the webbrowser.parent is changed from Panel1 to Panel2, it will also cause WebBrowser to re-
Initialization

7. Write HTML code directly to WebBrowser, no need to navigate to the actual existing file

Var
Strstream:tstringstream;
setnotestr:string;

Begin

Setnotestr: = ' <body bgcolor=222222 align=center><br><p align=center><font size=+2 color= #FFFFFF > Dot blog http://seelearn.com</font></p>; ';
Setnotestr: =setnotestr+ ' <br><p align=center><font size=+2 color= #FFFFFF > Click the Left button to view the corresponding picture </font ></p> ';
Strstream:=tstringstream.create (SETNOTESTR);
WebBrowser1.Navigate (' About:blank ');
Try
strstream.position:=0;
(Webbrowser1.document as IPersistStreamInit). Load (Tstreamadapter.create (Strstream));
Finally
Strstream.free;
End

8. Forward, rewind, refresh

Self. Webbrowser1.goback

Self. Webbrowser1.goforward

Self. Webbrowser1.refresh

9. Capturing the NewWindow2 event, that is, the new open window event

Procedure Tform1.webbrowser1newwindow2 (Sender:tobject;
var Ppdisp:idispatch; var cancel:wordbool);
Var
Newwindow:tform2;
Begin
Exit
newwindow:= tform2.create (nil);
Newwindow.show;
Ppdisp:= NewWindow.Webbrowser1.DefaultDispatch;
End

It is worth the problem is that the method can not get the new window URL, step back only to wait until the new WebBrowser trigger BeforeNavigate2 event to judge the

10. When an IFRAME exists in the Web page, determine if the page is downloaded.

Procedure Tform1.webbrowser1documentcomplete (Sender:tobject;
Const Pdisp:idispatch; var url:olevariant);
Begin

If webbrowser1.application = Pdisp then ShowMessage (' page has been fully downloaded ')

End

Note that each IFRAME is downloaded to trigger the DocumentComplete event, so a page may be triggered multiple times before the actual download is complete




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


Delphi in WebBrowser

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.