Click here to Skip to main content
6,629,377 members and growing! (21,969 online)
Email Password   helpLost your password?
Desktop Development » Miscellaneous » HTML Browser Controls     Intermediate

WebBrowser Control: How to Print Preview

By Frank W. Wu

Insert the WebBrowser control into a dialog box, and add print preview functionality to it by implementing the IOleCommandTarget interface.
VC6, VC7.1Win2K, MFC, VS.NET2003, IE 6.0, IE 5.5, Dev
Posted:8 Feb 2001
Updated:22 Mar 2005
Views:177,697
Bookmarked:62 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
27 votes for this article.
Popularity: 6.55 Rating: 4.58 out of 5
1 vote, 7.7%
1

2

3
1 vote, 7.7%
4
11 votes, 84.6%
5

Sample Image - wbp.jpg

Introduction

The WebBrowser control is widely used to display web pages either in MFC or in Windows Forms applications. The control has more functionality than you may think - print preview, for instance. If you have been using IE 5.5 or higher, you probably have already seen the print preview window. It is a part of the WebBrowser control. If you have worked with CHtmlView, you also should know this functionality. And you perhaps have recognized that this preview window has nothing to do with the traditional print preview with MFC. This article shows you how simple it is to use print preview and page setup in the WebBrowser control. The requirement is IE 5.5 or higher installed.

Add print preview functionality

In the print preview method, I start by verifying the WebBrowser control is valid. Then, I retrieve a pointer to the IDispatch interface pointer to the HTMLDocument, from which I query the IOleCommandTarget interface, and store the pointer in lpTarget. You can find information on the IOleCommandTarget interface in MSDN. I won't repeat the instructions here. Finally, I execute the print preview command by calling IOleCommandTarget::Exec with the appropriate parameters. The command ID OLECMDID_PRINTPREVIEW is defined in "docobj.h". The GUI of print preview is handled by the WebBrowser control.

Following is the source code of the method:

void CWBPDlg::OnDemoPrintpreview() 
{
    // Verify the WebBrowser control is valid.

    LPDISPATCH lpDispApp = m_wndBrowser.GetApplication();
    if(lpDispApp)
    {
        // Get the HTMLDocument interface.

        LPDISPATCH lpDispDoc = m_wndBrowser.GetDocument();
        if (lpDispDoc != NULL)
        {
            // Get the IOleCommandTarget interface so that 

            // we can dispatch the command.

            LPOLECOMMANDTARGET lpTarget = NULL;
            if (SUCCEEDED(lpDispDoc->
                  QueryInterface(IID_IOleCommandTarget,
                                     (LPVOID*) &lpTarget)))
            {
                // Execute the print preview command. The 

                // control will handle the print preview

                // GUI.

                // OLECMDID_PRINTPREVIEW is defined in

                // "docobj.h".

                lpTarget->Exec(NULL,
                    OLECMDID_PRINTPREVIEW, 0, NULL, NULL);
                lpTarget->Release();
            }
            lpDispDoc->Release();
        }
        lpDispApp->Release();
    }
}

Now you can compile and run the application. The GUI of print preview is the same as that you have seen in IE.

Use WebBrowser::ExecWB method - Alexander Tsarfin's contribution

The WebBrowser class implements a wrapper that allows you to execute a command on an OLE object using the IOleCommandTarget::Exec method.

void CWBPDlg::OnDemoPrintpreview()
{
    m_wndBrowser.ExecWB(OLECMDID_PRINTPREVIEW,
                    OLECMDEXECOPT_PROMPTUSER, NULL, NULL);
}

Add page setup

With the same pattern, the following function opens the page setup dialog box:

void CWBPDlg::OnPagesetup()
{
    m_wndBrowser.ExecWB(OLECMDID_PAGESETUP, OLECMDEXECOPT_PROMPTUSER, NULL, NULL);
}

Conclusion

The above two approaches do the same work � one works with COM interfaces, and the other uses a nicely wrapped method. If you go a further step, you will find other OLE command IDs defined in docobj.h. Once you get the IOleCommandTarget interface, you can easily use them. That is beyond the scope of this article.

Revision History

  • 26 Jun 2002 - Reformatted code to prevent scrolling.
  • 20 March 2005 - Added page setup dialog box.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Frank W. Wu


Member

Occupation: Software Developer (Senior)
Location: United States United States

Other popular Miscellaneous articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 59 (Total in Forum: 59) (Refresh)FirstPrevNext
GeneralThanks! Just what I needed. PinmemberMitchel Haas16:13 24 Aug '09  
GeneralScroll doesn't work PinmemberFrank Lavaleaf6:36 17 Oct '07  
GeneralRe: Scroll doesn't work PinmemberFrank W. Wu9:31 17 Oct '07  
GeneralRe: Scroll doesn't work PinmemberFrank Lavaleaf10:01 17 Oct '07  
GeneralRe: Scroll doesn't work PinmemberFrank Lavaleaf10:23 17 Oct '07  
QuestionChanging the size of the Print Preview Pinmemberandrewtruckle1:57 18 Feb '07  
AnswerRe: Changing the size of the Print Preview PinmemberFrank W. Wu7:28 18 Feb '07  
GeneralRe: Changing the size of the Print Preview [modified] Pinmemberandrewtruckle8:57 18 Feb '07  
Questionhow to do preview in the same window, not another popup window? Pinmemberflyingxu5:27 14 Jan '07  
AnswerRe: how to do preview in the same window, not another popup window? PinmemberFrank W. Wu12:44 15 Jan '07  
GeneralApplet Pinmembercolin-1230:48 13 Sep '06  
GeneralFreeing memory twice PinmemberFrank W. Wu4:49 13 Sep '06  
GeneralRe: Freeing memory twice Pinmembercolin-1235:28 13 Sep '06  
GeneralMy firewall(ZoneAlarm) is complaining PinmemberAlexandru2:18 31 Jan '06  
GeneralHow to add the browser object in CHtmlView's Scrollbar Pinmemberdhawan_boss@yahoo.com0:17 29 Jul '05  
GeneralRe: How to add the browser object in CHtmlView's Scrollbar PinmemberFrank W. Wu7:21 29 Jul '05  
GeneralPlease help me.. Pinmemberkpt05071:26 12 Apr '04  
QuestionRe: Please help me.. PinmemberChrisRibe12:46 24 Jul '06  
AnswerRe: Please help me.. PinmemberRohit code project21:04 10 Dec '06  
GeneralRe: Please help me.. PinmemberChrisRibe5:31 12 Dec '06  
GeneralThank You! PinmemberDouggie9377:32 20 Feb '04  
Generalweb Pinmembersssssssssssssssssssss1:42 16 Jan '04  
GeneralGood Article PinmemberKoundinya1:03 9 Jan '04  
GeneralHow to navigate WenBrouser to HTML from resources Pinmembergeorgeivanov0:40 8 Dec '03  
GeneralDirect Print from CHtmlView Pinsussbryan f10:30 4 Dec '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 22 Mar 2005
Editor: Smitha Vijayan
Copyright 2001 by Frank W. Wu
Everything else Copyright © CodeProject, 1999-2009
Web21 | Advertise on the Code Project