Access the HTML source code in the webbrowser control directly
---- In order to display HTML documents in its own procedure, we generally use an ActiveX control TWEBBRowser that comes with IE (Internet Explorer IE). This control uses the same kernel as IE, powerful, starting from Delphi5, formally getting support from Inprise, replacing the original THTML control, becoming the preferred control of the HTML document in Delphi.
---- But in the actual programming process, I found that this control provides a lot of restrictions, such as browsing of the HTML document, can only be implemented by specifying the URL or file name, can not read and write directly like the THTML control in the past HTML source. So if the program dynamically generates a HTML text, you must write the text content first to a temporary file, then pass the file name of this file to the WebBrowser control, implement display. Walking this detachment makes the program response speed, and it is easy to leave some "garbage" (temporary document).
---- After investigating some programs that use the WebBBrowser control, I found most programs, such as the famous domestic software Foxmail, is the way to deliver HTML documents through temporary files; but some foreign software, such as MS own Outlook Express does not exist, because it does not need to generate temporary files, the display speed of the HTML document is significantly more than 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. Here, you must specifically thank the netizen Anglefalls on BBS (BBS.WHNET.EDU.CN) to provide clues.
---- In fact, the Document object in the webbrowser control provides an IPERSISTREAMINIT interface. Through this interface, we can easily implement read and write to the HTML source code.
---- The following is the definition and description of the IPersistStreaminit interface:
Uses ActiveX, Comobj;
{IPERSISTREAM Interface}
{$ EXTERNALSYM IPersistStream} IPersistStream = interface (IPersist) [ '{00000109-0000-0000-C000-000000000046}'] function IsDirty: HResult; stdcall; // whether the last modified save function Load (const stm: IStream) : Hresult; stdcall; // Load Function Save from the stream (const stm: bool): hResult; stdcall; // Save the stream Function GetSizeMax (out cbsize: LargeInt): hResult; stdcall; // Save 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); varStream: IStream; hHTMLText: HGLOBAL; psi: IPersistStreamInit; beginif 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)); tryOleCheck (WebBrowser.Document.QueryInterface (IPersistStreamInit, psi)); tryOleCheck (psi.InitNew); OleCheck (psi.Load (Stream)); finallypsi: = nil; end; FinallyStream: = NIL; End;
- One memory will copy the HTML source code that needs to be displayed. This is because the next step is required to create a stream that the WebBrowser control can read. The parameter gptr of the GlobalLoc function indicates that a fixed memory area that needs to be assigned. If the assignment fails, it returns 0, then triggers an exception via the RaiseLastWin32ELOR function, prompting the user; then use the CreateStreamOnhglobal function to establish a stream based on the global heap memory block The second parameter is automatically released when it is released when TRUE is released. If the establishment of success, this flow and the in-memory block just built are shared with 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 who return type HRESULT must be packaged in Olecheck, because a COM interface operation that does not check the return state is too dangerous; The release of the interface, although Delphi can be complete in the background, but as a good programming habit, it should also be explicitly manually released, and the release simply sets the interface to NIL.
---- Next to read the HTML source code:
function GetHtml (const WebBrowser: TWebBrowser): string; constBufSize = $ 10000; varSize: Int64; Stream: IStream; hHTMLText: HGLOBAL; psi: IPersistStreamInit; beginif 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); Tryolecheck (psi.save (stream, false);
Size: = Strlen (Pchar (HHTMLText)); setLENGTH (Result, Size); CopyMemory (Pchar (Result), Pointer (HHTMLText), Size); FinallyStream: = NIL; End; Finallypsi: = NIL; 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, then quit; then get the IPersistStreaminit interface; then get the size of the HTML source: I should use the getSizemax function of the IPersistStreaminit interface, but in my machine, this function range value 0, invalid. Therefore, you can only define a sufficiently large buffer, such as buffsize = $ 1000 bytes (note that this buffer should be large enough); then allocate the global heap memory block, build stream, and then write HTML text. Because this HTML text is the string ending in # 0 in the stream, you can use Size: = Strlen (Pchar (HHTMLText)) to get the actual length with setLength (Result, size); set the return string length of HTML source Length, final replication string to return in 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. A function is provided below, and the webbrowser.document object initialization is initialized by displaying a blank page.
Procedure ShowbLankPage (WebBrowser: TWebBrowser); VARURL: Olevariant; BeginURL: = 'About: blank'; webbrowser.navigate2 (URL);
---- It is recommended to call this function in the FormCreate event you have a webbrowser control, initialize the webbrowser.document object. ---- This article is debugged in the Win NT Delphi 5 environment
---- Reference: MSDN ---- Special Thank: Baiyun Huanghe BBS (bbs.Whnet.edu.cn) Netizen Anglefalls