5,696,038 members and growing! (16,521 online)
Email Password   helpLost your password?
Desktop Development » Printing » General     Intermediate

Easy to use class for ScreenCapture to printer

By Albert Hermann

A class for easily capturing screen and printing to default printer.
VC6, VC7, C++Windows, NT4, Win2K, WinXP, MFC, VS.NET2002, Visual Studio, Dev

Posted: 13 May 2003
Updated: 9 May 2004
Views: 82,065
Bookmarked: 24 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
25 votes for this Article.
Popularity: 5.36 Rating: 3.83 out of 5
1 vote, 4.0%
1
0 votes, 0.0%
2
2 votes, 8.0%
3
4 votes, 16.0%
4
18 votes, 72.0%
5

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

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

Albert Hermann


IT Programmer since 1987
Occupation: Web Developer
Location: Brazil Brazil

Other popular Printing articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 56 (Total in Forum: 56) (Refresh)FirstPrevNext
GeneralNO MOVE DIALOGmembercsanmartin26:51 20 Nov '06  
GeneralRe: NO MOVE DIALOGmemberAlbert Hermann9:10 20 Nov '06  
GeneralRe: NO MOVE DIALOGmembercsanmartin222:32 20 Nov '06  
GeneralSome problems facedmemberdacylet5:12 1 Mar '06  
GeneralDetected memory leaks!memberl00p1n63:40 26 Feb '06  
GeneralA bug? OS=Win98memberLemonx16:26 22 Feb '06  
GeneralGreat work & little question...memberAlberto_Canabal3:59 11 Jan '06  
GeneralRe: Great work & little question...memberAlbert Hermann7:57 11 Jan '06  
GeneralThank you so much!memberyeunri21:12 7 Sep '05  
Jokewow 2 - really easy to usememberStephan Simson5:07 25 Aug '05  
GeneralA little problem, memory leaksussSaruth23:03 3 Jul '05  
GeneralRe: A little problem, memory leakmemberAlbert Hermann2:03 4 Jul '05  
GeneralIntercepting a print jobmemberAlexEvans17:43 2 Jul '05  
GeneralRe: Intercepting a print jobmemberAlbert Hermann2:09 4 Jul '05  
GeneralGreat CodememberMacca185:42 7 Jun '05  
GeneralRe: Great CodememberAlbert Hermann6:59 7 Jun '05  
GeneralAmazing job!!!!memberDP TWO11:24 13 May '05  
GeneralRe: Amazing job!!!!memberAlbert Hermann7:04 7 Jun '05  
GeneralGreat! One suggestionmembera.damm21:00 28 Apr '05  
GeneralRe: Great! One suggestionmemberAlbert Hermann6:46 2 May '05  
Generalblurry textmembermikeorama1234510:40 20 Oct '04  
GeneralI was going mad! but thanks to this article I am not anymore;)memberjoli74690:28 16 Oct '04  
GeneralWow~memberrider760:24 16 Sep '04  
GeneralgetdevicecapsmemberAlex Evans21:13 14 Jul '04  
GeneralRe: getdevicecapsmemberAlbert Hermann3:29 15 Jul '04  

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

PermaLink | Privacy | Terms of Use
Last Updated: 9 May 2004
Editor: Nishant Sivakumar
Copyright 2003 by Albert Hermann
Everything else Copyright © CodeProject, 1999-2008
Web10 | Advertise on the Code Project