// Retrieve the number of pixels-per-logical-inch in the // horizontal and vertical directions for the display upon which // the bitmap was created. These are likely the same as for // the present display, so we use those values here.
// Retrieve the number of pixels-per-logical-inch in the // horizontal and vertical directions for the printer upon which // the bitmap will be printed.
// Use StretchDIBits to scale the bitmap and maintain // its original proportions (that is, if the bitmap was square // when it appeared in the application's client area, it should // also appear square on the page).
// Compute the starting point for the text-output operation. The // string will be centered horizontally and positioned three lines // down from the top of the page.
// Determine whether the user has pressed the Cancel button in the // AbortPrintJob dialog box; if the button has been pressed, call // the AbortDoc function. Otherwise, inform the spooler that the // page is complete.
nError = EndPage(pd.hDC);
if (nError <= 0) { errhandler("EndPage", hwnd); goto Error; }
// Inform the driver that document has ended.
nError = EndDoc(pd.hDC); if (nError <= 0) errhandler("EndDoc", hwnd);
MSDN里面有一段代码
memset( &di, 0, sizeof(DOCINFO) );
di.cbSize = sizeof(DOCINFO);
di.lpszDocName = "Bitmap Printing Test";
di.lpszOutput = (LPTSTR) NULL;
di.lpszDataType = (LPTSTR) NULL;
di.fwType = 0;
// Begin a print job by calling the StartDoc function.
nError = StartDoc(pd.hDC, &di);
if (nError == SP_ERROR)
{
errhandler("StartDoc", hwnd);
goto Error;
}
// Inform the driver that the application is about to begin
// sending data.
nError = StartPage(pd.hDC);
if (nError <= 0)
{
errhandler("StartPage", hwnd);
goto Error;
}
// Retrieve the number of pixels-per-logical-inch in the
// horizontal and vertical directions for the display upon which
// the bitmap was created. These are likely the same as for
// the present display, so we use those values here.
hWinDC = GetDC(hWnd);
fLogPelsX1 = (float) GetDeviceCaps(hWinDC, LOGPIXELSX);
fLogPelsY1 = (float) GetDeviceCaps(hWindDC, LOGPIXELSY);
// Retrieve the number of pixels-per-logical-inch in the
// horizontal and vertical directions for the printer upon which
// the bitmap will be printed.
fLogPelsX2 = (float) GetDeviceCaps(pd.hDC, LOGPIXELSX);
fLogPelsY2 = (float) GetDeviceCaps(pd.hDC, LOGPIXELSY);
// Determine the scaling factors required to print the bitmap and
// retain its original proportions.
if (fLogPelsX1 > fLogPelsX2)
fScaleX = (fLogPelsX1 / fLogPelsX2);
else fScaleX = (fLogPelsX2 / fLogPelsX1);
if (fLogPelsY1 > fLogPelsY2)
fScaleY = (fLogPelsY1 / fLogPelsY2);
else fScaleY = (fLogPelsY2 / fLogPelsY1);
// Compute the coordinates of the upper left corner of the
// centered bitmap.
cWidthPels = GetDeviceCaps(pd.hDC, HORZRES);
xLeft = ((cWidthPels / 2) - ((int) (((float) bmih.biWidth)
* fScaleX)) / 2);
cHeightPels = GetDeviceCaps(pd.hDC, VERTRES);
yTop = ((cHeightPels / 2) - ((int) (((float) bmih.biHeight)
* fScaleY)) / 2);
// Use StretchDIBits to scale the bitmap and maintain
// its original proportions (that is, if the bitmap was square
// when it appeared in the application's client area, it should
// also appear square on the page).
if (StretchDIBits(pd.hDC, xLeft, yTop, (int) ((float) bmih.biWidth
* fScaleX), (int) ((float) bmih.biHeight * fScaleY), 0, 0,
bmih.biWidth, bmih.biHeight, lpBits, lpBitsInfo, iUsage,
SRCCOPY) == GDI_ERROR)
{
errhandler("StretchDIBits Failed", hwnd);
}
// Retrieve the width of the string that specifies the full path
// and filename for the file that contains the bitmap.
GetTextExtentPoint32(pd.hDC, ofn.lpstrFile,
ofn.nFileExtension + 3, &szMetric);
// Compute the starting point for the text-output operation. The
// string will be centered horizontally and positioned three lines
// down from the top of the page.
xLeft = ((cWidthPels / 2) - (szMetric.cx / 2));
yTop = (szMetric.cy * 3);
// Print the path and filename for the bitmap, centered at the top
// of the page.
TextOut(pd.hDC, xLeft, yTop, ofn.lpstrFile,
ofn.nFileExtension + 3);
// Determine whether the user has pressed the Cancel button in the
// AbortPrintJob dialog box; if the button has been pressed, call
// the AbortDoc function. Otherwise, inform the spooler that the
// page is complete.
nError = EndPage(pd.hDC);
if (nError <= 0)
{
errhandler("EndPage", hwnd);
goto Error;
}
// Inform the driver that document has ended.
nError = EndDoc(pd.hDC);
if (nError <= 0)
errhandler("EndDoc", hwnd);
Error:
// Enable the application's window.
EnableWindow(hwnd, TRUE);
// Remove the AbortPrintJob dialog box.
DestroyWindow(hdlgCancel);
// Delete the printer DC.
DeleteDC(pd.hDC);