16,548
社区成员




BOOL CGdiPlusHelper::Print( HDC &hDC, CRect &rect, LPCTSTR lpszTitle /*= NULL*/ )
{
CPrintDialog dlg( FALSE, PD_PAGENUMS|PD_HIDEPRINTTOFILE|PD_SELECTION, NULL);
if( IDOK == dlg.DoModal() )
{
CDC *pPrinteDC = CDC::FromHandle( dlg.GetPrinterDC() );
int nPaperWidth = pPrinteDC->GetDeviceCaps( HORZRES );
int nPaperHeigh = pPrinteDC->GetDeviceCaps( VERTRES );
double dXFactor = double(rect.Width()) / double(nPaperWidth);
double dYFactor = double(rect.Height())/ double(nPaperHeigh);
double dMaxFact = max( dXFactor, dYFactor );
int nWidthSrc = rect.Width();
int nHeighSrc = rect.Height();
int nWidthDst = int(rect.Width() / dMaxFact);
int nHeighDst = int(rect.Height()/ dMaxFact);
HDC hDCMem = CreateCompatibleDC( hDC );
HBITMAP hBitmap = CreateCompatibleBitmap( hDC, nWidthDst, nHeighDst );
SelectObject( hDCMem, hBitmap );
SetStretchBltMode( hDCMem, COLORONCOLOR );
StretchBlt( hDCMem, 0, 0, nWidthDst, nHeighDst, hDC, rect.left, rect.top, nWidthSrc, nHeighSrc, SRCCOPY );
pPrinteDC->StartDoc( lpszTitle );
BitBlt( pPrinteDC->m_hDC, (nPaperWidth-nWidthDst)/2, (nPaperHeigh-nHeighDst)/2, nWidthDst, nHeighDst, hDCMem, 0, 0, SRCCOPY );
pPrinteDC->EndDoc();
DeleteObject( hDCMem );
DeleteObject( hBitmap );
return TRUE;
}
return FALSE;
}
// 你需要打印的DC句柄
HDC hDC
// 你需要打印的区域大小
CRect &rect
// 如果采用虚拟打印机,会生成一个打印文件。此参数为默认打印文件名。
LPCTSTR lpszTitle