?? 孫鑫vc++視頻教程筆記之---lesson 7 對話框.txt
字號:
類的繼承圖表
CObject CCmdTarget CWnd //由CWnd派生,是一個窗口類 CDialog
對話框分為模態對話框和非模態的對話框
CDialog::DoModal //virtual int DoModal() 調用DoModal()創建一個模態的對話框 它的返回值是做為CDialog::EndDailog成員函數的參數,這個參數用來關閉對話框 CDialog::EndDailog //用來關閉模態的對話框 CDialog::Create //創建非模態的對話框 //初始化一個CDialog對象,創建一個非模態的對話框(modeless dialog box) //把它將一個CDialog對象關聯起來 //BOOL Create( LPCTSTR lpszTemplateName, CWnd* pParentWnd = NULL); //BOOL Create( UINT nIDTemplate, CWnd* pParentWnd = NULL); //lpszTemplateName: 對話框模板的名字 //nIDTemplate: 對話框模板的ID號 //pParentWnd: 對話框父窗口的指針,如果為NULL,則對話框的父窗口將被設置為主應用程序窗口
模態對話框的顯示不用調用ShowWindow而非模態的對話框在調用Create創建完對話框之后,需要調用ShowWindow顯示對話框//ShowWindow(SW_SHOW)
CAUTION: 對于模態的對話框,在模態的對話框顯示時,應用程序是暫停執行的,所以模態的對話框對象可以 是局部對象,不會在模態的對話框顯示之前析構。 而對于非模態的對話框,應用程序不會暫停執行,所以非模態對話框的對象不能是局部對象,非模態的 對話框對象有兩種定義方法: 1:定義對話框成員變量 2:在堆上分配內存,在堆上分配的內存和我們整個應用程序的生命周期是一樣的,可以如下定義: CTestDlg *pDlg=new CTestDlg(); //注意內存看見的回收,防止memory leak CAUTION: 對于模態的對話框,當我們點擊OK或者Cancel按鈕時這個對話框窗口是被銷毀了的 對于非模態的對話框,這個對話框窗口并沒有被銷毀,只不過是隱藏了,當我們點擊OK時,由基類的 虛函數OnOK()響應 CDialog::OnOK //virtual void OnOK(); //如果你在一個非模態的對話框中實現了一個OK Button,你必須重載OnOK成員函數,在它內部調用 //DestroyWindow,不要調用基類的成員函數,因為它調用的是EndDialog,which makes the dialog //box invisible but does not destroy it. CButton::Create //BOOL Create(LPCTSTR lpszCaption, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID) //創建Button //按鈕也是子窗口,如果dwstyle中不包含WS_VISIBLE,則在創建完Button后,繼續調用ShowWindow()顯示 //按鈕
CWnd和任何由CWnd類派生出來的窗口類對象,內部都有一個成員變量m_hWnd保存和這個對象相關聯的窗口的句柄,沒有窗口和它關聯時,m_hWnd的值為NULL
靜態的文本框也是一個窗口,要獲取靜態文本框的文本,可以使用函數 CWnd::GetWindowText設置文本使用 CWnd::SetWindowText CWnd::GetWindowText //int GetWindowText( LPTSTR lpszStringBuf, int nMaxCount )const; //void GetWindowText( CString& rString ) const; CWnd::SetWindowText // void SetWindowText( LPTSTR lpszString) 對于靜態的文本框是不能接收通告消息的,如果讓它接收通告消息,需要把它的屬性的style的"Notify"項選上
atoi函數 int atoi( const char *string) //將數值字符,轉化為整形數值 char* _itoa( int value, char *string, int radix); //Convert an integer to a string //string 指向結果 //radix 2-36數值的*進制
訪問控件的方式 1、獲取對話框上控件的指針 CWnd::GetDlgItem CWnd* GetDlgItem( int nID ) const; void CWnd::GetDlgItem( int nID,HWND* phWnd ) const; 2、CWnd::GetDlgItemText //int GetDlgItemText( int nID, LPTSTR lpStr, int nMaxCount ) const; //int GetDlgItemText( int nID, CString& rString ) const;CWnd::SetDlgItemText //void SetDlgItemText( int nID, LPCTSTR lpszString); 3、CWnd::GetDlgItemInt //UINT GetDlgItemInt( int nID, BOOL* lpTrans = NULL, BOOL bSigned = TRUE ) const; //可以獲取控件的文本,并把它轉化為無符號整形 //如果遇到非數值字符,或者數值超過所能表示的最大值,將會發生錯誤。此時lpTrans指向0,如果沒有錯誤 //lpTrans接收一個非零的值,如果lpTrans為NULL,GetDlgItemInt不會對錯誤產生警告 //bSigned指示接收的數值字符是否是有符號的CWnd::SetDlgItemInt //void SetDlgItemInt( int nID, UINT nValue, BOOL bSigned = TRUE );
4、控件與成員變量的關聯,在DoDataExchange函數中void CTestDlg::DoDataExchange(CDataExchange* pDX){ CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CTestDlg) DDX_Text(pDX, IDC_EDIT1, m_num1); //將控件IDC_EDIT1與成員變量m_num1相關聯 DDX_Text(pDX, IDC_EDIT2, m_num2); DDX_Text(pDX, IDC_EDIT3, m_num3); //}}AFX_DATA_MAP} DDX_函數有很多種,關聯不同的控件,需要選擇不同的DDX_函數,如DDX_Scroll,DDX_Radio等 DoDataExchange的調用時間: //Called by the framework to exchange an validate dialog data //由框架調用,來交換和調用對話框的數據 //Never call this function directly.It is called by the UpdateData member function. //我們重來不會直接調用這個函數,它由UpdateData成員函數調用 //Call UpdateData to initialize a dialog box's control or retrive data from a dialog box //調用UpdateData來初始化一個對話框控件,或者從對話框獲得數據
CWnd::UpdateData //BOOL UpdatData( BOOL bSaveAndValidate = TRUE ) //Call the member function to initialize data in a dialog box, or to retrieve and validate //dialog data //Flag that indicates whether dialog box is being initialized (FALSE) or data is being retrieve //(TRUE) //The framework automatically calls UpdateData with bSaveAndValidate set to FALSE when a modal //dialog box is created in the default implementation of CDialog::OnInitDialog
在控件關聯的成員變量設置最大最小值后,DoDataExchange函數中會增加如下幾條語句
void CTestDlg::DoDataExchange(CDataExchange* pDX){ CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CTestDlg) DDX_Text(pDX, IDC_EDIT1, m_num1); //DDX : Dialog Data Exchange1 DDV_MinMaxInt(pDX, m_num1, 0, 100); //DDV : Dialog Data Validate DDX_Text(pDX, IDC_EDIT2, m_num2);2 DDV_MinMaxInt(pDX, m_num2, 0, 100); //DDV_函數也有很多,同DDX_函數一樣 DDX_Text(pDX, IDC_EDIT3, m_num3); //}}AFX_DATA_MAP}
獲取文本的消息:WM_GETTEXT An application sends a WM_GETTEXT message to copy the text that corresponds to a window into a buffer provided by the caller. To send this message,call the SendMessage function with the following parameters. SendMessage{ (HWND) hWnd, //handle to destination window WM_GETTEXT, //message to send (WPARAM) wParam, //number of the character to copy (LPARAM) lParam //text buffer } 設置文本的時候使用消息 WM_SETTEXT
SendMessage{ (HWND) hWnd, //handle to destination window WM_SETTEXT, //message to send (WPARAM) wParam, //not used;must be zero (LPARAM) lParam //window-text string (LPCTSTR) }
給對話框的子控件發送消息CWnd::SendDlgMessage LRESULT SendDlgItemMessage( int nID, UINT message, WPARAM wParam=0, LPARAM lParam = 0) //等價于我們現調用GetDlgItem再調用SendMessage 編輯框消息 EM_GETSEL //The EM_GETSEL message retrieves the starting and ending character positions of the current //selection in an edit control. You can send this message to either an edit control or a rich //edit control SendMessage{ (HWND) hWnd, //handle to destination window WM_GETTEXT, //message to send (WPARAM) wParam, //starting position (LPDWORD) (LPARAM) lParam //ending position (LPDWORD) } EM_SETSEL設置復選的開始位置和結束位置 SendMessage{ (HWND) hWnd, //handle to destination window WM_SETTEXT, //message to send (WPARAM) wParam, //starting position (LPARAM) lParam //ending position } //If the start is 0 and the end is -1, all the text in the edit control is selected. //If the start is -1,any current selection is deselected 設置窗口的焦點 CWnd::SetFocus CWnd* SetFocus();
總結: 對話框控件訪問七種方式 1、GetDlgItem()->Get(Set)WindowText() //常用 2、GetDlgItemText()/SetDlgItemText() 3、GetDlgItemInt()/SetDlgItemInt() 4、將控件和整型變量相關聯 //常用 5、將控件和控件變量相關聯 //常用 6、SendMessage() //不常用 7、SendDlgItemMessage() //不常用 對話框的收縮與擴展 CRect::IsRectEmpty //Determines whether CRect is empty.CRect is empty if the width and/or height are 0; CRect::IsRectNull //Determines whether the top, bottom, left,and right member variables are all equal to 0. CWnd::GetWindowRect void GetWindowRect( LPRECT lpRect) const; //獲取窗口矩形區域的大小 CWnd::SetWindowPos //BOOL SetWindowPos( const CWnd* pWndInsertAfter, int x,int y, int cx, int cy, UINT nFlags); SetWindowPos和DeferWindowPos用來重排Z-order //應用程序通過設置WS_EX_TOPMOST風格創建最頂層窗口。 //應用程序可用函數BringWindowToTop把一個窗口放置到Z次序的頂部。兄弟窗口 共享同一個父窗口的多個子窗口叫兄弟窗口。活動窗口 活動窗口是應用程序的頂層窗口 應用程序則調用函數SetActiveWindow來激活一個頂層窗口
前臺窗口和后臺窗口 在Windows系統中,每一個進程可運行多個線程,每個線程都能創建窗口。創建正在使用窗口的線程 稱之為前臺線程,這個窗口就稱之為前臺窗口。所有其它的線程都是后臺線程,由后臺線程所創建的 窗口叫后臺窗口。 用戶通過單擊一個窗口、使用ALT+TAB或ALT+ESC組合鍵來設置前臺窗口,應用程序則用函數 SetForegroundWindow設置前臺窗口。如果新的前臺窗口是一個頂層窗口,那么Windows系統就激活它, 換句話說,Windows系統激活相應的頂層窗口。 SetWindowPos???
SetWindowLong函數 //The SetWindowLong function changes an attribute of the specified window //SetWindowLong函數可以改變指定窗口的屬性 long SetWindowLong { HWND hWnd, // handle to window int nIndex, // offset of value to set long dwNewLong // new value } 當 nIndex=GWL_WNDPROC 可以Sets a new address for the window procedure 此時可以把dwNewLong設置成新的窗口過程的地址。
函數的返回值: If the function succeeds, the return value is the previous value of the specified 32-bit integer If the function fails , the return value is zero 當 nIndex=GWL_WNDPROC ,它的返回值就是以前的窗口過程的地址 當對話框上的子控件全部創建完畢,對話框將要顯示時發送消息:WM_INITDIALOG //Sent to a dialog box before the dialog box is displayed
子控件的創建時間在對話框創建之后,對話框將要顯示之前
窗口過程函數的寫法: LRESULT CALLBACK WindowProc{ HWND hwnd, //handle to window UINT uMsg, //message identifier WPARAM wParam, //first message parameter LPARAM lParam //second message parameter } 當處理的是WM_CHAR消息時,wParam表示字符的ASCII碼
CAUTION! 窗口過程函數是一個全局函數
全局的SetFocus函數 HWND SetFocus( HWND hwnd //handle to window ); 全局的GetNextWindow函數: HWND GetNextWindow( HWND hWnd, //handle to current window UINT uCmd // direction ); //當 uCmd=GW_HWNDNEXT 當前窗口的下一個窗口 //當 uCmd=GW_HWNDPREV 當前窗口的上一個窗口 編輯框的Style如果如果沒有復選Multiline屬性,則編輯框無法接受回車符發送的消息取消Multiline屬性是在 02:29:10 寫OnOK函數時
獲取窗口句柄的GetWindow全局函數 HWND GetWindow( HWND hWnd, // handle to original window UINT uCmd // relationship GetNextDlgTabItem函數 //The GetNextDlgTabItem function retrieves a handle to the first control that has the //WS_TABSTOP style that precedes(前面)( or follows)the specified control. HWND GetNextDlgTabItem( HWND hDlg, //handle to dialog box HWND hCtrl, //handle to known control 已知控件的句柄 bool bPrevious //direction flag 方向。If this parameter is TRUE, the function searches //for the previous control in the dialog box.If this parameter is FALSE, //the function searches for the next control in the dialog box );CWnd::GetNextWindow CWnd* GetNextWindow( UINT nFlag = GW_HWNDNEXT )const;//具有缺省的方向GW_HWNDNEXT
獲取當前具有輸入焦點的窗口的指針的函數: CWnd::GetFocus CWnd::GetWindow CWnd* GetWindow( UINT nCmd) const;
CWnd::GetNextDlgTabItem CWnd* GetNextDlgTabItem( CWnd* pWndCtrl, bool bPrevious = FALSE) const; //函數尋找具有WS_TABSTOP style的control,循環依次調用 菜單項 Layout->TabOrder顯示順序
void CTestDlg::OnOK() 什么時候調用????????
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -