原类中的函数是这样的: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 ();
}
}
因为原来的类CBCGPGridCtrl::Print()函数不支持打印方向控制,所以自己继承这个类然后重写打印函数,可通过传入参数以实现打印方向控制,现在问题是调用时都没有问题,但是在DEBUG模式退出程序后,会出现调试错误,
函数:void AFXAPI AfxGlobalFree(HGLOBAL hGlobal),中断的行是ASSERT(GlobalFlags(hGlobal) != GMEM_INVALID_HANDLE);
如果我不添加修改打印方向的那段代码,都没有任何问题,估计是修改打印方向后,有内存没有正确释放,请各位高手帮忙看看该怎么改这段代码,谢谢!

解决方案 »

  1.   

    改成这样: 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);
    }