导航
  • 主页
  • VC综合技术
  • 互联网技术
  • MFC AppLauncher
  • .NET 技术
  • VC/MFC界面
  • VC/MFC 进程
  • VC/MFC 数据库
  • VC++技术资源

继承类自定义函数以修改打印方向debug出错release正常

waitlife 2009-08-08 08:26:07
原类中的函数是这样的:

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);
如果我不添加修改打印方向的那段代码,都没有任何问题,估计是修改打印方向后,有内存没有正确释放,请各位高手帮忙看看该怎么改这段代码,谢谢!
...全文
125 1 收藏 4
写回复
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
srvsw 2009-08-20
改成这样:

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);
}

回复
waitlife 2009-08-10
没人会,还是分太少?
回复
waitlife 2009-08-09
请帮忙呀,谢谢
回复
waitlife 2009-08-08
请大家帮忙,谢谢!
回复
发动态
发帖子
VC/MFC
创建于2007-09-28

1.5w+

社区成员

VC/MFC相关问题讨论
申请成为版主
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……