HOWTO: Print a DocumentID: Q139652
|
This article describes each of the seven steps required to print a document to a printer in Windows programming. Note that while Windows CE version 2.0 and later do provide printing support, you need to consider the following:
Q135119 PRB: StartDoc() Fails with Non-Zeroed DOCINFOThe InitDocStruct function illustrated later in this article performs this initialization.
StartPage( hDC );
for( i = START_PAGE; i <= END_PAGE; i++)
{
StartPage();
DrawStuff();
EndPage();
}
/*==============================================*/
/* Sample code : Typical printing process */
/* =============================================*/
void PrintStuff( HWND hWndParent )
{
HDC hDC;
DOCINFO di;
// Need a printer DC to print to.
hDC = GetPrinterDC();
// Did you get a good DC?
if( !hdc)
{
MessageBox(NULL, "Error creating DC", "Error",
MB_APPLMODAL | MB_OK );
return;
}
// You always have to use an AbortProc().
if( SetAbortProc( hDC, AbortProc ) == SP_ERROR )
{
MessageBox( NULL, "Error setting up AbortProc",
"Error", MB_APPLMODAL | MB_OK);
return;
}
// Init the DOCINFO and start the document.
InitDocStruct( &di, "MyDoc");
StartDoc( hDC, &di );
// Print one page.
StartPage( hDC );
DrawStuff( hDC );
EndPage( hDC );
// Indicate end of document.
EndDoc( hDC );
// Clean up
DeleteDC( hDC );
}
/*===============================*/
/* Obtain printer device context */
/* ==============================*/
HDC GetPrinterDC(void)
{
PRINTDLG pdlg;
// Initialize the PRINTDLG structure.
memset( &pdlg, 0, sizeof( PRINTDLG ) );
pdlg.lStructSize = sizeof( PRINTDLG );
// Set the flag to return printer DC.
pdlg.Flags = PD_RETURNDEFAULT | PD_RETURNDC;
// Invoke the printer dialog box.
PrintDlg( &pdlg );
// hDC member of the PRINTDLG structure contains
// the printer DC.
return pdlg.hDC;
}
/*===============================*/
/* The Abort Procudure */
/* ==============================*/
BOOL CALLBACK AbortProc( HDC hDC, int Error )
{
MSG msg;
while( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
return TRUE;
}
/*===============================*/
/* Initialize DOCINFO structure */
/* ==============================*/
void InitDocStruct( DOCINFO* di, char* docname)
{
// Always zero it before using it.
memset( di, 0, sizeof( DOCINFO ) );
// Fill in the required members.
di->cbSize = sizeof( DOCINFO );
di->lpszDocName = docname;
}
/*===============================*/
/* Drawing on the DC */
/* ==============================*/
void DrawStuff( HDC hdc)
{
// This is the function that does draws on a given DC.
// You are printing text here.
TextOut(hdc, 0,0, "Test Printing", lstrlen( "Test Printing" ) );
}
Additional query words:
Keywords : kbcode kbGDI kbNTOS350 kbNTOS351 kbPrinting kbWinCE kbWinOS95 kbDSupport
Version : winnt:3.5,3.51
Platform : winnt
Issue type : kbhowto
Last Reviewed: July 6, 1999