HOWTO: Print a DocumentLast reviewed: March 4, 1998Article ID: Q139652 |
|
The information in this article applies to:
- Microsoft Win32 Application Programming Interface (API) included with: - Microsoft Windows NT versions 3.5, 3.51 - Microsoft Windows 95 - Windows CE version 2.0
SUMMARYThis 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 caveats:
MORE INFORMATION
Sample CodeThe following PrintStuff() function illustrates the printing process:
/*==============================================*/
/* 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" ) );
}
Keywords : GdiPrn kbcode Version : WINNT:3.5,3.51;WIN95 Platform : Win95 winnt Issue type : kbhowto |
================================================================================
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |