Drawing on Printer using C
Window kernel and GDI subsystem manage screen and printer uniformly through the help of Device Context(DC). All the Win32 GDI drawing APIs work as it is with printer device context. We use GetDC() or BeginPrint() to get the DC while drawing on windows screen. However, getting the handle to the device context of a printer is different. Here we get the handle from printer subsystem using Win32 PrintDlg() call.
Drawing on the Printer DC
Printing on a printer is defined by a windows printer spooling 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().
Win32 APIs for Printing
We have an example program to demo the drawing on the printer DC. We have used printer PRINTDLGA data structure and PrintDlg() printer dialog call. These two will get the printer dc from the user input. Later we have used APIs like InitDocStruct(), StartDoc(), StartPage(), EndPage(), EndDoc().
This is the context data structure that the PrintDlg API uses to initialize the Print Dialog Box. This acts as both input and output parameter for PrintDlg(). User selects the settings and after that user presses okay/print and dialog ends. Programmer uses this structure to know the information about the user's selections.
Pops a Print Dialog Box or a Print Setup dialog box before the user. It enables the user to specify the properties (printer name, number of copies, pages etc) of a particular print job.
A pointer to a PRINTDLG structure is used as input and output argument. For input this contains information used to initialize the dialog box. PrintDlg() function blocks until user dismisses the dialog or presses okay/print. On return this structure contains information about the user's selections.
This returns true or nonzero once user clicks the OK button. Programmer should check the members of the PRINTDLG structure which have been given by the user. User might cancel or closed the Print dialog or an error might occurred. False or zero will be returned on these cases. Extended error information like system errors or other failures etc can be retrieved with CommDlgExtendedError function.
Printing DOCINFO structure
The DOCINFO structure contains the document name, output file names and other information used by the API StartDoc. Size of the structure (cbSize) and document name (lpszDocName) should be initialized before calling to StartDoc().
Print API functions
Print APIs take the printer DC as the first argument. The name of these APIs tells what function they do. So these are pretty much easy to understand and use.
Printer DC demo application
Here is a demo application to show the utilities of printer related functions.
Printer demo output
About our authors: Team EQA
MFC hides WinMain in its framework and includes source file on WinMain(). This explains how framework calls global CWinApp::Initinstance() from entry WinMain.
This is constructed during global C++ objects are constructed and is already available when Windows calls the WinMain function, which is supplied by the ...
Define a custom Window class structure, Register the class name, CreateWindow, Show windows and write message get and dispatch loop statements. Define the Window CallBack procedure and write the handlers.
DispatchMessage() is a API which indirectly triggers the Window CallBack procedure. Message structure members from this function are passed to the CallBack procedure. CallBack procedure should implement event handlers depending on the need of the application.
LPARAM and WPARAM are the two parameters in Window CallBack procedure. They signifies parameters of various events. They are used in handing individual events.
We need to write WinMain and need to follow all these in a Win32 application. However we need not to write much if we are writing an application with MFC ...
Questions index C Questions C++ Questions Win32 MFC COM/DCOM DLL Questions
Compilers & Editors
Download Visual Studio Download XCode Download Visual Studio Code Android studio install sdk Eclipse installer Best C compilers IDEs
Development system setup
Windows media creation tool MSDN subscription Ubuntu virtualbox
New updated posts
Why learn C? Draw on printer Memory leaks