Webbrowser in Delphi

Source: Internet
Author: User
1. Obtain the <SCRIPT> var currid = 123 </SCRIPT> Program You can call ID: = form1.webbrowser1. oleobject. document. script. currid is worth noting that the variable can be defined by JavaScript or VBScript. If this variable cannot be found in webbrowser1, the call will trigger an exception event, that is, the variable currid does not exist.

 

2. Execute the function TMP: = 'currid = getnextid (currid) '+ #13 #10;
Form1.webbrowser1.oleobject.document.parentwindow.exe cscript (TMP, 'javascript '); the method to call a function is the execScript interface. Similarly, if the function does not exist or a running error occurs, a script error exception is triggered.

 

3. Set the webpage background image webbrowser1.oleobject. Document. Body. Background: = 'HTTP: // seelearn.com/bg.gif' background color webbrowser1.oleobject. Document. Body. bgcolor: = '# eeeeeeee'

 

4. Call the known object SRC: = webbrowser1.oleobject. Document. getelementbyid ('img1'). SRC on the webpage. This method is actually getelementbyid in JavaScript.

 

5. Obtain all frames on the page using DHTML.
Frames: Invalid wb.oleobject.doc ument. frames;
For I: = 0 to frames. Length do
Memo1.lines.add(framespolici}.doc ument. Body. innerhtml); 6. After borderstyle = bsnone Webbrowser It is a very unexpected issue that will be reinitialized. Delphi has done a very good job in the control of window controls, and seldom encountered such bugs based on analysis, in many cases, formstyle is changed. If Webbrowser Changing. Parent from Panel1 to panel2. Webbrowser Repeat
Initialization.

 

7. Direct Webbrowser Write HTML Code , 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> click blog http://seelearn.com </font> </ p> ';
Setnotestr: = setnotestr + '<br> <p align = center> <font size = + 2 color = # ffffff> click the button on the left to view the corresponding image </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 and backward. Refresh self. webbrowser1.goback self. webbrowser1.goforward self. webbrowser1.refresh

 

9. Capture the newwindow2 event, that is, the new 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.defadisdispatch;
End; it is worth noting that this method cannot obtain the URL of the newly opened window. The method to take a step back can only wait until the new Webbrowser Beforenavigate2 event triggered in

 

10. If an IFRAME exists in the webpage, determine whether the page has been downloaded. Procedure tform1.webbrowser1documentcomplete (Sender: tobject;
Const Pdisp: idispatch; var URL: olevariant );
Begin if webbrowser1.application = Pdisp then showmessage ('Download all completed on the page') end; Description: after each IFRAME download is completed, the documentcomplete event is triggered, therefore, a page may be triggered multiple times before the download is completed.

Initialization & finalization)

You are executing a method of twebbrowser to perform the expected operation, for example, execwb may encounter errors such as "trying to activate unregistered lost targets" or "OLE object not registered", or no errors but no expected results, for example, you cannot copy the selected webpage content to the clipboard. When I used it for programming, I found that execwb sometimes works but sometimes does not work. I added twebbrowser to the default Project Main Window generated by Delphi, the "OLE object not registered" error does not occur during the runtime. It is also an accidental opportunity to know that the OLE object needs initialization and termination (there are too few things to understand ).

Initialization oleinitialize (NiL );

Finalization

Try

Oleuninitialize;

Except

End;

 

How to remove the scroll bar: Core code: webbrowser1.oleobject. Document. Body. Scroll: = 'no'; the premise of removing the scroll bar using this code isWebbrowserThe page must be opened, that is, remove the scroll bar after the page is loaded. Therefore, you must first determine whether the page has been loaded. If the page has been loaded, execute the preceding statement to remove the scroll bar.

Step 1: Set a tag: = 1 (indicating the loading is completed) in the webbrowser1documentcomplete event as follows:

Procedure tform1.webbrowser1documentcomplete (Sender: tobject;

Const Pdisp: idispatch;

VaR URL: olevariant );

Begin

Tag: = 1; // remove the webbrowser1 scroll bar flag

End;

Step 2:

Procedure tform1.speedbutton1click (Sender: tobject );

VaR DOC: ihtmldocument2;

Begin

Tag: = 0; // remove the webbrowser1 scroll bar flag

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

While (TAG = 0)

Do application. processmessages;

Webbrowser1.oleobject. Document. Body. Scroll: = 'no ';

End;

Note: before use, you must add mshtml to uses;

================================================= How do I flip the page after removing the scroll bar? Use the following code

VaR DOC: ihtmldocument2;

Begin

DOC: = webbrowser1.document as ihtmldocument2;

Doc. get_parent1_1_scroll (x, y );

End; ^ the position where you want to scroll

 

WebbrowserNo error prompt box is displayed.

Set this attribute: webbrowser1.silent: = true

 

LetWebbrowserWhen the link in is clicked, it is opened in its own window

InWebbrowserIn the newwindow2 event:

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

VaR cancel: wordbool );

Begin // open the new window

Ppdisp: =Webbrowser. Application;

End;

 

Shield webbrower right-click menu

Put an applicationevents control and set the following code in the event onmessage of applicationevents: (The applicationevents control is found on the additional tab in Delphi)

Procedure tform1.applicationevents1message (var msg: tagmsg; var handled: Boolean );

Begin // right-click the webpage

If MSG. Message = wm_rbuttondown then

Begin

// If the following line is removed, the right-click menu is blocked and the custom right-click menu is now displayed //

Popupmenu1.popup (mouse. cursorpos. X, mouse. cursorpos. y );

Handled: = true;

End;

End;

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.