Printing a Document

After an application initializes the necessary variables, registers itsAbortProc function, anddisplays its modeless Cancel dialog box, it can start the print job bycalling the StartDoc function.

After the application begins a print job, it can define individual pagesin the document by calling theStartPage and EndPage functions andembedding the appropriate calls to GDI drawing functions within this bracket.After the application has defined the last page, it can close the document andend the print job by calling the EndDoc function.

The following example shows the code required to print a string of textand a bitmapped image. The string of text, centered at the top of the page,identifies the path and file name for the file that contains the bitmappedimage. The bitmapped image, centered vertically and horizontally on the page,is drawn so that the same proportions used to draw the image in theapplication's window are maintained. Note that when targeting a printer DC, usea DIB instead of a DDB -- that is whyStretchDIBits is used in thiscode.

    //Zero and then initialize the members of a DOCINFO structure.

 

   memset( &di, 0, sizeof(DOCINFO) );

   di.cbSize = sizeof(DOCINFO);

   di.lpszDocName = (LPCTSTR)TEXT("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((LPCTSTR)TEXT("StartDoc"), hWnd);

       goto Error;

    }

 

    //Inform the driver that the application is about to begin

    //sending data.

 

   nError = StartPage(pd.hDC);

    if(nError <= 0)

    {

       errhandler((LPCTSTR)TEXT("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(hWinDC, 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((LPCTSTR)TEXT("StretchDIBits Failed"), hWnd);

    }

 

 

    //Retrieve the width of the string that specifies the full path

    //and filename for the file that contains the bitmap.

 

   GetTextExtentPoint32W(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

    // pageis complete.

 

   nError = EndPage(pd.hDC);

 

    if(nError <= 0)

    {

       errhandler((LPCTSTR)TEXT("EndPage"), hWnd);

       goto Error;

    }

 

    //Inform the driver that document has ended.

 

   nError = EndDoc(pd.hDC);

    if (nError<= 0)

       errhandler((LPCTSTR)TEXT("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);

Because the pixels on a screen typically have different dimensions thanthe dots on a printer, it is necessary to scale bitmapped images to obtain aWYSIWYG effect. This is done by obtaining horizontal and vertical scalingfactors and then applying those factors to the width and height values passedto the StretchDIBits function. In thesample application, the scaling factors were obtained by retrieving thehorizontal and vertical logical-pixel count for the two devices. Once thescaling factors were obtained, they were used to adjust the bitmap width andheight.

To center the bitmap on the page, the application first computed the widthand height of the scaled bitmap. (The bitmap was scaled to maintain theoriginal proportions of the image.) These values were divided by two and thensubtracted from half of the width and height of the page. The result definesthe coordinates of the upper-left corner of the bitmap.

To center the text at the top of the page, the application called theGetTextExtentPoint32 function to retrieve the width and height of the string specifying thepath names and file names. Once these values were obtained, the applicationused the height to position the string three lines down the page and the widthto position the string horizontally centered on the page.

The following illustration shows a representation of the page that appearedwhen the application printed the bitmapped image in the Winlogo.bmp file. Thisillustration also depicts the variables used to position the text and toposition and scale the bitmap.

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值