打印时调试断言错误
Posted
技术标签:
【中文标题】打印时调试断言错误【英文标题】:Debug assertion error on printing 【发布时间】:2014-03-26 00:04:30 【问题描述】:我有一个在 Visual Studio 2010 专业版中创建的简单文本编辑器。基本上我修改了VS2010向导自动生成的MFC MDI程序。问题是当我打印时,它在 viewrich.cpp 第 294 行给了我一个调试断言错误。我没有修改代码中与打印有关的任何内容,尽管我使用 Rich Edit 的方式可能有问题。这就是我掌握的所有信息。提前致谢。
Viewrich.cpp
BOOL CRichEditView::PaginateTo(CDC* pDC, CPrintInfo* pInfo)
// attempts pagination to pInfo->m_nCurPage, TRUE == success
ASSERT_VALID(this);
ASSERT_VALID(pDC);
CRect rectSave = pInfo->m_rectDraw;
UINT nPageSave = pInfo->m_nCurPage;
ASSERT(nPageSave > 1); // LINE 294
ASSERT(nPageSave >= (UINT)m_aPageStart.GetSize());
VERIFY(pDC->SaveDC() != 0);
pDC->IntersectClipRect(0, 0, 0, 0);
pInfo->m_nCurPage = (int)m_aPageStart.GetSize();
while (pInfo->m_nCurPage < nPageSave)
ASSERT(pInfo->m_nCurPage == (UINT)m_aPageStart.GetSize());
OnPrepareDC(pDC, pInfo);
ASSERT(pInfo->m_bContinuePrinting);
pInfo->m_rectDraw.SetRect(0, 0,
pDC->GetDeviceCaps(HORZRES), pDC->GetDeviceCaps(VERTRES));
pDC->DPtoLP(&pInfo->m_rectDraw);
OnPrint(pDC, pInfo);
if (pInfo->m_nCurPage == (UINT)m_aPageStart.GetSize())
break;
++pInfo->m_nCurPage;
BOOL bResult = pInfo->m_nCurPage == nPageSave;
pDC->RestoreDC(-1);
pInfo->m_nCurPage = nPageSave;
pInfo->m_rectDraw = rectSave;
ASSERT_VALID(this);
return bResult;
EmergenceView.cpp
IMPLEMENT_DYNCREATE(CEmergenceView, CRichEditView)
BEGIN_MESSAGE_MAP(CEmergenceView, CRichEditView)
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, &CRichEditView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, &CRichEditView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CEmergenceView::OnFilePrintPreview)
ON_WM_CONTEXTMENU()
ON_WM_RBUTTONUP()
ON_COMMAND(ID_MUTATE_GROUP, &CEmergenceView::OnMutateGroup)
ON_UPDATE_COMMAND_UI(ID_MUTATE_GROUP, &CEmergenceView::OnUpdateMutateGroup)
ON_COMMAND(ID_MUTATE_RANDOMISE, &CEmergenceView::OnMutateRandomise)
ON_UPDATE_COMMAND_UI(ID_MUTATE_RANDOMISE, &CEmergenceView::OnUpdateMutateRandomise)
ON_COMMAND(ID_HELP_STATISTICS, &CEmergenceView::OnHelpStatistics)
ON_UPDATE_COMMAND_UI(ID_HELP_STATISTICS, &CEmergenceView::OnUpdateHelpStatistics)
ON_COMMAND(ID_MUTATE_POETRIZE, &CEmergenceView::OnMutatePoetrize)
ON_COMMAND(ID_EDIT_SELECTALL, &CEmergenceView::OnEditSelectall)
END_MESSAGE_MAP()
// CEmergenceView construction/destruction
CEmergenceView::CEmergenceView()
// TODO: add construction code here
CEmergenceView::~CEmergenceView()
BOOL CEmergenceView::PreCreateWindow(CREATESTRUCT& cs)
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CRichEditView::PreCreateWindow(cs);
// CEmergenceView drawing
void CEmergenceView::OnDraw(CDC* /*pDC*/)
CEmergenceDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
// TODO: add draw code for native data here
// CEmergenceView printing
void CEmergenceView::OnFilePrintPreview()
#ifndef SHARED_HANDLERS
AFXPrintPreview(this);
#endif
BOOL CEmergenceView::OnPreparePrinting(CPrintInfo* pInfo)
// default preparation
return DoPreparePrinting(pInfo);
void CEmergenceView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
// TODO: add extra initialization before printing
void CEmergenceView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
// TODO: add cleanup after printing
【问题讨论】:
您能否发布断言的 MFC 代码以及启动打印的代码 查看OnPreparePrinting
(msdn.microsoft.com/en-us/library/a59dff1e.aspx) 的文档,看来您必须致电pInfo->SetMaxPage()
。
【参考方案1】:
断言说明了一切:
UINT nPageSave = pInfo->m_nCurPage;
ASSERT(nPageSave > 1);
这是要打印/分页的当前页面的值。它在 CPrintInfo 的构造函数中设置为 1。但是有些东西改变了它,使它成为 0 或负数。通常这个值完全由 RTF 打印输出控制。所以你必须做一些操纵它的事情。
您必须在 CPrintInfo 中设置最小页面和最大页面值(SetMinPage 和 SetMaxPage)。
【讨论】:
我已经设置了最小页面和最大页面,但它仍然给我同样的错误......以上是关于打印时调试断言错误的主要内容,如果未能解决你的问题,请参考以下文章
在 Visual C++ 2005 中开发的 Visual C++ 项目 - 在 Visual C++ 2010 中,打开菜单时调试断言失败,但发布模式有效,如何解决?