Direct access to HTML source in WebBrowser controls (Favorites)

zhaozj2021-02-11  163

Direct access to the HTML source code of the WebBrowser control: evaluation: the station Date: 2002-05-22 Description: Source:

-------------------------------------------------- ------------------------------

Htmlwebbrowser.oleObject.document.documentelement.innerhtml; TextWebBrowser.oleObject.document.documentelement.innertext; ***************************************** We generally use an ActiveX control TWEBBRowser that comes with IE (Internet Explorer here).

This control uses the same kernel as IE, powerful, and starts from Delphi5, formally getting support from Inprise, replacing the original THTML control, becoming Delphi

Show the preferred control of the HTML document. But in the actual programming process, I found that this control provides a lot of restrictions, such as browsing HTML documents, can only be implemented by specifying URLs or file names, can't make

Read and write the HTML source code directly as the THTML control. So if the program dynamically generates an HTML text, you must write the text to a temporary file, and then the article of this file

The name is passed to the WebBrowser control, which is displayed. Walking this detachment makes the program response speed, and it is easy to leave some "garbage" (temporary document). ---- After investigation, some programs I found out that most programs, such as famous domestic software Foxmail, delivered HTML documents through temporary files.

Method; but some foreign software, such as MS own Outlook Express does not have this problem, but because it does not need to generate temporary files, the display speed of the HTML document is obviously super

Foxmail. ---- To this end, I check out some relevant information, and finally find the way to directly access the HTML source code directly in the WebBrowser control with the help of netizens. I have to thank Baiyun Huang here.

Netizen Anglefalls on Crane BBS (BBS.Whnet.edu.cn) provides clues. ---- In fact, the Document object in the webbrowser control, this object provides an IPersistStreaminit interface, and we can easily implement HTML sources through this interface.

