- Table of Contents
- Copyright
- About the Authors
- About the Contributors
- Acknowledgments
- Tell Us What You Think!
- Introduction
- How to Use This Book
- What You Need to Use This Book
- What's New in Visual C++ 6.0
- Contacting the Main Author
- Part I: Introduction
- Chapter 1. The Visual C++ 6.0 Environment
- Part II: MFC Programming
- Chapter 2. MFC Class Library Overview
- Chapter 3. MFC Message Handling Mechanism
- Chapter 4. The Document View Architecture
- Chapter 5. Creating and Using Dialog Boxes
- Chapter 6. Working with Device Contexts and GDI Objects
- Chapter 7. Creating and Using Property Sheets
- Chapter 8. Working with the File System
- Chapter 9. Using Serialization with File and Archive Objects
- Part III: Internet Programming with MFC
- Chapter 10. MFC and the Internet Server API (ISAPI)
- Chapter 11. The WinInet API
- Chapter 12. MFC HTML Support
- Part IV: Advanced Programming Topics
- Chapter 13. Using the Standard C++ Library
- Chapter 14. Error Detection and Exception Handling Techniques
- Chapter 15. Debugging and Profiling Strategies
- Chapter 16. Multithreading
- Chapter 17. Using Scripting and Other Tools to Automate the Visual C++ IDE
- Part V: Database Programming
- Chapter 18. Creating Custom AppWizards
- Chapter 19. Database Overview
- Chapter 20. ODBC Programming
- Chapter 21. MFC Database Classes
- Chapter 22. Using OLE DB
- Chapter 23. Programming with ADO
- Part VI: MFC Support for COM and ActiveX
- Chapter 24. Overview of COM and Active Technologies
- Chapter 25. Active Documents
- Chapter 26. Active Containers
- Chapter 27. Active Servers
- Chapter 28. ActiveX Controls
- Part VII: Using the Active Template Library
- Chapter 29. ATL Architecture
- Chapter 30. Creating COM Objects Using ATL
- Chapter 31. Creating ActiveX Controls Using ATL
- Chapter 32. Using ATL to Create MTS and COM+ Components
- Part VIII: Finishing Touches
- Chapter 33. Adding Windows Help
- Part IX: Appendix
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 CHtmlView class hides the WebBrowser control behind another layer of abstraction, allowing functions to use more intuitive parameters.
- Functions in the CHtmlView class take advantage of C++ features such as overloading and optional parameters, simplifying your code.
- You can leverage standard Document/View events, such as OnInitialUpdate.
- You can easily create multiple instances of your class derived from CHtmlView.
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:
- The URL to be loaded
- An optional navigation flag
- An optional target frame for the navigation
- An optional string of header information to be sent to the server
- An optional buffer of information to be sent as part of an HTTP POST command
- The size of the previous parameter, in bytes
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 address of an ITEMIDLIST structure that refers to a folder location
- A browser navigation flag, as with other versions of Navigate discussed earlier in this chapter
- A string specifying the target HTML frame for the navigation, or NULL if the current frame is to be used
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:
- The URL to be loaded
- Navigation flags, if any
- A CByteArray containing the POST data
- The target HTML frame, or NULL to use the current frame
- An optional string of header information to be sent to the server
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:
- GoBack is used to navigate to the previous location in the history list.
- GoHome is used to navigate to the home URL.
- GoForward is used to navigate to the next location in the history list.
- Stop is used to halt the download of a URL.
- Refresh is used to reload the current URL.
- LoadFromResource is used to load a document that has been stored as a resource.
- Navigate, discussed earlier, is used to navigate to a specific URL.
- Navigate2, also discussed earlier, is used to navigate to a specific URL or special folder, such as Desktop, or My Computer.
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:
- A Dynamic HTML document can be embedded in your program's resource file, just like other resources such as dialog boxes, icons, and menus.
- A Dynamic HTML document can be loaded from a file on the local computer.
- The document can be downloaded from a remote location on the Internet or other network.
If your program uses a specific Dynamic HTML document that must be available at all times, store it as an HTML resource.
Using MFC and Dynamic HTML | Next Section

Account Sign In
View your cart