Use CHtmlView in VC6 to display HTML files in dialog box Control

Source: Internet
Author: User

A new CHtmlView class appears in Visual Studio 6.0. With this class, we can display HTML files in the control of the dialog box. To use the CHtmlView class, you must have a comprehensive and in-depth understanding of its definition and implementation. Let's compare CHtmlView and CListView. By comparing these two classes, we will find some interesting differences. First, there is a CListCtrl class for CListView in MFC, but there is no CHtmlCtrl class for CHtmlView. Second, the use of CListView depends on the document/View Structure of MFC, the implementation of CHtmlView is based on COM. It is implemented through the IWebBrowser2 interface, and there is no relationship between IWebBrowser2 and the MFC Document/view structure.

To display HTML files in the control of the dialog box, we can also create a corresponding CHtmlCtrl class for CHtmlView. The source code of the CHtmlCtrl program is as follows:

Create a static control (or other control). The ID and size of this control are the same as those on the interface.

Bool chtmlctrl: createfromstatic (uint NID, cwnd * pparent) {cstatic wndstatic; If (! Wndstatic. subclassdlgitem (NID, pparent) return false; // obtain the static rectangular area and convert it to the client area coordinate crect RC of the parent window; 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, // pparent In the rectangle, // NID of the parent window, // null of the Control ID); // framework/document}

To prevent the master program from regard the chtmlview object as a document/view framework, you need to overload it. cview: onmouseactivate and cview: ondestroy. 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 (p0000topwnd, nHitTest, msg );} void CHtmlCtrl: OnDestroy () {if (m_pBrowserApp) {m_pBrowserApp-> Release (); m_pBrowserApp = NULL;} CWnd: OnDestroy (); // bypass CView documentation/framework}

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.

     virtual void PostNcDestroy() {  }     

To implement the "app:" pseudo protocol, the heavy-duty navigation processor OnBeforeNavigate2 (). Transfer "app:" to a virtual protocol processor. Because the app is a false protocol, it is important to cancel this navigation when browsing.

     
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;        }}   

Reload OnAppCmd () to process the app: Command. 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}

After OnMouseActivate, OnDestroy, and PostNcDestroy are reloaded, CHtrmlCtrl can work like a control in the dialog box. For detailed usage, see the example program: AboutHtml. Run abouthtml.exe and open the About dialog box ...... What a wonderful music! What's more interesting is that the HTML source files, images, sounds, and other files used by the program are stored as resources 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: It is important to use the actual file name 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://AboutHtml.exe/about.htm">

This line of code tells the browser that the current directory is "res: // AboutHtml.exe". When the browser encounters Code , it will follow the path res: // AboutHtml.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 implement the About dialog box, first create a dialog box class: CAboutDialog, which declares a CHtmlCtrl object: m_page. The initialization code of CAboutDialog 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 simple function used to simplify the design of the dialog box. Because it is too difficult to insert a COM object, I inserted a static control in the dialog box and changed its default ID. Call CreateFromStatic to create a static CStatic object with the identical ID number, size, and position. This method is very effective when DestroyWindow is called. To load the web page, call the CHtmlCtrl: LoadFromResource function, which is inherited by CHtmlView. You can also use the full path res: // AboutHtml.exe/about.htm as the parameter.
Now you know how CHtmlCtrl bypasses CView in the dialog box to replace the framework, and how to create HTML files, including text, images, and sounds, and use it as a resource in a program.

In addition, there is another problem: the "OK" button in the CAboutDialog dialog box is actually 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. Is there a simpler way ...... For programmers, laziness is a virtue.
Assume that HTML has the following image links:

<A href="ok"></A>        

When you click it, the browser displays the "OK" file, but before the display, the control first executes CHtmlCtrl: OnBeforeNavigate2. CHtmlCtrl can do anything 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 special name. CHtmlCtrl regards it as an "OK" button. In order to achieve this idea, the program creates an impersonating co-solution called app: to replace the OK ”. in about.htm, the actual link is app: OK. Every time the browser navigating to app: somewhere, CHtmlCtrl calls a virtual function: CHtmlCtrl: OnAppCmd with "somewhere" as the parameter.

void CMyHtmlCtrl::OnAppCmd( LPCTSTR lpszWhere ){   if (_tcsicmp(lpszWhere, _T("ok"))==0)    {GetParent()->SendMessage(WM_COMMAND,IDOK);   }  }

You can make other links in the HTML file, such as app: cancel, app: refresh, or app: whatever, and write your own code in OnAppCmd to process "cancel", "refresh", "whatever", and strings. It is somewhat like programming in VB.

Refer to the example program to improve your About dialog box. If you are interested, you can even use this technology to implement Easter eggs.

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.