16,548
社区成员




void CBCGPGridCtrl::Print ()
{
// Printing without the Document/View framework
ASSERT_VALID(this);
CDC dc;
CPrintDialog printDlg(FALSE);
if (printDlg.DoModal() == IDCANCEL)
return;
dc.Attach(printDlg.GetPrinterDC());
dc.m_bPrinting = TRUE;
CString strTitle;
strTitle.LoadString(AFX_IDS_APP_TITLE);
DOCINFO di;
::ZeroMemory (&di, sizeof (DOCINFO));
di.cbSize = sizeof (DOCINFO);
di.lpszDocName = strTitle;
BOOL bPrintingOK = dc.StartDoc(&di);
m_bIsPrinting = TRUE;
m_pPrintDC = &dc;
CPrintInfo printInfo;
printInfo.m_rectDraw.SetRect(0,0,
dc.GetDeviceCaps(HORZRES),
dc.GetDeviceCaps(VERTRES));
// -----------------------------------------
// Prepare page for printing, calc page size
// -----------------------------------------
int nFirstItem = 0;// By default print all grid items
int nLastItem = GetTotalItems () - 1;// from first row to the last
OnPreparePrintPages (&printInfo, nFirstItem, nLastItem);
OnBeginPrinting(&dc, &printInfo);
// ---------------
// Set up margins:
// ---------------
CRect rectMargins = OnGetPageMargins (&dc, &printInfo);
printInfo.m_rectDraw.DeflateRect (&rectMargins);
int nPagesCount = OnCalcPrintPages (&dc, &printInfo);
printInfo.SetMaxPage (nPagesCount);
CRect rectDraw = printInfo.m_rectDraw;
for (printInfo.m_nCurPage = printInfo.GetMinPage(); printInfo.m_nCurPage <= printInfo.GetMaxPage() && bPrintingOK; printInfo.m_nCurPage++)
{
printInfo.m_rectDraw = rectDraw;
dc.StartPage();
OnPrint(&dc, &printInfo);
bPrintingOK = (dc.EndPage() > 0);
}
OnEndPrinting(&dc, &printInfo);
if (bPrintingOK)
dc.EndDoc();
else
dc.AbortDoc();
m_bIsPrinting = FALSE;
m_pPrintDC = NULL;
dc.DeleteDC();
AdjustLayout ();
}
class CBCGPGridCtrlEx : public CBCGPGridCtrl
{
public:
void CBCGPGridCtrl::Print ()
{
// Printing without the Document/View framework
ASSERT_VALID(this);
PRINTDLG pd;
LPDEVMODE lpDevMode;
CDC dc;
CPrintDialog printDlg(FALSE);
pd.lStructSize = (DWORD)sizeof(PRINTDLG);
if(AfxGetApp()->GetPrinterDeviceDefaults(&pd))
{
lpDevMode = (LPDEVMODE)GlobalLock(pd.hDevMode);
if(lpDevMode)
{
lpDevMode->dmPaperSize = DMPAPER_A4;
lpDevMode->dmOrientation = dmOrientation;
}
GlobalUnlock(pd.hDevMode);
}
printDlg.m_pd.hDevMode = pd.hDevMode;
if (printDlg.DoModal() == IDCANCEL)
return;
dc.Attach(printDlg.GetPrinterDC());
dc.m_bPrinting = TRUE;
CString strTitle;
strTitle.LoadString(AFX_IDS_APP_TITLE);
DOCINFO di;
::ZeroMemory (&di, sizeof (DOCINFO));
di.cbSize = sizeof (DOCINFO);
di.lpszDocName = strTitle;
BOOL bPrintingOK = dc.StartDoc(&di);
m_bIsPrinting = TRUE;
m_pPrintDC = &dc;
CPrintInfo printInfo;
printInfo.m_rectDraw.SetRect(0,0,
dc.GetDeviceCaps(HORZRES),
dc.GetDeviceCaps(VERTRES));
// -----------------------------------------
// Prepare page for printing, calc page size
// -----------------------------------------
int nFirstItem = 0;// By default print all grid items
int nLastItem = GetTotalItems () - 1;// from first row to the last
OnPreparePrintPages (&printInfo, nFirstItem, nLastItem);
OnBeginPrinting(&dc, &printInfo);
// ---------------
// Set up margins:
// ---------------
CRect rectMargins = OnGetPageMargins (&dc, &printInfo);
printInfo.m_rectDraw.DeflateRect (&rectMargins);
int nPagesCount = OnCalcPrintPages (&dc, &printInfo);
printInfo.SetMaxPage (nPagesCount);
CRect rectDraw = printInfo.m_rectDraw;
for (printInfo.m_nCurPage = printInfo.GetMinPage(); printInfo.m_nCurPage <= printInfo.GetMaxPage() && bPrintingOK; printInfo.m_nCurPage++)
{
printInfo.m_rectDraw = rectDraw;
dc.StartPage();
OnPrint(&dc, &printInfo);
bPrintingOK = (dc.EndPage() > 0);
}
OnEndPrinting(&dc, &printInfo);
if (bPrintingOK)
dc.EndDoc();
else
dc.AbortDoc();
m_bIsPrinting = FALSE;
m_pPrintDC = NULL;
dc.DeleteDC();
AdjustLayout ();
}
}
void Print()
{
// Printing without the Document/View framework
ASSERT_VALID(this);
CDC dc;
CPrintDialog printDlg(FALSE);
if (printDlg.DoModal() == IDCANCEL)
return;
dc.Attach(printDlg.GetPrinterDC());
dc.m_bPrinting = TRUE;
// 打印方向调节(直接更改设备环境描述,有待评价)
PRINTDLG pd;
LPDEVMODE lpDevMode;
pd.lStructSize = (DWORD)sizeof(PRINTDLG);
BOOL bRet = AfxGetApp()->GetPrinterDeviceDefaults(&pd);
if(bRet)
{
//DEVMODE FAR *pDevMode = (DEVMODE FAR *)::GlobalLock(pd.hDevMode);
lpDevMode = (LPDEVMODE)::GlobalLock(pd.hDevMode);
if(lpDevMode)
{
lpDevMode->dmPaperSize = DMPAPER_A4;
lpDevMode->dmOrientation = m_dmOrientation;
dc.ResetDC(lpDevMode);
}
::GlobalUnlock(pd.hDevMode);
}