Главная страница | назад





Article #17074: Simple Printing code example

 Question and Answer Database
FAQ2074C.txt Simple Printing code example
Category :Windows API
Platform :All
Product :C++Builder ALL
Question:
Is there an example of printing to the default printer that will
work in both windows 95 and NT?
Answer:
Yes, this code also works in win32 console or GUI.
//-----------------------------------------------------------
//
// Print Example
//
// This prints 2 pages of text with the Page and line number
// displayed.
//
//
//-----------------------------------------------------------
#include 
#include 
#include 
#define PAGESTOPRINT 2
// this function gets the handle to the default printer's Device Context so
// that we may make GDI calls to it.
HDC GetDefaultPrinterDC(void)
{
//return GetDC(GetDesktopWindow);
char szPrinter[80];
char *szDevice;
GetProfileString("windows", "device", ",,,", szPrinter, 80);
if (NULL != (szDevice = strtok(szPrinter, ",")))
return CreateDC(NULL, szDevice, NULL, NULL);
return NULL;
}
int main(void)
{
char output[80] = { NULL };
int PageY;
TEXTMETRIC PtrTxtMet;
// declare DocInfo for use and set the name of the print job as
// 'Name Of Document'
static DOCINFO DocInfo = { sizeof(DOCINFO), "Name Of Document", NULL };
HDC PrintDC = GetDefaultPrinterDC();
// You may want to use the PageX var to see how wide an output you can use.
// Use GetTextExtentPoint32() API function to see how wide a particular
// string of chars is. Something like this:
/*
SIZE TextSize;
TheString = "The String";
GetTextExtentPoint32(
PrintDC, // handle of device context
TheString, // address of text string
strlen(TheString), // number of characters in string
&TextSize // address of structure for string size
);
*/
//PageX = GetDeviceCaps(PrintDC, HORZRES); // Width of printer page in pixels
PageY = GetDeviceCaps(PrintDC, VERTRES); // Height of printer page in pixels
if (PrintDC) {
GetTextMetrics(PrintDC, &PtrTxtMet);
if(StartDoc(PrintDC, &DocInfo)> 0) { // Begin Print Job
for(int Pages = 0; Pages < PAGESTOPRINT; ++Pages) {
if(StartPage(PrintDC)> 0) { // start a printer page
int Y = 0;
while(Y < PageY) {
Y += (PtrTxtMet.tmHeight + PtrTxtMet.tmExternalLeading);
sprintf(output, "Page: %d, Line %d ", Pages + 1,
Y/(PtrTxtMet.tmHeight + PtrTxtMet.tmExternalLeading));
TextOut(PrintDC, // Device context
0, // X coordinate
Y, // Y coordinate
output, // string to output
strlen(output) // size of string in chars
);
} // Page while loop
// feed in a new page
if (!EndPage(PrintDC)> 0) {
printf("Print Error: EndPage() Failed!");
DeleteDC(PrintDC);
return 1;
// Checks for errors, call GetLastError() to determine
// the nature of the problem. There is some code in
// FormatMessage documentation for doing this effectively.
// exits app if problem occurs.
}
} // if StartPage()
else
printf("Print Error: StartPage() Failed!");
} // Pages FOR loop
EndDoc(PrintDC); // Finish Print Job
} // if StartDoc()
DeleteDC(PrintDC);
} // if PrintDC
return 0;
}
7/2/98 10:32:32 AM

Last Modified: 01-SEP-99