Click here to Skip to main content
11,647,659 members (68,200 online)
Click here to Skip to main content

Easy to use class for ScreenCapture to printer

, 9 May 2004 CPOL 131.7K 3.6K 34
Rate this:
Please Sign up or sign in to vote.
A class for easily capturing screen and printing to default printer.

Introduction

For a project, I'm actually working on, I needed a way to easily create screen shots of the foreground dialog and to print the image directly to the default printer. As I couldn't find the right solution ready for use, I wrote a quick 'n dirty own class, based on the many articles found at The Code Project.

The attached code builds a very easy to use screen capture function, which can be called in a button callback for example. It is possible to adjust the capture area and the print orientation. The desired area will be captured and printed.

Using the code

As said, one of the aims was, to make the class as easy to use as possible.

  1. Add the files PrntScreen.cpp and PrntScreen.h to your project.
  2. In your application (or dialog class etc.), add the following line:
    #include "PrntScreen.h"
  3. At the place, where you want to capture and print the screen, add the following lines to your code:

    Example 1:

    CPrntScreen * ScrCap;
    ScrCap = new CPrntScreen();  //create instance
    ScrCap->DoPrntScreen();  //execute capture function
    delete ScrCap;  //remove instance
    ScrCap=NULL;

    Example2:

    CPrntScreen * ScrCap;
    ScrCap = new CPrntScreen("Some error ocurred!!!","Message");
    ScrCap->DoPrntScreen(1,1,true);
    delete ScrCap;
    ScrCap=NULL;

Example 1 uses just the default parameters, whereas the Example 2 uses available options.

Description

Constructor

The constructor of the class is defined as CPrntScreen(char* sErrTxt, char* sCaption), thus accepting two parameters. The idea was to permit customizing the error message box for your own needs/language.

Description of the parameters:

  • char* sErrTxt = pointer to characters pointing to a desired error message.
  • char* sCaption = pointer to characters pointing to a desired message box caption.
    if the parameters are omitted, the default text/caption will be shown in case of an error.

Functions

The class contains just one function, DoPrntScreen(int nArea, int nOrientation, bool bDialog).
Description of the parameters:

  • nArea determines the area of screen to be captured, with:
    • 0 = Whole screen
    • 1 = Foreground window
    • 2 = Foreground window - Client area
      If parameter is omitted, 0 (Whole Screen) will be used by default

  • nOrientation determines the print orientation, with:
    • 1 = Portrait (you may also use DMORIENT_PORTRAIT)
    • 2 = Landscape (you may also use DMORIENT_LANDSCAPE)
      This parameter is only used, if bDialog=false
      If parameter is omitted, 2 (Portrait) will be used by default

  • bDialog controls, if a printer dialog will be shown:
    • false = Use default printer without showing dialog
    • true = Show printer dialog
      If parameter is omitted, false (Default Printer) will be used by default

