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;
Remove the scroll bar:
CoreCode: Webbrowser1.oleobject. Document. Body. Scroll: = 'no ';
The precondition for removing the scroll bar using this code is that a webpage must be opened in webbrowser, that is, the scroll bar should be removed after the webpage 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 flag Tag: = 1 in the webbrowser1documentcomplete event (indicating loading is complete)
The Code is 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
Webbrowser does not pop up an error prompt box
Set this attribute: webbrowser1.silent: = true
Open the link in webbrowser in its own window
Set the code in the newwindow2 event of webbrowser:
Procedure tform1.webbrowsernewwindow2 (Sender: tobject; var ppdisp: idispatch;
VaR cancel: wordbool );
Begin
// Open the new window on your own
Ppdisp: = webbrowser. Application;
End;
Shield webbrower right-click menu
Put an applicationevents control and set the following code in the event onmessage of applicationevents:
(Find the applicationevents control 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.
// Popupmenu1.popup (mouse. cursorpos. X, mouse. cursorpos. y );
Handled: = true;
End;
End;
xmlns: DC = "http://purl.org/dc/elements/1.1/"
xmlns: trackback = "http://madskills.com/public/xml/rss/module/trackback/">
DC: identifier = "http://my.donews.com/robot5/2006/01/21/webbrowser%e5%bc%80%e5%8f%91/"
DC: title = "webbrowser development"
trackback: Ping = "http://my.donews.com/robot5/2006/01/21/webbrowser%e5%bc%80%e5%8f%91/trackback/"/>
-->