Visual C++ 6 Unleashed

Visual C++ 6 Unleashed

By MICKEY WILLIAMS and David Bennett

The CHtmlView Class

The CHtmlView class simplifies using the WebBrowser control in MFC view classes. CHtmlView encapsulates an instance of the WebBrowser control, and uses it for the view's user interface. In many ways, CHtmlView is much easier to use than the WebBrowser control:

The disadvantages of using CHtmlView are that it is most useful when your application fits into the Document/View model, and that some standard Document/View methods don't work as expected (see note).

Navigating with CHtmlView

CHtmlView includes wrappers for WebBrowser navigation functions, including Navigate and Navigate2. As was discussed earlier, the Navigate2 function can be used to load special folders, such as the Control Panel, in addition to HTML content specified by an URL.

The CHtmlView::Navigate function differs slightly from the Navigate function supplied by the WebBrowser control:

void Navigate(LPCTSTR URL,
              DWORD   dwFlags = 0,
              LPCTSTR lpszTargetFrameName = NULL,
              LPCTSTR lpszHeaders = NULL,
              LPVOID  lpvPostData = NULL,
              DWORD   dwPostDataLen = 0);

There are a few differences between the CHtmlView::Navigate function and the WebBrowser control's version of Navigate. The first thing you might notice is that CHtmlView::Navigate has six parameters instead of five:

Another difference is that the fourth and fifth parameters are reversed. Also, none of the parameters are passed as VARIANT or COleVariant, rather they are passed as easier to use C++ types, such as DWORD or LPCTSTR. This simplifies the work required to use this function in CHtmlView.

In many cases, you can take advantage of the default parameters offered by the Navigate function. Typically, you only need to provide the first parameter:

Navigate("http:://msdn.microsoft.com");

There are three overloaded versions of the Navigate2 function. The first version is used to load a special folder location:

void Navigate2(LPITEMIDLIST pIDL,
               DWORD dwFlags = 0,
               LPCTSTR lpszTargetFrameName = NULL);

This version of the function has three parameters:

The second version is used to navigate to a URL:

void Navigate2(LPCTSTR lpszURL,
               DWORD   dwFlags = 0,
               LPCTSTR lpszTargetFrameName = NULL,
               LPCTSTR lpszHeaders = NULL,
               LPVOID  lpvPostData = NULL,
               DWORD   dwPostDataLen = 0);

This version of the function has exactly the same parameters as the CHtmlView::Navigate function discussed earlier.

The third version is used when performing an HTTP POST command, and allows you to pass a CByteArray as the POST data:

void Navigate2(LPCTSTR     lpszURL,
               DWORD       dwFlags,
               CByteArray& baPostedData,
               LPCTSTR     lpszTargetFrameName = NULL,
               LPCTSTR     lpszHeader = NULL);

Just to keep things interesting, this version of Navigate2 has different parameter ordering than other versions of Navigate2 offered by CHtmlView:

Note that this version of Navigate2 doesn't require the size of the POST data to be passed as a parameter—that information is contained in the CByteArray parameter.

As with the Navigate function, many of the parameters used with Navigate2 are optional. For example, you can navigate to a special folder just by passing the appropriate PIDL to Navigate2:

LPITEMIDLIST pidl;
// Retrieve the control panel's folder PIDL
HRESULT hr = SHGetSpecialFolderLocation(NULL, CSIDL_CONTROLS, &pidl);
if(SUCCEDED(hr))
{
    Navigate2(pidl);
}






						

Other Useful CHtmlView Functions

The CHtmlView class includes member functions that wrap WebBrowser control navigation functions. In addition to Navigate and Navigate2, the following are commonly used member functions:

Except for the Navigate and Navigate2 functions, the functions listed above have no parameters. For example, to reload the current page, you can simply call the Refresh function:

void CMyHtmlView::OnViewRefresh()
{
    Refresh();
}

Adding Dynamic HTML to Your Programs

So, where do you find a DHTML document to use in your application? There are three ways to introduce DHTML into your program:

If your program uses a specific Dynamic HTML document that must be available at all times, store it as an HTML resource.

Share ThisShare This

Informit Network