EQuestionAnswers.com Computer/Electronics Questions and Answers
C, C++, VC++, COM/DCOM, DLL and more
#Login #Sign up  Facebook Twitter TGoogle+
 

#Previous question #Index of VC++/WIN32/MFC Questions #Next question

Screen DC vs Printer DC

Window kernel and GDI manages screen and printer uniformly through Device Context. All GDI drawing APIs work as it is with printer DC. However getting the handle to the device context is different in printer DC. We use GetDC() or BeginPrint() while drawing on screen windows. Here we get the handle from printer subsystem using PrintDlg() call.

Printing on Printer DC

Printing on a printer is defined by a printer job. This job is directly linked to a document which is getting printed. A print job document must have a name. This appears in printer job section or in in spooler in printer control panel. StartDoc() API is used to start a job in the printer. Printer device context area is defined by one or more pages. StartPage() API is used to start an area where drawing can be done. The width and height of this page is defined by page size which user selected in printer dialog. A4,A3 A2 etc are like available page size. Printer drawing area in printer DC is proprotinal to page dimension. Printer resolution or dots per inch is the factor or multiplier. Better solution means the area of printer DC is bigger. HORZRES x VERTRES is the actual area of printer DC which is returned by GetDeviceCaps() Programmers can use same Win32 GDI drawing APIs as it is used in screen and windows. Programmers should calculate the width and height of the each objects getting printed. Objects should not overlap and it should go to next page when there is no room left for this page. EndPage() can be called and next to this another StartPage() to go to the next page. These calls are sequential and there is no way to go to previous page. Once drawings are done the EndPage() can be called and then EndDoc() is called to end the printer job. Printer is comparatively slow process and there is a chance that user abort the job in between. Win32 subsystem triggers the AbortProc callback when printer job is aborted. User can install an abort callback routine using SetAbortProc().

Printer DC demo application

Here is a demo application to show the utilities of printer related functions.

