Click here to Skip to main content
12,546,932 members (54,115 online)
Click here to Skip to main content
Add your own
alternative version


34 bookmarked

Easy to use class for ScreenCapture to printer

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


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


    CPrntScreen * ScrCap;
    ScrCap = new CPrntScreen("Some error ocurred!!!","Message");
    delete ScrCap;

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



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.


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


  • 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 :) 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 :(
  • 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"


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


About the Author

Albert Hermann
Web Developer
Brazil Brazil
IT Programmer since 1987

You may also be interested in...


Comments and Discussions

GeneralRe: this works Pin
fauito5-May-04 5:06
sussfauito5-May-04 5:06 
GeneralRe: this works Pin
mvno5-May-04 9:13
membermvno5-May-04 9:13 
GeneralRe: this works Pin
Albert Hermann7-May-04 9:59
memberAlbert Hermann7-May-04 9:59 
QuestionCould you send me a demo project? Pin
K@iser22-Nov-03 11:48
memberK@iser22-Nov-03 11:48 
AnswerRe: Could you send me a demo project? Pin
Albert Hermann24-Nov-03 2:55
memberAlbert Hermann24-Nov-03 2:55 
GeneralBig sorry for the mess Pin
Albert Hermann25-Sep-03 10:39
memberAlbert Hermann25-Sep-03 10:39 
GeneralSmall suggestion Pin
John Noël25-Sep-03 1:18
memberJohn Noël25-Sep-03 1:18 
Generalprinting from time to time Pin
Martin_Leh14-Aug-03 7:09
memberMartin_Leh14-Aug-03 7:09 
Hi everybody !

I implemented your code to make a hardcopy and it worked - for a moment. At homeOffice (HP Deskjet 930C) it worked 10 time very good.

Now beeing in the Office (HP LaserJet 1200) it works only rarely. Most attemps ends with a blank page.

Is there any Problem with device-depindig bmps ? I would like to print an existing memDC (Mirror-Image to make all drawings, when all is finished it will be displayed to the screen to prevent flickering).
But using this DC and calculationg the size etc. causes a blank sheet too. Are there Problems with sizes etc ? It drives me crazy - I have no idea what to do now Confused | :confused:

Any help ?


Martin L
GeneralNo luck printing :( Pin
oscar_r6-Aug-03 9:44
memberoscar_r6-Aug-03 9:44 
Generalhmm... Pin
eagertolearn3-Aug-03 6:03
membereagertolearn3-Aug-03 6:03 
GeneralRe: hmm... Pin
MessnerEW18-Feb-04 20:31
memberMessnerEW18-Feb-04 20:31 
Generalworks but prints blank pages Pin
JBrady31-Jul-03 6:49
memberJBrady31-Jul-03 6:49 
GeneralRe: works but prints blank pages Pin
Albert Hermann31-Jul-03 15:21
memberAlbert Hermann31-Jul-03 15:21 
GeneralRe: works but prints blank pages Pin
Albert Hermann1-Aug-03 9:33
memberAlbert Hermann1-Aug-03 9:33 
GeneralRe: works but prints blank pages Pin
JBrady1-Aug-03 10:06
memberJBrady1-Aug-03 10:06 
GeneralRe: works but prints blank pages Pin
JBrady1-Aug-03 11:21
memberJBrady1-Aug-03 11:21 
GeneralRe: works but prints blank pages Pin
Albert Hermann2-Aug-03 2:38
memberAlbert Hermann2-Aug-03 2:38 
GeneralRe: works but prints blank pages Pin
dmartinlv6-Aug-03 4:29
memberdmartinlv6-Aug-03 4:29 
GeneralRe: works but prints blank pages Pin
JBrady19-Aug-03 6:51
memberJBrady19-Aug-03 6:51 
Generalfunction does not take 2 parameters Pin
craigsmith00712-Jun-03 4:42
membercraigsmith00712-Jun-03 4:42 
GeneralRe: function does not take 2 parameters Pin
Albert Hermann12-Jun-03 8:37
memberAlbert Hermann12-Jun-03 8:37 
GeneralTry the Rich !..i think only u can!.. Pin
Balaji200028-May-03 6:37
memberBalaji200028-May-03 6:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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
Web02 | 2.8.161018.1 | Last Updated 10 May 2004
Article Copyright 2003 by Albert Hermann
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid