Click here to Skip to main content
11,641,224 members (61,699 online)
Click here to Skip to main content

WebBrowser Control: How to Print Preview

, 22 Mar 2005 247.1K 3.9K 75
Rate this:
Please Sign up or sign in to vote.
Insert the WebBrowser control into a dialog box, and add print preview functionality to it by implementing the IOleCommandTarget interface.

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

Share

About the Author

Frank W. Wu
Technical Lead
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
Questionprint web browser control page in silverlight MVVM Pin
Member 102767946-Mar-15 20:36
memberMember 102767946-Mar-15 20:36 
GeneralThanks! Just what I needed. Pin
Mitchel Haas24-Aug-09 15:13
memberMitchel Haas24-Aug-09 15:13 
GeneralScroll doesn't work Pin
Frank Lavaleaf17-Oct-07 5:36
memberFrank Lavaleaf17-Oct-07 5:36 
GeneralRe: Scroll doesn't work Pin
Frank W. Wu17-Oct-07 8:31
memberFrank W. Wu17-Oct-07 8:31 
GeneralRe: Scroll doesn't work Pin
Frank Lavaleaf17-Oct-07 9:01
memberFrank Lavaleaf17-Oct-07 9:01 
GeneralRe: Scroll doesn't work Pin
Frank Lavaleaf17-Oct-07 9:23
memberFrank Lavaleaf17-Oct-07 9:23 
QuestionChanging the size of the Print Preview Pin
andrewtruckle18-Feb-07 0:57
memberandrewtruckle18-Feb-07 0:57 
AnswerRe: Changing the size of the Print Preview Pin
Frank W. Wu18-Feb-07 6:28
memberFrank W. Wu18-Feb-07 6:28 
GeneralRe: Changing the size of the Print Preview [modified] Pin
andrewtruckle18-Feb-07 7:57
memberandrewtruckle18-Feb-07 7:57 
Questionhow to do preview in the same window, not another popup window? Pin
flyingxu14-Jan-07 4:27
memberflyingxu14-Jan-07 4:27 
AnswerRe: how to do preview in the same window, not another popup window? Pin
Frank W. Wu15-Jan-07 11:44
memberFrank W. Wu15-Jan-07 11:44 
GeneralApplet Pin
colin-12312-Sep-06 23:48
membercolin-12312-Sep-06 23:48 
GeneralFreeing memory twice Pin
Frank W. Wu13-Sep-06 3:49
memberFrank W. Wu13-Sep-06 3:49 
GeneralRe: Freeing memory twice Pin
colin-12313-Sep-06 4:28
membercolin-12313-Sep-06 4:28 
GeneralMy firewall(ZoneAlarm) is complaining Pin
Alexandru31-Jan-06 1:18
memberAlexandru31-Jan-06 1:18 
QuestionHow to add the browser object in CHtmlView's Scrollbar Pin
dhawan_boss@yahoo.com28-Jul-05 23:17
memberdhawan_boss@yahoo.com28-Jul-05 23:17 
AnswerRe: How to add the browser object in CHtmlView's Scrollbar Pin
Frank W. Wu29-Jul-05 6:21
memberFrank W. Wu29-Jul-05 6:21 
GeneralPlease help me.. Pin
kpt050712-Apr-04 0:26
memberkpt050712-Apr-04 0:26 
QuestionRe: Please help me.. Pin
ChrisRibe24-Jul-06 11:46
memberChrisRibe24-Jul-06 11:46 
AnswerRe: Please help me.. Pin
Rohit code project10-Dec-06 20:04
memberRohit code project10-Dec-06 20:04 
GeneralRe: Please help me.. Pin
ChrisRibe12-Dec-06 4:31
memberChrisRibe12-Dec-06 4:31 
GeneralThank You! Pin
Douggie93720-Feb-04 6:32
memberDouggie93720-Feb-04 6:32 
Generalweb Pin
sssssssssssssssssssss16-Jan-04 0:42
membersssssssssssssssssssss16-Jan-04 0:42 
GeneralGood Article Pin
Koundinya9-Jan-04 0:03
memberKoundinya9-Jan-04 0:03 
QuestionHow to navigate WenBrouser to HTML from resources Pin
georgeivanov7-Dec-03 23:40
membergeorgeivanov7-Dec-03 23:40 
GeneralDirect Print from CHtmlView Pin
bryan f4-Dec-03 9:30
sussbryan f4-Dec-03 9:30 
GeneralRe: Direct Print from CHtmlView Pin
Anonymous17-Oct-05 9:55
sussAnonymous17-Oct-05 9:55 
GeneralRight scrollbar Pin
tinkywinky31-Mar-03 0:36
membertinkywinky31-Mar-03 0:36 
GeneralRe: Right scrollbar Pin
m0xx14-Oct-03 11:32
memberm0xx14-Oct-03 11:32 
GeneralThanks! Pin
xzackely3-Jan-03 17:52
memberxzackely3-Jan-03 17:52 
Questionhow to save target to a linked file (ex .txt or .sql) Pin
lanlan22-Oct-02 17:29
susslanlan22-Oct-02 17:29 
Generalgreat, but.... Pin
Anonymous4-Oct-02 18:29
sussAnonymous4-Oct-02 18:29 
GeneralCHtmlView and Swing Pin
Sky17-Jul-02 22:35
memberSky17-Jul-02 22:35 
GeneralCustomize Bowser Pin
Sadiqain12-Jul-02 1:14
memberSadiqain12-Jul-02 1:14 
GeneralMine worketh not Pin
JDMagnum18-Apr-02 13:32
memberJDMagnum18-Apr-02 13:32 
GeneralWebbrowser control Pin
Anonymous1-Mar-02 8:00
memberAnonymous1-Mar-02 8:00 
QuestionHow to handle popup window in custom browser Pin
coolvcguy17-Dec-01 18:30
membercoolvcguy17-Dec-01 18:30 
GeneralPrinting specific frame in Webbrowser control Pin
anooo29-Nov-01 17:38
memberanooo29-Nov-01 17:38 
GeneralPage Setup Pin
Sander Duivestein31-Oct-01 0:51
memberSander Duivestein31-Oct-01 0:51 
GeneralRe: Page Setup Pin
Anonymous6-Mar-02 1:02
memberAnonymous6-Mar-02 1:02 
GeneralRe: Page Setup Pin
Jester10-Jul-02 0:34
sussJester10-Jul-02 0:34 
GeneralRe: Page Setup Pin
Anonymous24-Jul-02 6:02
sussAnonymous24-Jul-02 6:02 
GeneralRe: Page Setup Pin
Methar14-Aug-02 5:20
sussMethar14-Aug-02 5:20 
GeneralRe: Page Setup Pin
Anonymous19-Jun-03 10:40
sussAnonymous19-Jun-03 10:40 
GeneralRe: Page Setup Pin
Anonymous17-Aug-04 1:52
sussAnonymous17-Aug-04 1:52 
GeneralI want to change the webBrowser text color Pin
Anonymous24-Oct-01 22:24
memberAnonymous24-Oct-01 22:24 
GeneralThanks! Pin
Anonymous20-Oct-01 21:47
memberAnonymous20-Oct-01 21:47 
GeneralPrint Preview from a file Pin
GK10-Sep-01 11:07
memberGK10-Sep-01 11:07 
QuestionIs there a trick to use this in VB? Pin
Luke27-Apr-01 11:58
memberLuke27-Apr-01 11:58 
AnswerRe: Is there a trick to use this in VB? Pin
KJK::Hyperion1-Nov-01 11:19
memberKJK::Hyperion1-Nov-01 11:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150731.1 | Last Updated 23 Mar 2005
Article Copyright 2001 by Frank W. Wu
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid