Use ChtmlView in VC6 to display HTML files in dialog control

xiaoxiao2021-03-06  56

Compilation /

Zhao Xiangning

This article has a new class ChtmlView in Visual Studio 6.0. Using this class, we can implement the HTML file in the control of the dialog box. To use the ChtmlView class, there must be a comprehensive and in-depth understanding of its definition and implementation. We may wish to make a comparison with ChtmlView and ClistView. By comparing these two classes, we will find some interesting differences. First, the ClistView in the MFC has a corresponding CListCtrl class, while ChtmlView does not have a CHTMLCTRL class. Second, the use of ClistView relies on the MFC document / view structure, and the implementation of ChtmlView is based on COM. There is no relationship between IWebBrowser2 interface, and there is no relationship between IWebBrowser2 and the MFC document / view structure. In order to display HTML files in the control of the dialog, we can also create a corresponding class Chtmlctrl for ChtmlView. The following is the source code of class chtmlctrl: Create a static control (or other control), the ID and size position of this control are the same as the control over the interface. Bool Chtmlctrl :: CreateFromStatic (uint nid, cwnd * pparent)

{

CSTATIC WNDSTATIC;

IF (! wndstatic.subclassdlgitem (nid, pparent))

Return False;

/ / Get the static controlled rectangular area and convert to the customer area coordinate of the parent window

CRECT RC;

WndStatic.GetwindowRect (& RC);

Pparent-> ScreenToclient (& RC);

WndStatic.DestroyWindow ();

// Create HTML Control (ChTMLVIEW)

Return

CREATE (null, // class name

Null, // Title

(Ws_child | ws_visible), // style

RC, / / ​​rectangular area

PParent, // / parent window

NID, // Control ID

NULL); /// frame / document

} In order to avoid the master program to treat the ChtmlView object as a document / view frame, you need to be overloaded, cView :: OnMouseActivate and CView :: ONDSTROY. In addition, when the user clicks in the control, OnMouseActivate is responsible for responding (WM_MOUSEActivate). INT Chtmlctrl :: ONMOUSEACTIVATE (CWND * PDESKTOPWND, UINT NHITTEST, UINT MSG)

{

// Bypass CVIEW Document / Frame

Return CWnd :: ONMOUSEACTIVATE (PDESkTopWnd, NHittest, MSG);

}

Void Chtmlctrl :: ONDESTROY ()

{

IF (m_pbrowserapp)

{

m_pbrowserapp-> release ();

m_pbrowserapp = null;

}

CWnd :: ONDESTROY (); // Bypass CVIEW Document / Frame

}

Typically, chtmlView is released in Virtual Void PostNCDestroy (), but the control in the dialog is often implemented as a stack object, so it is not necessary to do in postncdestroy (). Virtual void postncdestroy () {} In order to implement the "App:" pseudo protocol, the navigation processor OnBeforenaviGate2 () is overloaded. Pass "App:" link to a virtual protocol processor. Because App: is a fake agreement, it is important to cancel this navigation in browsing. Void Chtmlctrl :: OnBeforenaviGate2 (LPCTSTSTR LPSZURL,

DWORD NFLAGS,

LPCTSTR LPSZTARGETFRAMENAME,

CByteArray & baposteddata,

LPCTSTR LPSZHEADERS,

BOOL * PBCANCEL)

