Use the chtmlview control in the VC dialog box to display HTML

Source: Internet
Author: User
In the VC dialog box, use the chtmlview control to display HTML. Microsoft's MFC provides a new chtmlview class in Visual Studio 6.0, we can display HTML files in a program based on the document view structure. But can it be used to implement this function in the dialog box? We may compare chtmlview and clistview. By comparing these two classes, we will find some interesting differences. In MFC, clistview has a corresponding clistctrl class used in the dialog box, the chtmlview does not have a chtmlctrl class. Therefore, to display HTML files in the control of the dialog box, we have to create a corresponding subclass chtmlctrl for chtmlview. To demonstrate the usage of this class, an HTML file named "about.htm" is displayed in the "about" dialog box of the program. What's more interesting is that the HTML source file used by the program is stored as a resource in the EXE file. After the program is compiled and run, the result 1 is as follows:


Figure 1. dialog box for displaying HTML files

  I. Implementation Method

To display HTML files in the dialog box, you must associate the chtmlctrl class with a static control (or other control) in the dialog box to provide a window for displaying HTML files, therefore, the createfromstatic () function is defined in the chtmlctrl class. The Code is as follows:

Bool chtmlctrl: createfromstatic (uint NID, cwnd * pparent)
{
Cstatic wndstatic; // static control object;
If (! Wndstatic. subclassdlgitem (NID, pparent ))
Return false;
// Obtain the static control rectangular area and convert it to the client 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, // the ID of the control;
Null); // cancels the support of the document Framework;
}

To prevent the master program from regard the chtmlview object as a document/view framework, you need to overload the cview: onmouseactivate () and cview: ondestroy () functions. In addition, when you click in the control, onmouseactivate is responsible for the response (wm_mouseactivate ).

Int chtmlctrl: onmouseactivate (cwnd * p0000topwnd, uint nhittest, uint MSG)
{
// Bypass cview document/framework
Return cwnd: onmouseactivate (psf-topwnd, nhittest, MSG );
}

Void chtmlctrl: ondestroy ()
{
If (m_pbrowserapp)
{
M_pbrowserapp-> release ();
M_pbrowserapp = NULL;
}
Cwnd: ondestroy (); // document/framework of bypass cview
}

Generally, chtmlview releases space in virtual void postncdestroy (), but the control in the dialog box is often implemented as a stack object. Therefore, you do not have to do anything in postncdestroy.

To play back HTML files in resources, you need to reload the navigation processor onbeforenavigate2 () to implement the "app:" pseudo protocol ,. Transfer "app:" to a virtual protocol processor. Because the app is a false protocol, you need to set the pbcancel parameter to "true" to stop this navigation.

Void chtmlctrl: onbeforenavigate2 (lpctstr 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;
}
}

Define a virtual function onappcmd () to process the app: Command. For example, when the browser is about to navigate to "app: foo", this function is called and the value of the parameter lpszwhere is "foo ".

Void chtmlctrl: onappcmd (lpctstr lpszwhere) {// default: Do Nothing}

For HTML files as resources and embedded image and music files, it is very important to use the actual name of the file as the resource name so that the browser can find them. On a common web page, we use the following syntax to use images:

This Code assumes that the image file "pd.jpg" exists in the current directory (the directory where the page file is located. How can we reference image files that exist as resources in the EXE file? The method is the same. In this case, we must tell the browser the location of the web page file. Add the following code at the beginning of the web page file:

<Base url = "res: // showhtml.exe/about.htm">

This line of code tells the browser that the current directory is "res: // showhtml.exe". When the browser encounters the Code , it will follow the path Res: // showhtml.exe/pd.jpg. Otherwise, it will be searched in the path of the program file. Generally, Res: // modulename can be used to access resources in a dynamic library or executable file. Here Res: means the same as http:, ftp:, file:, and mailto. That is, "the first name in this path is a file name, and the second name is the resource name in the file ". The rest of the work is done by the browser.

To load the web page in the dialog box, call the chtmlctrl: loadfromresource function, which is inherited by chtmlview. You can also use the full path Res: // showhtml.exe/about.htm as the parameter. In addition, the "OK" button in the caboutdialog dialog box is processed. In fact, it is not a button, but an image embedded in the HTML file, use JScript to control the status of the image when it is pressed and when it is started. The tips for handling the "OK" button are mainly to solve the communication between the dialog box and the master program. The Dynamic HTML document layer (COM) technology allows users to process images or links clicked by obtaining image elements and then listening for onclick events. But this is a very troublesome method. There is also a simpler method. Assume that HTML has the following image links:

<A href = "OK"> </a>

When you click it, the browser displays the "OK" file, but before it is displayed, the control first executes the chtmlview: onbeforenavigate2 () function. Therefore, you can define the chtmlctrl subclass cmyhtmlctrl, reload this function to implement anything you want to do. The following code disables the dialog box when you click "OK" on the HTML file.

Void chtmlctrl: onbeforenavigate2 (
Lpctstr lpszurl,
...,
Bool * pbcancel)
{
If (_ tcscmp (lpszurl, _ T ("OK") = 0)
{
// "OK" clicked:
* Pbcancel = true; // abort
Getparent ()-> sendmessage (wm_command, idok); // will close Dialog
}
}

In fact, "OK" is not a file; it is just a very special name, you can define a chtmlctrl subclass cmyhtmlctrl, this class regards "OK" image as "OK" button. To achieve this idea, the program creates a impersonating protocol called app: to replace "OK". In about.htm, the actual link is defined as APP: OK. When you navigate to app: somewhere in the browser, cmyhtmlctrl calls a virtual function: cmyhtmlctrl: onappcmd with "somewhere" as the parameter.

Void cmyhtmlctrl: onappcmd (lpctstr lpszwhere)
{
If (_ tcsicmp (lpszwhere, _ T ("OK") = 0)
{
Getparent ()-> sendmessage (wm_command, idok );
}
}

  Ii. programming steps

1. Start visual c ++ 6.0 and generate a single-document application named "showhtml ";

2. Modify the "about" dialog box resource in the program, place a static control in it, and set its ID to idc_htmlview;

3. Add HTML file resources to the program and set the ID to "about.htm ";

4. Add the chtmlctrl and cmyhtmlctrl files to the program;

5. Add a cmyhtmlctrl Class Object m_page to the cabout class, and use classwizard to overload the oninitdialog () function of the cabout class;

6. Compile and run the program.

 

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.