一、关于对话框:
1、如果直接在对话框资源上放置控件,谁来为控件分配内存?
在模式对话框关闭之后,谁来释放控件资源?
注意到,在默认的模式对话框上,确定按钮调用了OnOK()函数,跟踪此函数:
调用的是基类的虚函数:
void CDialog::OnOK()
{
if (!UpdateData(TRUE)) 第一个疑问
{
TRACE(traceAppMsg, 0, "UpdateData failed during dialog termination.\n");
// the UpdateData routine will set focus to correct item
return;
}
EndDialog(IDOK); 第二个疑问
}
其中,UpdateDate(TRUE)又调用了CDialog基类 CWnd的虚函数:
UpdateData(TRUE))什么作用呢?
BOOL CWnd::UpdateData(BOOL bSaveAndValidate)
{
ASSERT(::IsWindow(m_hWnd)); // calling UpdateData before DoModal?
CDataExchange dx(this, bSaveAndValidate);
// prevent control notifications from being dispatched during UpdateData
_AFX_THREAD_STATE* pThreadState = AfxGetThreadState();
HWND hWndOldLockout = pThreadState->m_hLockoutNotifyWindow;
ASSERT(hWndOldLockout != m_hWnd); // must not recurse
pThreadState->m_hLockoutNotifyWindow = m_hWnd;
BOOL bOK = FALSE; // assume failure
TRY
{
DoDataExchange(&dx);
bOK = TRUE; // it worked
}
CATCH(CUserException, e)
{
// validation failed - user already alerted, fall through
ASSERT(!bOK);
// Note: DELETE_EXCEPTION_(e) not required
}
AND_CATCH_ALL(e)
{
// validation failed due to OOM or other resource failure
e->ReportError(MB_ICONEXCLAMATION, AFX_IDP_INTERNAL_FAILURE);
ASSERT(!bOK);
DELETE_EXCEPTION(e);
}
END_CATCH_ALL
pThreadState->m_hLockoutNotifyWindow = hWndOldLockout;
return bOK;
}
关于UpdateDate函数,设置断点发现,该函数在CDialog启动的时候,和关闭的时候都调用。那么它有什么作用呢?
解释:
CWnd::UpdateData
调用此函数来初始化对话框中的数据,或者从对话框中接收数据
简单的说,如果Updatedata(TRUE) == 将控件的值赋值给成员变量;
Updatedata(FALSE) == 将成员变量的值赋值给控件;
EndDialog(IDOK);什么作用呢?
EndDialog是CDialog的函数,注意,不是CWnd的。
void CDialog::EndDialog(int nResult)
{
ASSERT(::IsWindow(m_hWnd));
if (m_nFlags & (WF_MODALLOOP|WF_CONTINUEMODAL))
EndModalLoop(nResult); 。。。。。。。。。。。。。。。。。。。。。CWnd的
::EndDialog(m_hWnd, nResult); 。。。。。。。。。。。。。。。。。。Cwnd的
}