{

Const char App_Protocol [] = "app:";

INT LEN = _TCSLEN (App_Protocol);

IF (_TCSNICMP (LPSZURL, APP_PROTOCOL, LEN) == 0)

{

ONAppCMD (LPSZURL LEN);

* pbcancel = true;

}

} Overload onappcmd (), process the app: command, when the browser is ready to navigate to "App: Foo", this function is called, the value of the parameter lpszwhere is "foo". Void Chtmlctrl :: onappcmd (lpctstr lpszwhere) {// default: do nothing}

After overmouseactivate, OnDestroy, and PostncDestroy, Chtrmlctrl can work like a control in the dialog box. For detailed use, see Example: AboutHTML. Run Abouthtml.exe and open the About dialog box ... how wonderful music! More interesting is the HTML source file, image, sound and other files used in the program to store them as a resource in the exe file: // in abouthtml.rc

About.htm html discardable "res // About.htm"

Pd.jpg html discardable "res // pd.jpg"

Okup.gif html discardable "res // okup.gif"

Okdn.gif html discardable "res // okdn.gif"

Mozart.wav HTML Discardable "Res // Mozart.wav" Note: The actual name with the file is important as the resource name so that the browser can find them. In a normal web page, we use the image to use the following syntax:

This code assumes that the image file "pd.jpg" exists in the current directory (the directory where the page file is located). How do we quoted if the image file is existed as a resource? Like the method, at this time, we must tell the browser web page file. To this end, add the following code at the beginning of the web page file:

This line code tells the browser that the current directory is "res: //abouthtml.exe", when the browser encounters the code , it follows: //abouthtml.exe/pd .jpg lookup. Otherwise, it will look for the path of the program file. Usually use res: // modulename to access resources in dynamic libraries or executable files. Here RES: The meaning is the meaning of HTTP:, FTP:, File:, and Mailto. That is: "The first name in this path is a file name, the second name is the resource name in the file." The rest of the work is done by the browser. In order to implement the About dialog, build a dialog class: CaboutDialog, which declares a chtmlctrl object: m_page. The initial code of the CaboutDialog itself is as follows: BOOL CaboutDialog :: OnInitDialog () {

Verify (cdialog :: oninitdialog ());

Verify (m_page.createfromstatic (idc_htmlview, this));

m_page.loadfromresource (_t ("about.htm");

Return True;

} ChtmlCtrl :: CREATEFROMSTATIC is a very simple function that is used to simplify the design of the dialog. Because the method inserting a COM object is too trouble, I insert a static control in the dialog, change its default ID number. The CreateFromStatic is then called to create a static CSTATIC object with the identical ID number, size, and locations. Then call DestroyWindow, this method is very effective. In order to load the web page, call the chtmlctrl :: loadFromResource function, which is inherited by chtmlView. You can also use a full path res: //abouthtml.exe/about.htm as a parameter. You already know how chtmlctrl is bypassed CView in the dialog; know how to create an HTML file, which contain text, images, and sound, and use it as a resource in the program. In addition, there is a problem that the "OK" button in the CaboutDialog dialog is, in fact, it is not a button at all, but an image embedded in the HTML file, with jscript to control the image is pressed The state of time and when you bounce. Handling the "OK" button is mainly the communication between the dialog box and the master program. Using Dynamic HTML Document Layer (COM) technology can handle users click on images or links, and the method is to obtain image elements and then listen to the OnClick event. But this is a very troublesome way. Is there a simpler way ... For programmers, lazy is a virtue. Suppose HTML has the following image link: When the user clicks it, the browser displays this "OK" file, but before the display, control first execute Chtmlctrl :: onbeforenavigate2. Chtmlctrl can do anything you want to do in this function. Void CMYHTMLCTRL :: OnBeforenaviGate2 (

LPCTSTR LPSZURL,

...,

BOOL * PBCANCEL)

{

IF (_TCSCMP (LPSZURL, _T ("OK")) == 0)

{

// "OK" ClickED:

* pbcancel = true; // Abort // Will Close Dialog

GetParent () -> SendMessage (wm_command, idok);

}

} In fact, "OK" is not a file; it is just a very special name, chtmlctrl regards it as a "OK" button. In order to achieve this idea, a proprigation protocol called App: is created instead of "OK" in the program, and the actual link in About.htm is App: OK. Whenever the browser navigates to App: Somewhere, Chtmlctrl calls a virtual function as a parameter: chtmlctrl :: onappcmd. Void CMYHTMLCTRL :: OnAppcmd (lpctstr lpszwhere)

{

IF (_tcsiicmp (lpszwhere, _t ("ok")) == 0)

{

GetParent () -> SendMessage (wm_command, idok);

}

} You can make additional links in the HTML file, such as: App: Cancel, App: Refresh, or App: WhatVER, etc., and write your own code in ONAppCMD to handle "Cancel", "Refresh", and "Whatver ", String, it is a bit like programmed in VB. Refer to the example program, improve your own About dialog box. If you are interested, you can even use this technology to achieve Easter eggs.

Latest Reviews [Published Reviews] [Article Submission] See all comments Recommend to friends print

Why do I compile errors with VS2003? I: / my documents / 1-code / abouthtmldemo / htmlctrl.cpp (59): Error C2248: "Atl :: _ noaddrefreleaseoncComptr :: release": Unable to access Private Members (in "ATL :: _ noaddrefreleaseonc.comptr "Declaration) with [t = iWebBBRowser2] and [t = iWebBrowser2] H: / Program Files / Microsoft Visual Studio .NET 2003 / VC7 / ATLMFC / INCLUDE / ATLCOMCLI.H (81): See "ATL :: _ NoaddreFreleaseoncComptr :: Release" declaration with [t = iWebBBBBROWSER2] (BBORN Published on 2004-3-22 15:12:00) There is another version of translation: http://www.vckbase. COM / DOCUMENT / VIEWDOC.ASP? id = 510both area good.//comment from http://webdigest.myrice.com/index.html (WebDigest Posted 2003-5-24 21:02:00)

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

New Post(0)