CTestDlg dlg;
dlg.DoModal();
對于模態(tài)對話框,對應(yīng)有個(gè)函數(shù)EndDialog,在MSDN中的解釋是這樣的:
Call this member function to terminate a modal dialog box. This member function returns nResult as the return value of DoModal(DoModal的返回值作為一個(gè)參數(shù)傳遞給EndDialog). You must use the EndDialog function to complete processing whenever a modal dialog box is created.
You can call EndDialog at any time, even in OnInitDialog, in which case you should close the dialog box before it is shown or before the input focus is set.
EndDialog does not close the dialog box immediately. Instead, it sets a flag that directs the dialog box to close as soon as the current message handler returns.
2. 創(chuàng)建非模態(tài)對話框:
注:不能像聲明模態(tài)對話框那樣聲明Dialog的局部對象,Dialog也是屬于資源的一種,如果定義為局部對象,則會(huì)在函數(shù)的時(shí)候析構(gòu)掉。
有兩種辦法解決:一種是將對話框資源聲明為類的成員變量,一種是聲明指針變量,申請堆內(nèi)存,因?yàn)槎褍?nèi)存是在程序結(jié)束后才清空資源的。
CTestDlg *pDlg=new CTestDlg(); //最好還是將此行放在構(gòu)造函數(shù)執(zhí)行,然后在析構(gòu)函數(shù)delete();
pDlg->Create(IDD_DIALOG1,this);
pDlg->ShowWindow(SW_SHOW); //參數(shù)定義了顯示模式
3. 對于對話框上自動(dòng)創(chuàng)建的OK按鈕,對應(yīng)的消息響應(yīng)函數(shù)述OnOk,在模態(tài)對話框上該函數(shù)調(diào)用后是銷毀窗口,但在非模態(tài)對話框上,該函數(shù)的調(diào)用只是隱藏對話框,在MSDN中的解釋如下:
If you implement the OK button in a modeless dialog box, you must override the OnOK member function and call DestroyWindow from within it. Don’t call the base-class member function, because it calls EndDialog, which makes the dialog box invisible but does not destroy it.
4. 動(dòng)態(tài)創(chuàng)建按鈕:
添加類的成員變量:CButton btn;
//Button派生于CWnd,所以如果Create成功,成員m_hWnd會(huì)擁有該按鈕的句柄,否則為空
if(btn.m_hWnd==NULL)
{
//如果成功創(chuàng)建一個(gè)button后,再次創(chuàng)建的同一個(gè)button話,即同一個(gè)資源,程序會(huì)出錯(cuò)。
btn.Create("test",BS_PUSHBUTTON,CRect(0,0,100,100),this,100);
btn.ShowWindow(SW_SHOW); //按鈕也是一個(gè)對話框,所以也需要顯示
//以上兩條語句可以用一條語句代替
btn.Create("test",BS_PUSHBUTTON | WS_VISIBLE,CRect(0,0,100,100),this,100);
}
else
{
btn.DestroyWindow(); //銷毀窗口,將btn.m_hWnd設(shè)置成NULL
}
5. 文本控件的七種調(diào)用方式(示例代碼演示將文本框1和2中的數(shù)字相加輸出到文本框3中):
CString num1,num2,num3;
GetDlgItem(IDC_NUM1)->GetWindowText(num1);
GetDlgItem(IDC_NUM2)->GetWindowText(num2);
int result=atoi(num1)+atoi(num2); //atoi函數(shù)將char*轉(zhuǎn)為int
num3.Format("%d",result);
GetDlgItem(IDC_NUM3)->SetWindowText(num3);
CString num1,num2,num3;
GetDlgItemText(IDC_NUM1,num1);
GetDlgItemText(IDC_NUM2,num2);
int result=atoi(num1)+atoi(num2);
num3.Format("%d",result);
SetDlgItemText(IDC_NUM3,num3);
int num1,num2,num3;
num1=GetDlgItemInt(IDC_NUM1);
num2=GetDlgItemInt(IDC_NUM2);
m_num3=m_num1+m_num2;
SetDlgItemInt(IDC_NUM3,num3);
UpdateData(TRUE);
m_num3=m_num1+m_num2;
UpdateData(FALSE);
CString num1,num2,num3;
m_edit1.GetWindowText(num1);
m_edit2.GetWindowText(num2);
int result=atoi(num1)+atoi(num2);
num3.Format("%d",result);
m_edit3.SetWindowText(num3);
注:將整形變量或控件變量和控件ID進(jìn)行關(guān)聯(lián)是在函數(shù)DoDataExchange中進(jìn)行的,它是由框架自動(dòng)調(diào)用的一個(gè)函數(shù)。DoDataExchange函數(shù)完成對數(shù)據(jù)的交換和校驗(yàn),其中是一些以DDX和DDV字符開始的函數(shù),前者完成數(shù)據(jù)交換,后者完成數(shù)據(jù)校驗(yàn),一般如設(shè)置了整型變量的最大最小值,就會(huì)出現(xiàn)DDV校驗(yàn)函數(shù)
a. Win32 API:
注:WM_GETTEXT消息將控件中的字符拷貝到指定的buffer中,WM_SETTEXT功能相反
CString num1,num2,num3;
::SendMessage(GetDlgItem(IDC_NUM1)->m_hWnd,WM_GETTEXT,10,(LPARAM)num1.GetBuffer(10));
::SendMessage(GetDlgItem(IDC_NUM2)->m_hWnd,WM_GETTEXT,10,(LPARAM)num 2.GetBuffer(10));
int result=atoi(num1)+atoi(num2);
num3.Format("%d",result);
::SendMessage(GetDlgItem(IDC_NUM3)->m_hWnd,WM_SETTEXT,0,(LPARAM)(LPCTSTR)num3);
b. MFC函數(shù):
CString num1,num2,num3;
GetDlgItem(IDC_NUM1)->SendMessage(WM_GETTEXT,10,(LPARAM)num1.GetBuffer(10));
GetDlgItem(IDC_NUM2)->SendMessage(WM_GETTEXT,10,(LPARAM)num2.GetBuffer(10));
int result=atoi(num1)+atoi(num2);
num3.Format("%d",result);
GetDlgItem(IDC_NUM3)->SendMessage(WM_SETTEXT,0,(LPARAM)(LPCTSTR)num3);
CString num1,num2,num3;
SendDlgItemMessage(IDC_NUM1,WM_GETTEXT,10,(LPARAM)num1.GetBuffer(10));
SendDlgItemMessage(IDC_NUM2,WM_GETTEXT,10,(LPARAM)num2.GetBuffer(10));
int result=atoi(num1)+atoi(num2);
num3.Format("%d",result);
SendDlgItemMessage(IDC_NUM3,WM_SETTEXT,0,(LPARAM)(LPCTSTR)num3);
附:對文本框中的指定部分內(nèi)容進(jìn)行選中:消息EM_SETSEL
CString num1,num2,num3;
SendDlgItemMessage(IDC_NUM1,WM_GETTEXT,10,(LPARAM)num1.GetBuffer(10));
SendDlgItemMessage(IDC_NUM2,WM_GETTEXT,10,(LPARAM)num2.GetBuffer(10));
int result=atoi(num1)+atoi(num2);
num3.Format("%d",result);
SendDlgItemMessage(IDC_NUM3,WM_SETTEXT,0,(LPARAM)(LPCTSTR)num3);
SendDlgItemMessage(IDC_NUM3,EM_SETSEL,1,3);
GetDlgItem(IDC_NUM3)->SetFocus(); //必須將焦點(diǎn)移動(dòng)到當(dāng)前需要設(shè)置的文本框上
6. 模擬窗口的縮放功能:
static CRect oldRect;
static CRect newRect;
CString str;
if(GetDlgItem(IDC_ACTION)->GetWindowText(str),str=="收縮")
{
GetDlgItem(IDC_ACTION)->SetWindowText("放大");
// GetWindowRect以屏幕坐標(biāo)的方式得到當(dāng)前CWnd對象,即對話框的矩形區(qū)域
GetWindowRect(&oldRect);
CRect sepratorRect; //分隔條,用picture控件模擬
GetDlgItem(IDC_SEPRATOR)->GetWindowRect(&sepratorRect);
newRect.left=oldRect.left;
newRect.top=oldRect.top;
newRect.right=oldRect.right;
newRect.bottom=sepratorRect.bottom;
//SetWindowPos: Call this member function to change the size, position, and Z-order of child, pop-up, and top-level windows.
SetWindowPos(&wndTopMost,newRect.left,newRect.top,newRect.Width(),
newRect.Height(),SWP_SHOWWINDOW);
}
else
{
GetDlgItem(IDC_ACTION)->SetWindowText("收縮");
SetWindowPos(&wndTopMost,oldRect.left,oldRect.top,oldRect.Width(),
oldRect.Height(),SWP_SHOWWINDOW);
}
7. Z-Order
窗口的Z次序表明了重疊窗口堆中窗口的位置,這個(gè)窗口堆是按一個(gè)假想的軸定位的,這個(gè)軸就是從屏幕向外伸展的Z軸。Z次序最上面的窗口覆蓋所有其它的窗口,Z次序最底層的窗口被所有其它的窗口覆蓋。應(yīng)用程序設(shè)置窗口在Z次序中的位置是通過把它放在一個(gè)給定窗口的后面,或是放在窗口堆的頂部或底部。
Windows系統(tǒng)管理三個(gè)獨(dú)立的Z次序——一個(gè)用于頂層窗口、一個(gè)用于兄弟窗口,還有一個(gè)是用于最頂層窗口。最頂層窗口覆蓋所有其它非最頂層窗口,而不管它是不是活動(dòng)窗口或是前臺(tái)窗口。應(yīng)用程序通過設(shè)置WS_EX_TOPMOST風(fēng)格創(chuàng)建最頂層窗口。
一般情況下,Windows系統(tǒng)把剛剛創(chuàng)建的窗口放在Z次序的頂部,用戶可通過激活另外一個(gè)窗口來改變Z次序;Windows系統(tǒng)總是把活動(dòng)的窗口放在Z次序的頂部,應(yīng)用程序可用函數(shù)BringWindowToTop把一個(gè)窗口放置到Z次序的頂部。函數(shù)SetWindowPos和DeferWindowPos用來重排Z次序。
8. 為窗口上的控件設(shè)置Tab順序:
查看Tab順序Layout->Tab Order
傳遞控件的焦點(diǎn)GetNextDlgTabItem(GetFocus())->SetFocus();
9. CDialog的消息WM_INITDIALOG表示在對話框的子控件等創(chuàng)建好,在對話框的被顯示前調(diào)用,他和WM_CREATE消息的區(qū)別是,后者調(diào)用的時(shí)候?qū)υ捒蛏系淖涌丶€沒有創(chuàng)建完成。