History

  • Version 1.0: First version
  • Version 1.1: Updated version.
    Following changes where done:
    • Defined default parameters for even easier use (see example 1 above).
    • Introduced a flag for enabling the use of the printer dialog.
    • Changed the stretching function. Now the captured area will be stretched to printer page in the relation, it has to the whole screen. Capturing of smaller dialogs will not anymore cause huge printouts Smile | :) Having done this, it is definitely best to set print orientation to landscape!
    • The above changes are compatible to the old version, thus no code change is needed when using the new version. Just drop in the new code. Only the printout may change proportions, due to the change of stretching Frown | :(
  • Version 1.1A: Bug fix, fixing an error which caused empty pages on some printers.
  • Version 1.2: Bug fix (hopefully) and new features. This new version includes:
    • The code suggested by Dieter Hammer, which should eliminate that nasty blank page problem
    • Correct printing of dialogs, which were partially pushed out of desktop area
    • Proportional correct printout, independent of paper orientation
    • Checking, if printer is actually capable of printing images
    • And I have added a demo project. The prj file is for VC7, sorry, I don't have VC6 anymore. Just look to the last callback handlers in "PrintDlg.cpp"

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Albert Hermann
Web Developer
Brazil Brazil
IT Programmer since 1987

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 3 Pin
diqiudiguo11-Sep-13 21:57
memberdiqiudiguo11-Sep-13 21:57 
QuestionLicensing Terms Pin
Scott Bartine8-Dec-11 5:44
memberScott Bartine8-Dec-11 5:44 
AnswerRe: Licensing Terms Pin
a. hermann15-Dec-11 0:16
membera. hermann15-Dec-11 0:16 
AnswerRe: Licensing Terms Pin
Albert Hermann15-Dec-11 0:45
memberAlbert Hermann15-Dec-11 0:45 
GeneralNO MOVE DIALOG Pin
csanmartin220-Nov-06 5:51
membercsanmartin220-Nov-06 5:51 
GeneralRe: NO MOVE DIALOG Pin
Albert Hermann20-Nov-06 8:10
memberAlbert Hermann20-Nov-06 8:10 
GeneralRe: NO MOVE DIALOG Pin
csanmartin220-Nov-06 21:32
membercsanmartin220-Nov-06 21:32 
GeneralSome problems faced Pin
dacylet1-Mar-06 4:12
memberdacylet1-Mar-06 4:12 
GeneralDetected memory leaks! Pin
l00p1n626-Feb-06 2:40
memberl00p1n626-Feb-06 2:40 
GeneralA bug? OS=Win98 Pin
Lemonx22-Feb-06 15:26
memberLemonx22-Feb-06 15:26 
GeneralGreat work & little question... Pin
Alberto_Canabal11-Jan-06 2:59
memberAlberto_Canabal11-Jan-06 2:59 
GeneralRe: Great work & little question... Pin
Albert Hermann11-Jan-06 6:57
memberAlbert Hermann11-Jan-06 6:57 
Hi Alberto, Thanks for the nice words. To change the code to always to print "fit to page size" (proportional correct off course), you would need to change the stretching section of the code. See arround line 375, there should be this code:
// Set magnitude of stretching
      // (in relation of screen to captured area to page size)
      scrXY = (double)sizeScreen.cx/(double)sizeScreen.cy;
      prnXY = (double)sizePrn.cx/(double)sizePrn.cy;
      if (scrXY>prnXY)
      {
        zoomx = ((double)sizeClient.cx/(double)sizeScreen.cx);
        zoomy = ((double)sizeClient.cy/(double)sizeScreen.cy)*(prnXY/scrXY);
      }
      else
      {
        zoomx = ((double)sizeClient.cx/(double)sizeScreen.cx)*(scrXY/prnXY);
        zoomy = ((double)sizeClient.cy/(double)sizeScreen.cy);
      }
Now change it to
// Set magnitude of stretching 
      // (in relation of captured area to page size)
      scrXY = (double)sizeClient.cx/(double)sizeClient.cy;
      prnXY = (double)sizePrn.cx/(double)sizePrn.cy;
      if (scrXY>prnXY) 
      {
        zoomx = (double)sizeClient.cx;
        zoomy = ((double)sizeClient.cy) *(prnXY/scrXY);
      }
      else
      {
        zoomx = ((double)sizeClient.cx)*(scrXY/prnXY);
        zoomy = (double)sizeClient.cy;
      }
and you should get the 'fit to page' printing. For active window, use the 2nd example, like
#include "PrntScreen.h"
    CPrntScreen * ScrCap;
    ScrCap = new CPrntScreen();
    ScrCap->DoPrntScreen(1,0,true);
    delete ScrCap;
    ScrCap = NULL;
And please do not forget to fix that error (memory leak) at line 294, as stated in the below posts! To do so, change line 294 from
CBitmap *oldImage = new CBitmap,srcImage;
to
CBitmap *oldImage = new CBitmap;
  CBitmap srcImage;
Havn't found time to update the article, will do so soon, thought. Greetings, Albert Hermann -- modified at 13:19 Wednesday 11th January, 2006
GeneralThank you so much! Pin
yeunri7-Sep-05 20:12
memberyeunri7-Sep-05 20:12 
Jokewow 2 - really easy to use Pin
Stephan Simson25-Aug-05 4:07
memberStephan Simson25-Aug-05 4:07 
GeneralA little problem, memory leak Pin
Saruth3-Jul-05 22:03
sussSaruth3-Jul-05 22:03 
GeneralRe: A little problem, memory leak Pin
Albert Hermann4-Jul-05 1:03
memberAlbert Hermann4-Jul-05 1:03 
GeneralIntercepting a print job Pin
AlexEvans2-Jul-05 16:43
memberAlexEvans2-Jul-05 16:43 
GeneralRe: Intercepting a print job Pin
Albert Hermann4-Jul-05 1:09
memberAlbert Hermann4-Jul-05 1:09 
GeneralGreat Code Pin
Macca187-Jun-05 4:42
memberMacca187-Jun-05 4:42 
GeneralRe: Great Code Pin
Albert Hermann7-Jun-05 5:59
memberAlbert Hermann7-Jun-05 5:59 
GeneralAmazing job!!!! Pin
DP TWO13-May-05 10:24
memberDP TWO13-May-05 10:24 
GeneralRe: Amazing job!!!! Pin
Albert Hermann7-Jun-05 6:04
memberAlbert Hermann7-Jun-05 6:04 
GeneralGreat! One suggestion Pin
a.damm28-Apr-05 20:00
membera.damm28-Apr-05 20:00 
GeneralRe: Great! One suggestion Pin
Albert Hermann2-May-05 5:46
memberAlbert Hermann2-May-05 5:46 
Generalblurry text Pin
mikeorama1234520-Oct-04 9:40
membermikeorama1234520-Oct-04 9:40 

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
Web03 | 2.8.150804.3 | Last Updated 10 May 2004
Article Copyright 2003 by Albert Hermann
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid