Click here to Skip to main content
15,885,874 members
Articles / Desktop Programming / MFC

Easy to Use Class for ScreenCapture to Printer

Rate me:
Please Sign up or sign in to vote.
4.14/5 (29 votes)
9 May 2004CPOL3 min read 168.9K   3.9K   38   62
A class for easily capturing screen and printing to default printer

Introduction

For a project I'm 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:
    C++
    #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:

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

    Example 2:

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

Example 1 uses just the default parameters, whereas 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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer
Brazil Brazil
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralI was going mad! but thanks to this article I am not anymore;) Pin
joli746915-Oct-04 23:28
joli746915-Oct-04 23:28 
GeneralWow~ Pin
rider7615-Sep-04 23:24
rider7615-Sep-04 23:24 
Generalgetdevicecaps Pin
Alex Evans14-Jul-04 20:13
Alex Evans14-Jul-04 20:13 
GeneralRe: getdevicecaps Pin
Albert Hermann15-Jul-04 2:29
Albert Hermann15-Jul-04 2:29 
GeneralRe: getdevicecaps Pin
Alex Evans19-Jul-04 16:30
Alex Evans19-Jul-04 16:30 
GeneralGreat job - thanks Pin
mvno4-May-04 12:07
mvno4-May-04 12:07 
GeneralNow I need to print a dialog Pin
mais767-Apr-04 23:16
mais767-Apr-04 23:16 
GeneralRe: Now I need to print a dialog Pin
Albert Hermann8-Apr-04 10:25
Albert Hermann8-Apr-04 10:25 
The only way how i can imagine how to solve this is to force the dialog back to visible area. Here some code which brings back the dialog to the screen center during capture:

I suppose you use "Dialog Capture", so insert into the following section
case 1:
    ..
    ..
    GetWindowRect(wHndle,&rectClient);
    nX    = rectClient.left;
    nY    = rectClient.top;
    nX2   = rectClient.right;
    nY2   = rectClient.bottom;
              <-------insert here the code below!
    if (nX < 0) nX = 0;
    if (nY < 0) nY = 0;
    ..
    ..


the following code:
//Let's check if dialog was pulled partially outside of view
if ((nX < 0)||(nY < 0)||(nX2 > sizeClient.cx) || (nY2 > sizeClient.cy))
{
  //in this case, we will force the dialog to screen center first

  HWND hwndOwner;
  RECT rc, rcDlg, rcOwner;

  // Get the desktop and dialog box rectangles.
  hwndOwner = GetDesktopWindow();
  GetWindowRect(hwndOwner, &rcOwner);
  GetWindowRect(wHndle, &rcDlg);
  CopyRect(&rc, &rcOwner);

  // Offset the owner and dialog box rectangles so that
  // right and bottom values represent the width and
  // height, and then offset the owner again to discard
  // space taken up by the dialog box.
  OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);
  OffsetRect(&rc, -rc.left, -rc.top);
  OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);

  // The new position is the sum of half the remaining
  // space and the owner's original position.
  SetWindowPos(wHndle,
      HWND_TOP,
      rcOwner.left + (rc.right / 2),
      rcOwner.top + (rc.bottom / 2),
      0, 0,          // ignores size arguments
      SWP_NOSIZE);

  //Update window
  UpdateWindow(wHndle);

  //and retrieve rectangles again
  GetWindowRect(wHndle,&rectClient);
  nX    = rectClient.left;
  nY    = rectClient.top;
  nX2   = rectClient.right;
  nY2   = rectClient.bottom;
}



Hope that helps,

Albert
GeneralRe: Now I need to print a dialog Pin
mais768-Apr-04 23:37
mais768-Apr-04 23:37 
GeneralClient area and toolbar Pin
craigsmith00718-Feb-04 16:57
craigsmith00718-Feb-04 16:57 
GeneralRe: Client area and toolbar Pin
Albert Hermann2-Mar-04 4:09
Albert Hermann2-Mar-04 4:09 
Generalthis works Pin
Dieter Hammer7-Jan-04 1:33
Dieter Hammer7-Jan-04 1:33 
GeneralRe: this works Pin
Albert Hermann20-Jan-04 8:33
Albert Hermann20-Jan-04 8:33 
GeneralRe: this works Pin
Frank Vetter5-May-04 5:06
Frank Vetter5-May-04 5:06 
GeneralRe: this works Pin
mvno5-May-04 9:13
mvno5-May-04 9:13 
GeneralRe: this works Pin
Albert Hermann7-May-04 9:59
Albert Hermann7-May-04 9:59 
QuestionCould you send me a demo project? Pin
K@iser22-Nov-03 11:48
K@iser22-Nov-03 11:48 
AnswerRe: Could you send me a demo project? Pin
Albert Hermann24-Nov-03 2:55
Albert Hermann24-Nov-03 2:55 
GeneralBig sorry for the mess Pin
Albert Hermann25-Sep-03 10:39
Albert Hermann25-Sep-03 10:39 
GeneralSmall suggestion Pin
Gemini_II25-Sep-03 1:18
Gemini_II25-Sep-03 1:18 
Generalprinting from time to time Pin
Martin_Leh14-Aug-03 7:09
Martin_Leh14-Aug-03 7:09 
GeneralNo luck printing :( Pin
oscar_r6-Aug-03 9:44
oscar_r6-Aug-03 9:44 
Generalhmm... Pin
eagertolearn3-Aug-03 6:03
eagertolearn3-Aug-03 6:03 
GeneralRe: hmm... Pin
MGutmann18-Feb-04 20:31
MGutmann18-Feb-04 20:31 
Generalworks but prints blank pages Pin
JBrady31-Jul-03 6:49
JBrady31-Jul-03 6:49 

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.