#include <commdlg.h>
#include <stdio.h>
/*===============================*/ 
/* Initialize DOCINFO structure */ 
/* ==============================*/ 
void InitPrintJobDoc( DOCINFO* di, char* docname)
{
  memset( di, 0, sizeof( DOCINFO ) );
10    /* Fill in the required members. */
11    di->cbSize = sizeof( DOCINFO );
12    di->lpszDocName = docname;
13  }
14 
15  /*===============================*/ 
16  /* Drawing on the DC on Page */ 
17  /* ==============================*/ 
18  void DrawPage( HDC hdc, UINT Page)
19  {
20    char line[50];
21    int nWidth, nHeight;
22 
23    nWidth = GetDeviceCaps(hdc, HORZRES);
24    nHeight = GetDeviceCaps(hdc, VERTRES);
25 
26    SelectObject(hdc, CreatePen(PS_SOLID,2,RGB(255,0,0)));
27    Rectangle(hdc, 0, 0, nWidth-4, nHeight-2);
28    sprintf (line,
29             "Test Printing, page#%u width x height (%dx%d)",
30         Page,
31         nWidth,
32         nHeight);
33    SetBkMode(hdc, TRANSPARENT); 
34    TextOut(hdc, 4, 4, line, lstrlen(line) );
35  }
36    
37  /*===============================*/ 
38  /* The Abort Procudure */ 
39  /* ==============================*/ 
40  BOOL CALLBACK AbortProc( HDC hDC, int Error )
41  {
42     MSG   msg;
43     while( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
44     {
45       TranslateMessage( &msg );
46       DispatchMessage( &msg );
47     }
48     return TRUE;
49  }
50  /*===============================*/ 
51  /* Obtain printer device context */ 
52  /* ==============================*/ 
53  HDC GetPrinterDC(void)
54  {
55    PRINTDLG pdlg;
56 
57    /* Initialize the PRINTDLG structure. */
58    memset( &pdlg, 0, sizeof( PRINTDLG ) );
59    pdlg.lStructSize = sizeof( PRINTDLG );
60    /* Set the flag to return printer DC. */
61    pdlg.Flags =  PD_RETURNDC;
62 
63    /* Invoke the printer dialog box. */
64    PrintDlg( &pdlg );
65    
66    /* hDC member of the PRINTDLG structure contains the printer DC. */
67    return pdlg.hDC;
68  }
69  /*==============================================*/ 
70  /* Sample code : Typical printing process */ 
71  /* =============================================*/ 
72  void PrintJob( void )
73  {
74    HDC        hDC;
75    DOCINFO    di;
76 
77    /* Need a printer DC to print to. */
78    hDC = GetPrinterDC();
79 
80    /* Did you get a good DC? */
81    if( !hDC)
82    {
83      MessageBox (NULL,
84                  "Error creating DC",
85                  "Error",
86                  MB_APPLMODAL | MB_OK );
87      return;
88    }
89 
90    /* You always have to use an AbortProc(). */
91    if( SetAbortProc( hDC, AbortProc ) == SP_ERROR )
92    {
93      MessageBox (NULL,
94                "Error setting up AbortProc",
95                  "Error",
96                  MB_APPLMODAL | MB_OK);
97      return;
98    }
99 
100    /* Init the DOCINFO and start the document. */
101    InitDocStruct( &di, "MyDoc");
102    StartDoc( hDC, &di );
103 
104    /* Print 1st page. */
105    StartPage( hDC );
106    DrawStuff( hDC, 1 );
107    EndPage( hDC );
108    
109    /* Print 2nd page. */
110    StartPage( hDC );
111    DrawStuff( hDC, 2 );
112    EndPage( hDC );
113    /* Indicate end of document.*/
114    EndDoc( hDC );
115    /* Clean up */
116    DeleteDC( hDC );
117  }
118 
119  /* Print DC demo with 2 pages */
120  int APIENTRY WinMain(HINSTANCE hInstance,
121                       HINSTANCE hPrevInstance,
122                       LPSTR     lpCmdLine,
123                       int       nCmdShow)
124  {
125 
126    if(MessageBox (NULL,
127                   "Do you want to test print 2 pages?",
128           "Test Print",
129           MB_YESNO  ) != 0)
130    {
131      PrintJob();
132    }
133    return 0;
134  }

Printer demo output

Print demo first dialog

Print demo PDF printer

Print demo Windows XPS printer

Print demo output PDF and Windows XPS file

You have viewed 1 page out of 109. Your VC++ learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

#Programming Windows, Fifth Edition (Developer Reference)
#Programming Applications for Microsoft Windows (Microsoft Programming Series)
#Programming Windows with MFC, Second Edition
#Visual C++ Programming
#Programming Microsoft Visual C++, Fifth Edition

* #1 webmaster Sat 26 Apr/2014 16:13:26(GMT)  Like 0 Unlike 0

Dear Users,

We are pleased to inform you that a forum/blog has been incorporated with www.mybestnotes.co.in. You are welcomed to add your comments, requests, codes, solutions and feedback to it. Please login(if already a member) or signup(for free) to avail to this facility.

Regards,
Webmaster

Your message goes here:

Name:Guest
Email:anonymous@unknown.com
My Post:*
Secutiry Code: ******  *
Preview this compose before posting this in discussion forum.
 
Note:
  1. Use [Sxx] code to show smiles. Example [S02]
  2. Use [URL ], example [URL http://www.google.com/]
  3. To display code or un formatted text use [CODE] [/CODE]. Example: [CODE] printf("Hello world"); [/CODE]
 [S01]   [S02]   [S03]   [S04]   [S05]   [S06]   [S07]   [S08] 
 [S09]   [S10]   [S11]   [S12]   [S13]   [S14]   [S15]   [S16] 
 [S17]   [S18]   [S19]   [S20]   [S21]   [S22]   [S23]   [S24] 

Note: Only members are allowed, * fields are mandatory.