Code read and write. ---- The following definitions and descriptions are IPersistStreamInit interface: {IPersistStream interface} {$ EXTERNALSYM IPersistStream} IPersistStream = interface (IPersist) [ '{00000109-0000-0000-C000-000000000046}'] function IsDirty: HResult; stdcall (// is modified after the last store (const stm: istream): hResult; stdcall; // Load Function Save from the stream (Const stm: istream; fcleardirtirt; stdcall; // Save Getting Function GetSizeMax (Out CBsize: LargeInt): hResult; stdcall; // Get saving the required space size end; {ipersistStreaminit Interface}

{$ Externalsym ipersistStreaminit} iPersistStreaminit = Interface (IPERSISTREAM)

['{7FD52380-4E07-101B-AE2D-08002B2EC713}'] function initnew: hResult; stdcall; // initialization END;

First, to achieve write, because this is the most urgent requirements: procedure SetHtml (const WebBrowser: TWebBrowser; const Html: string); var Stream: IStream; hHTMLText: HGLOBAL; psi: IPersistStreamInit; begin if not Assigned (WebBrowser.Document) then EXIT;

HHTMLTEXT: = Globalalloc (GPTR, Length (HTML) 1); if 0 = hhtmltext the raiselastwin32error;

CopyMemory (Pointer (HHTMLTEXT), PCHAR (HTML), Length (HTML);

OleCheck (CreateStreamOnHGlobal (hHTMLText, True, Stream)); try OleCheck (. WebBrowser.Document QueryInterface (IPersistStreamInit, psi)); try OleCheck (psi.InitNew); OleCheck (psi.Load (Stream)); finally psi: = nil End; Finally Stream: = NIL; END; END; ---- First, two parameters required for this process, webbrowser is the display destination control, HTML is the HTML source code that needs to be displayed; then check if the webbrowser.document object is Effective, ineffective, exit; then allocate a memory in the system global pile, copy the HTML source code that needs to be displayed in. This is because the next step needs to build a webbrowser control.

Read the stream. The parameter gptr of the GlobalAlloc function indicates that the memory area that needs to be allocated in 0, and if the assignment fails, it returns 0.

The RaiseLastWin32Error function triggers an exception, prompting the user; then build a stream based on the global heap memory block with the CreateStreamonhglobal function, and the second parameter is automatically released when the RRUE is released. This stream and just built if it is established

The standby memory block shares the same memory area. Then, use the WebBrowser.Document.QueryInterface function to create an IPersistStreaminit interface. then

You can use this interface directly, psi.initnew initialize the status; PSI.Load (stream) is loaded from the stream into the HTML source code. --- Here, the HTML source code specified by the HTML parameter is displayed in the control specified by the webbrowser parameter. ---- It is worth noting that each function called for the COM interface, that is, those that return types of HRESULT must be packaged in olecheck, because one does not check back

The COM interface operation is too dangerous; the release of the interface, although Delphi can be automated in the background, but as a good program habit, it should also be explicitly released,

Release simply set the interface to NIL. ---- then implemented to read HTML source code: function GetHtml (const WebBrowser: TWebBrowser): string; const BufSize = $ 10000; var Size: Int64; Stream: IStream; hHTMLText: HGLOBAL; psi: IPersistStreamInit; begin if not Assigned ( WebBrowser.Document) then Exit; OleCheck (WebBrowser.Document.QueryInterface (IPersistStreamInit, psi)); try //OleCheck(psi.GetSizeMax(Size)); hHTMLText: = GlobalAlloc (GPTR, BufSize); if 0 = hHTMLText then RaiseLastWin32Error ; OleCheck (CreateStreamOnHGlobal (hHTMLText, True, Stream)); try OleCheck (psi.Save (Stream, False)); Size: = StrLen (PChar (hHTMLText)); SetLength (Result, Size); CopyMemory (PChar (Result) Pointer (hhtmltext), size); Finally Stream: = nil; end; finally psi: = nil; end; end; ---- This function has a parameter webbrowser specifies that the HTML source code is read from that control, returns a string For the HTML source code in this control. First, you should first check if the webbrowser.document object is valid, if it is invalid, then quits; then get the IPersistStreaminit interface; then get the size of the HTML source: Original should be used

The getSizeMax function of the IPersistStreaminit interface, but in my machine, this function range value is 0, which is invalid. Therefore, you can only define a sufficiently big buffer, such as

BufSize = $ 1000 bytes (Note that this buffer should be large enough); then allocate the global heap memory block, set the stream, and then write HTML text to the stream. Because this HTML text is currently

The string ended in # 0, so you can use size: = strlen (pchar (hhtmltext)) to get the actual length, with setLength (Result, size); set the returns to the string length

The HTML source actual length, final replication string to the returns in the string. --- Here, the two functions required to directly access the HTML source code in the WebBrowser control are completely parsed. ---- But when you need to pay attention, before using these two functions, it is best to initialize the webbrowser.document object. Let's provide a function, by displaying a blank page

Now WebBrowser.Document object is initialized. Procedure ShowbLankPage (WebBrowser: TWEBBBROWSER); VAR URL: Olevariant; Begin Url: = 'About: blank'; webbrowser.navigate2 (URL); end; ---- It is recommended to call this in the form of the webbrowser control Function, initialize WebBrowser.Document objects. ---- This article is debugged in Win NT Delphi 5 Environment Different Class Number of WebBrowser Class and WebBrowser_v1 Class: QA002749 Establishment Date: March 20, 2000 Date 20: March 20, 2000 Category: Category of March 20:

Visual Basic - Web Browser

XYJUN: Operating System: WIN98 Programming Tools: VB6.0 Question: Different from the WebBrowser class and the webbrowser_v1 class in the VB object browser. I am inserted into a webbrowser control in the form, but only newwindow2 events, and there is no newwindow event. How to open new web pages with newWindow events

The web page is displayed in the same window (forbidden to open a new window)? And the difference between the event newwindow2 and newwindow?

Reply:

WebBrowser_v1 is the class of IE3, and IE4 uses the WebBrowser class later. If you want the newly opened web page to appear in the same window, you can do this: Private Sub webbrowser1_newwindow2 (PPDISP Asbject, Cancel AS Boolean) Set PPDisp = WebBrowser1.Object End Sub Event NewWindow2 and NewWindow's differences, use events NewWindow You can know what the click link is, and use the event newwindow2 can't know. and

NewWindow is only excited when the user selects "open link in a new window", and NewWindow2 has been excited at this time. When the web is opened when the web is opened with Window.Open.

. The above work can also be used to achieve NewWindow: Dim WithEvents Web_V1 As SHDocVwCtl.WebBrowser_V1 Private Sub Form_Load () Set Web_V1 = WebBrowser1.Object WebBrowser1.Navigate2 "http://askpro.yeah.net" End Sub Private Sub Web_V1_NewWindow (ByVal URL As String, _ byval flags as long, _ byval targetframename as string, _ postdata as variant, _ byval headers as string, _ processed as boolean processed = true webbrowser1.navigate URL END SUB

转载请注明原文地址:https://www.9cbs.com/read-6000.html

New Post(0)