-
2008-08-21
如何删除从CWnd或者CView派生类申请的内存。 - [MFC]
当我们销毁一个从CView派生对象是,会执行下面的过程。
一、CView的销毁
void CView::OnDestroy()
{
CFrameWnd* pFrame = GetParentFrame();
if (pFrame != NULL && pFrame->GetActiveView() == this)
pFrame->SetActiveView(NULL); // deactivate during death
CWnd::OnDestroy();
}
二、调用CWnd::OnDestroy(),该函数被调用之后,调用OnNcDestroy
void CWnd::OnDestroy()
{
#ifndef _AFX_NO_OCC_SUPPORT
// cleanup control container
delete m_pCtrlCont;
m_pCtrlCont = NULL;
#endif
// Active Accessibility
if (m_pProxy != NULL)
m_pProxy->SetServer(NULL, NULL);
if (m_pStdObject != NULL)
m_pStdObject->Release();
Default();
}
三、void CWnd::OnNcDestroy(),该函数是在销毁客户区域之后,被调用用来销毁非客户区域的函数,也是销毁
窗口之前最后调用的一个函数。该函数执行现场清理任务,最后调用需函数PostNcDestroy()
四、CView::PostNcDestroy()
// self destruction
void CView::PostNcDestroy()
{
// default for views is to allocate them on the heap
// the default post-cleanup is to 'delete this'.
// never explicitly call 'delete' on a view
delete this;
}
由此可见从CView派生的视图。我们在new之后,在销毁窗口时候仅仅需要调用DestroyWindow()函数即可。
void CWnd::PostNcDestroy()
{
// default to nothing
}
而由于CWnd的PosNcDestroy函数什么都没有做。所以从CWnd派生的类,在new之后,在销毁时需要
1.调用DestroyWindow()
2.delete obj -
2008-08-16
Window打印体系 - [MFC]
一、Windows 打印体系结构
1.获得打印机设备描述表
应用程序使用默认的打印机,则使用下面方法来创建设备描述表。
CDC dc;
CPrintDialog dlg(FALSE);
dlg.GetDefaults();
dc.Attach(dlg.GetPrinterDC);
2.标记打印开始
DOCINFO di;
::ZeroMemory(&di, sizeof(DOCINFO));
di.cbSize = sizeof(DOCINFO);
di.lpszDocName = _T("Budget Figures for the Current Fiscall Year");
dc.StarDco(&di):
3.打印
for(int i = 0; i <= nPageCount; i++)
{
dc.StartPage();
//
//Print page i
//
dc.EndPage();
}
4.结束打印
dc.EndDoc();
if(dc.StartDco(&di))
{
BOOL bContinue = TRUE;
for(int i = 1; i <= nPageCount && bContinue; i++)
{
dc.StartPage();
//initialize the device context
//print page i;
if(dc.EndPage() <= 0)
bContinue = FALSE;
}
if(bContinue)
dc.EndDoc();
else
dc.AbortDoc();
}







