時間上,oncreate,oninitial兩者先后順序不同,構造函數(shù)生成本類的對象,但沒有產生窗口,OnCreate后窗口產生,然后才是視圖的OnInitialUpDate,一般在這里對視圖的顯示做初始化。簡單點,就是ONCREATE只是產生VIEW的基本結構和變量而在OnInitialUpDate()中,主要初始化視圖中控件等。對各個變量進行初始化操作。
例子。我們要在視圖中添加一個button和combobox控件則
OnCreate函數(shù)中寫法如下:int CFormView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
CRect rect(20,20,100,50);
m_ctrlButton.Create("Button1",WS_CHILD|WS_VISIBLE,rect,this,NULL);//創(chuàng)建按扭控件
CFont *pFont=CFont::FromHandle((HFONT)::GetStockObject(ANSI_VAR_FONT));
CRect rect1(150,20,350,100);
m_combobox.Create(WS_CHILD|WS_VISIBLE|CBS_SIMPLE|CBS_NOINTEGRALHEIGHT|WS_VSCROLL,rect1,this,NULL);
return 0;
}
OnInitialUpDate中寫法
void CFormView::OnInitialUpdate()
{
CView::OnInitialUpdate();
// TODO: Add your specialized code here and/or call the base class
//初始化組合框控件
m_combobox.AddString("Mondy");
m_combobox.AddString("Tuesday");
m_combobox.AddString("Wednesday");
m_combobox.AddString("Thursday");
m_combobox.AddString("Saturday");
m_combobox.AddString("Sunday");
}
另外CVIEW::OnInitialUpdate()和CDLG::OnInitDialog()出現(xiàn)時間都差不多,前者指的是在view馬上顯示之前由框架去調用的,后者是before the dialog box display ;所以OnInitDialog()也在Create()和CreateIndirect()或domodal()之后.