Use of webbrowser components and mshtml in Delphi

Source: Internet
Author: User

Mshtml encapsulates the elements on the HTML page into component interfaces such as ihtmlinputelement, ihtmlinputbuttonelement, ihtmlinputtextelement, ihtmltextareaelement, ihtmltitleelement, and ihtmlformelement.
In Program The ihtmldocument2 interface provided by mshtml can be used to obtain the entire document object. The ihtmlelementcollection interface can obtain a set of all page elements. The item method of this interface can be used to obtain a specific component, then, set and read the attribute values of the component.
The following are examples of common functions. Code .
1. Open a page:
Web. navigate (extractfilepath (application. exename) + 'template/login.html ');
2. Retrieve the value property value of an htmlelement on the page:
Function getvaluebyelementname (web: twebbrowser; elementname: string; index: integer): string;
Begin
Result: = (Web. Document as ihtmldocument2). Body. All
Ihtmlelementcollection). Item (elementname, index) as ihtmlinputelement
). Value
End;
3. Set the value attribute for htmlelement
Procedure setvaluetextareaname (web: twebbrowser; elementname, value: string; index: integer );
Begin
(Web. Document as ihtmldocument2). Body. All
Ihtmlelementcollection). Item (elementname, index) as ihtmltextareaelement
). Value: = value;
End;
4. Determine whether the page execution result is successful
If an error occurs in a web application, the error page is usually displayed to the end user, so we cannot catch the HTTP Error, you can only record URL parameters to global variables in the webbeforenavigate2 event, and then determine whether the execution result is correct based on URL parameters and URL parameters in the global variables in the webdocumentcomplete event. of course, this requires coding the page address into the code to reduce the flexibility, but this is also the only method I can think of. If you have any good methods, please let me know.
5. shield the right mouse and some shortcut keys
This function requires the applicationevents component to be added to the webbrowser window, And the onmessage Event code is set as follows.
Procedure twebadapterform. applicationevents1message (var msg: tagmsg;
VaR handled: Boolean );
Const
_ Keypressmask = $80000000;
Begin
// Right-click Disabled
With MSG do
Begin
If not ischild (Web. Handle, hwnd) Then exit;
Handled: = (Message = wm_rbuttondown) or (Message = wm_rbuttonup) or (Message = wm_contextmenu );
End;
// Disable Ctrl + n
// Disable Ctrl + F
// Disable Ctrl +
If MSG. Message = wm_keydown then
Begin
If (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') then
Begin
Handled: = true;
End;
End;
End;
6. When the page is closed, the VCL form containing the page is also disabled. (internetexplorer 6.0 only)
This function requires you to uninstall the webbrowser component that comes with Delphi and install the actionx component (Microsoft Internet controls V1.1). In addition, future programs can only run in environments where Internet Explorer 6.0 is installed. the specific method is as follows:
In the onwindowclosing event of the webbrowser component, enter the self. Close; Code. To prevent window close, set the canclose parameter to flase.
7. How to package a new page window with a hyperlink over the page to the specified VCL window.
Procedure tform1.webnewwindow2 (Sender: tobject; var ppdisp: idispatch;
VaR cancel: wordbool );
VaR
Form: tform1;
Begin
Form: = tform1.create (NiL );
Ppdisp: = form. Web. defaultdispatch;
Form. show;
End;
8. After loading the HTML page in webbrowser, insert the HTML code at the top of the page. The following two methods can be used.
{1 .----------------------------------------------------------------}
Procedure tform1.button1click (Sender: tobject );
VaR
Range: ihtmltxtrange;
Begin
Range: = (webbrowser1.document as ihtmldocument2). Body
Ihtmlbodyelement). createTextRange;
Range. Collapse (false );
Range. pastehtml ('<br/> <B> hello! </B> ');
End;
{2 .----------------------------------------------------------------}
Procedure tform1.webbrowser1documentcomplete (Sender: tobject;
Const Pdisp: idispatch; var URL: olevariant );
VaR
Webdoc: htmldocument;
Webbody: htmlbody;
Begin
Webdoc: = webbrowser1.document as htmldocument;
Webbody: = webdoc. Body as htmlbody;
Webbody. insertadjacenthtml ('beforeend', 'End;
9. Select all the content displayed on the page and paste it into the Word document.
Webbrowser1.execwb (olecmdid_selectall, olecmdexecopt_dodefault); // select all webpages
Webbrowser1.execwb (olecmdid_copy, olecmdexecopt_dodefault); // copy the webpage
Worddocu. range. paste; // paste the Word Document
Webbrowser1.execwb (olecmdid_undo, olecmdexecopt_dodefault); // cancel all
Note: The document attribute value of webbrowser and the document attribute value of worddocument must not be nil.
10. How to solve the problem that the webpage does not respond to the carriage return event?
Public
{Public declarations}
Procedure msghandle (var msg: tmsg; var handled: Boolean );
End;
VaR
Form1: tform1;
Foleinplaceactiveobject: ioleinplaceactiveobject;
Implementation
{$ R *. DFM}
Procedure tform1.msghandle (var msg: tmsg; var handled: Boolean );
VaR
Ioipao: ioleinplaceactiveobject;
DISPATCH: idispatch;
Begin
If webbrowser1 = nil then
Begin
Handled: = false;
Exit;
End;
Handled: = (isdialogmessage (webbrowser1.handle, MSG) = true );
If (handled) and (not webbrowser1.busy) then
Begin
If foleinplaceactiveobject = nil then
Begin
DISPATCH: = webbrowser1.application;
If dispatch <> nil then
Begin
Dispatch. QueryInterface (ioleinplaceactiveobject, ioipao );
If ioipao <> nil then
Foleinplaceactiveobject: = ioipao;
End;
End;
End;
If foleinplaceactiveobject <> nil then
If (MSG. message = wm_keydown) or (MSG. message = wm_keyup) and (MSG. wparam = vk_back) or (MSG. wparam = vk_left) or (MSG. wparam = vk_right) then
Else
Foleinplaceactiveobject. translateaccelerator (MSG );
End;
Procedure tform1.formcreate (Sender: tobject );
Begin
Application. onmessage: = msghandle;
End;
Procedure tform1.formdestroy (Sender: tobject );
Begin
Foleinplaceactiveobject: = nil;
End;
11. How to call the JavaScript function sayhello () on the current page in webbrowser ()
Webbrowser1.oleobject.
Document.parentwindow.exe cscript ('sayhello () ', 'javascript ');
// Or
(Webbrowser1.document as ihtmldocument2
2.16.parentwindow.exe cscript ('sayhello () ', 'javascript ')
// Or
Webrowser1.document. Script. sayhello ();

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.