Exoware ....   Tech Notes    About Exoware    Home
 

Quality solutions, on time, and on budget

 Exoware .....  

Tech Note 

Using the MyApp HTML View Automation Classes

Eric Hartwell - January 2000

Microsoft's CHtmlView class provides the functionality of the WebBrowser control within the context of MFC's document/view architecture. The WebBrowser control is a window in which the user can browse sites on the World Wide Web, as well as folders in the local file system and on a network. This effectively makes the application a web browser. 

The functionality of CHtmlView is designed for applications that access the Web (and/or HTML documents). For HTML-based applications, however, we need a tighter integration between the view class and the actual application. For example,

  • Set HTML text directly from a string, rather than a URL
  • Access the HTML DOM directly from the application's CDocument class
  • Capture arbitrary events within the browser
  • Interact with scripting code running on the browser

This article explains how this is implemented for MyApp.

 

CHtmlView2 Class

CHtmlView2 was originally developed for displaying HTML screens in MyApp. Basically, this class adds the NavigateText method, which lets you specify the screen as an HTML characters string instead of a URL (see Tech Note: Setting HTML View Text Directly From a String).

void CHtmlView2::NavigateText(const char *pszHtmlText)

If the browser has already navigated to a page, the HTML is loaded directly. However, if it hasn't, the method caches the text, navigates to the internal page "about:blank" to initialize the document object, then loads the HTML.

 

CMyAppHtmlView Class

CMyAppHtmlView was developed for displaying HTML screens in MyApp. It builds on the CHtmlView2 foundation to add event sourcing and sinking (see Q181845), printing (see Q156732), and the ability to execute a script within the browser (see Exoware Tech Note: Issuing Commands to the HTML View).

Printing support is simply a matter of sending a print command to the browser control. (It's actually more complicated in BenWin32, since the user may choose to print a screen that isn't currently displayed).

void CMyAppHtmlView::OnPrint(CDC* pDC, CPrintInfo* pInfo)
{
    // Let the HTML control print the current screen
    ExecWB(pInfo->m_bPreview ? OLECMDID_PRINTPREVIEW : OLECMDID_PRINT,
           OLECMDEXECOPT_DONTPROMPTUSER, NULL, NULL);
}

 

CMyAppStudioHtmlDoc/View Implementation

  1. Create a document/view pair, where the view class is derived from CHtmlView or CHtmlView2, and the document class is derived from CMyAppDoc.
  2. Add printing support to the view class:
    void CnewView::OnPrint(CDC* pDC, CPrintInfo* pInfo)
    {
        // Let the HTML control print the current screen
        ExecWB(pInfo->m_bPreview ? OLECMDID_PRINTPREVIEW : OLECMDID_PRINT,
               OLECMDEXECOPT_DONTPROMPTUSER, NULL, NULL);
    }
  3. Add support for the clipboard [Edit - Cut, Copy, Paste, Select All] to the document/view's menu and the actual view class (see How to add clipboard use to CHtmlView):
    void CnewView::OnEditCut()       { ExecWB(OLECMDID_CUT,       OLECMDEXECOPT_DONTPROMPTUSER, NULL, NULL); }
    void CnewView::OnEditCopy()      { ExecWB(OLECMDID_COPY,      OLECMDEXECOPT_DONTPROMPTUSER, NULL, NULL); }
    void CnewView::OnEditPaste()     { ExecWB(OLECMDID_PASTE,     OLECMDEXECOPT_DONTPROMPTUSER, NULL, NULL); }
    void CnewView::OnEditSelectall() { ExecWB(OLECMDID_SELECTALL, OLECMDEXECOPT_DONTPROMPTUSER, NULL, NULL); }
  4. Add a pointer to the view's IHTMLDocument2 interface to the document's member data, and add an OnInitialUpdate function to get this interface pointer from the view.
    IHTMLDocument2 * m_spDocument2;
    void CMyAppHtmlDoc::OnIntialUpdate ( CMyAppHtmlView *pView, LPDISPATCH pDisp )
    {
        if (FAILED(pDisp->QueryInterface( IID_IHTMLDocument2, (void**)&m_spDocument2 )))
            m_spDocument2 = NULL;
    }
  5. Remember, the document object isn't valid until after the browser has finished navigating to the first page. Override the view's OnInitialUpdate method to load a blank screen (navigate to "about:blank") first. 
    void CMyAppHtmlView::OnInitialUpdate()
    {
        CHtmlView2::OnInitialUpdate();            // Default processing
        Navigate2("about:blank");                 // Initialize system to a blank screen
    }
  6. Once navigation is complete, the HTML document initialized. Override the view's OnNavigateComplete2 method to pass the IHTMLDocument2 interface to the document class for further processing:
    void CMyAppHtmlView::OnNavigateComplete2(LPCTSTR lpszURL)
    {
        CHtmlView2::OnNavigateComplete2(lpszURL); // Default processing
        LPDISPATCH pDisp = GetHtmlDocument();     // Get the HTML COM interface
        ((CMyAppHtmlDoc *)GetDocument())->OnIntialUpdate(this, pDisp);
        pDisp->Release();                         // Finished with this copy of COM interface
    }
    The document class is now connected to the HTML view's Document Object Model (DOM).
     
  7. You now have full control of the HTML DOM directly through the document class. You can set the HTML text or directly manipulate the DOM at this point. 
     

Using CMyAppHtmlDoc/View

The CMyAppHtmlDoc and CMyAppHtmlView classes forma a base class for HTML document/view pairs that are aware of MyApp plan information.

The next step is to derive classes for particular document/view types.

CMyAppRuleDoc/View

MyApp Rule screens are essentially data entry screens for the plan database. Each rule has a collection of values (the script variables), and a template which specifies how the values are to be displayed and edited. In addition, each rule has a Validate() method which validates its values according to internal program logic.

Rules are displayed as form views, list views, or both (one or more lists embedded within a form).

CMyAppScreenDoc/View

MyApp "screens" are what the user sees. In MyApp, the screens  are normally shown in Rule mode, with the literals and fields fixed and the only the values (script variables) changeable. However, the screens can also be switched to Edit mode, where the literals and fields themselves may be modified. (Later enhancement: The screens can also be shown in "Preview" mode, where the contents of the fields are replaced with actual data.)


References:

Resources:

MSDN, Microsoft Knowledge Base, Site Builder Workshop, Platform SDK, etc.


Revisions:

  1. December, 1999 - Description for MyApp implementation.
  2. January, 2000 - Overhaul yet again.


Send mail to webmaster@exoware.com with questions or comments about this web site.
Copyright © 1997-2005 Exoware. Last modified: March 16, 2005