C ++ MFC programming notes: Use of the day08 MFC dialog box

Source: Internet
Author: User

C ++ MFC programming notes: Use of the day08 MFC dialog box
1. MFC dialog box
1 category
Mode and non-Mode
2 related classes
CDialog class-the parent class is CWnd, which is essentially a window and the parent class of the dialog box class.
CCommonDialog class and subclass-General dialog box, color dialog box, file dialog box,
Find and replace dialog box, font Setting dialog box, print Setting dialog box, and
Print dialog box.
CPropertyPage class-property page dialog box.
3. In the Win32 wizard, use the MFC class to create a dialog box Program
3.1 mode dialog box
3.1.1 create and display dialog box
CDialog: DoModal ()
3.1.2 close the dialog box (no need for user processing)
CDialog: OnOK/OnCancel


3.2 non-mode dialog box
3.2.1 create and display dialog box
Similar to the normal window creation process
3.2.2 close the dialog box
1 rewrite the CDialog: OnOK/OnCancel function, in the function:
DestroyWindow ()
2 rewrite the CWnd: PostNcDestroy function. In the function:
Delete this;
3.3 create a single document program and use the menu to open the mode/non-mode dialog box respectively
Compare the Running Effect
3.4 trace the execution process of DoModal () Functions
3.4.1 search for and load dialog box Resources
3.4.2 set the parent window to unavailable
3.4.3 create dialog box
3.4.4 enter the message loop in the dialog box
3.4.5 when you click OK or the Cancel button, exit the loop.
3.4.6 hide the dialog box and set the parent window to available.
3.4.7 destruction dialog box
3.4.8 release dialog box Resources
3.4.9 return the return value of the DoModal () function
Example:
Create a win32 application, support MFC, and manually create code:
// DlgApp. cpp: Defines the entry point for the application.
//


# Include "stdafx. h" // # include
# Include "resource. h"
Class CMyDlg: public CDialog
{
Public:
CMyDlg (): CDialog (IDD_DIALOG1) {}// resource ID of the associated dialog box
// The following three functions must be rewritten when the non-mode dialog box is used
Virtual void PostNcDestroy ();
Virtual void OnOK ();
Virtual void OnCancel ();
};
Void CMyDlg: OnOK ()
{
CDialog: OnOK ();
DestroyWindow ();
}
Void CMyDlg: OnCancel ()
{
CDialog: OnCancel ();
DestroyWindow ();
}
Void CMyDlg: PostNcDestroy ()
{
CWnd: PostNcDestroy ();
Delete this;
}
Class CDlgApp: public CWinApp
{
Public:
Virtual BOOL InitInstance ();
};
CDlgApp theApp;
BOOL CDlgApp: InitInstance ()
{
/* Mode dialog box
CMyDlg dlg;
M_pMainWnd = & dlg;
Dlg. DoModal (); // create and display mode dialog box
*/
// Non-mode dialog box
CMyDlg * pDlg = new CMyDlg;
PDlg-> Create (IDD_DIALOG1); // resource ID of the dialog box
M_pMainWnd = pDlg;
PDlg-> ShowWindow (SW_SHOW );
Return TRUE;
}








Data Exchange Technology in dialog box 2 (DDX)
It is introduced to facilitate control operations and to obtain/set control values.
You can bind a widget to a member variable in the dialog box.
To achieve the purpose of the operation control.
1. Related functions
1.1 A series of binding Functions
DDX_Control: binds control-type variables.
DDX_Text, bound to a value type variable
For example, the dialog box contains an edit box control, ID, ID_EDIT1
Variable in the dialog box class, CString m_strText; // variable of the Value Type
Int m_nText; // Value Type Variable


CEdit m_wndText; // Control Type Variable
1.2 dialog box data exchange function, including 1.1 binding function
CWnd: DoDataExchange
1.3 When data exchange exists between the value type (CString) member variables and controls, the following function is called:
UpdateData ()
UpdateData (TRUE)-the value of the control is passed to the member variable.
UpdateData (FALSE)-the value of the member variable is displayed on the control.
2. Use
3 Principle
3.1 DDX_Control-Control Type Binding Function
DDX_Control (CDataExchange * pDX ,...)
{
// 1 obtain the control handle Based on the Control ID
HWND hWndCtrl = pDX-> PrepareCtrl (nIDC );
// 2 map the control-type variables to the control handle
RControl. SubclassWindow (hWndCtrl)
{
Attach (hWndCtrl );
}
}
3.2 DDX_Text-value-Type Binding Function
DDX_Text (CDataExchange * pDX, int nIDC ,...)
{
// 1 obtain the control handle Based on the Control ID
HWND hWndCtrl = pDX-> PrepareCtrl (nIDC );
// 2 variables and controls assign values to each other
If (pDX-> m_bSaveAndValidate)
{
Int nLen =: GetWindowTextLength (hWndCtrl );
: GetWindowText (hWndCtrl, value. GetBufferSetLength (nLen), nLen + 1 );
Value. ReleaseBuffer ();
}
Else
{
AfxSetWindowText (hWndCtrl, value );
}


}
4. Why is the call relationship of the function in CMyDlg: OnInitDialog ()? First
Call its parent class CDialog: OnInitDialog ()?
Only CDialog: OnInitDialog () can be called before the dialog box is displayed
Control and variable binding. Specific call relationship:
CDialog: OnInitDialog ()
{
UpdateData (FALSE );
{
DoDataExchange (pDx );
{
DDX_Control ();
DDX_Text ();
}
}
}


For example, create a win32 application, manually change to support MFC, and then write the code:
// DlgDDX. cpp: Defines the entry point for the application.
//


# Include "stdafx. h" // # include
# Include "resource. h"
Class CMyDlg: public CDialog
{
Public:
CMyDlg (): CDialog (IDD_DIALOG1) {}// resource ID of the associated dialog box
// The following three functions must be rewritten in the non-mode dialog box.
Virtual void PostNcDestroy ();
Virtual void OnOK ();
Virtual void OnCancel ();
Virtual BOOL OnInitDialog ();
Virtual void DoDataExchange (CDataExchange * pDX );
CButton m_wndOK; // control bound to the OK button
CString m_wndEdit1;
};
Void CMyDlg: DoDataExchange (CDataExchange * pDX)
{
CDialog: DoDataExchange (pDX );
DDX_Control (pDX, IDOK, m_wndOK); // The control is bound to the member variable on the interface.
DDX_Text (pDX, IDC_EDIT1, m_wndEdit1 );
}
Void CMyDlg: OnOK ()
{
UpdateData (TRUE );
AfxMessageBox (m_wndEdit1 );
// CDialog: OnOK ();
// DestroyWindow ();
}
Void CMyDlg: OnCancel ()
{
CDialog: OnCancel ();
DestroyWindow ();
}
Void CMyDlg: PostNcDestroy ()
{
CWnd: PostNcDestroy ();
Delete this;
}
// Dialog box Initialization
BOOL CMyDlg: OnInitDialog ()
{
If (! CDialog: OnInitDialog ())
{
Return FALSE;
}
CWnd * pbtn = GetDlgItem (IDCANCEL); // obtain the control
Pbtn-> EnableWindow (FALSE); // set to unavailable


M_wndOK.MoveWindow (100,100, TRUE); // The member variable bound to the operation is equal to the Operation Control
M_wndOK.SetWindowText ("DDX_ OK ");
M_wndEdit1 = "123 ";
UpdateData (FALSE );
Return TRUE;
}
Class CDlgApp: public CWinApp
{
Public:
Virtual BOOL InitInstance ();
};
CDlgApp theApp;
BOOL CDlgApp: InitInstance ()
{
/// * Mode dialog box
CMyDlg dlg;
M_pMainWnd = & dlg;
Dlg. DoModal (); // create and display mode dialog box
//*/


Return TRUE;
}




3. Implement the logon Function
Before the main window is displayed, set the Login Dialog Box. The main window is displayed only when the login is successful:
CDialogLogin dlglogin;
If (IDOK! = Dlglogin. DoModal () return false;
Add control and value binding:
Ctrl + w, select the member variables tab, and add the variables corresponding to the control.
Login Dialog Box processing:
Void CDialogLogin: OnOK ()
{
UpdateData (TRUE); // update the control value to the variable
If (m_UserName = "123" & m_UserPwd = "123") // simulate Login
{
CDialog: OnOK ();
}
Else
{
AfxMessageBox ("incorrect user name or password! ");
}
}


Example:
1. Create an MFC Dialog Box Application

2. In the Add dialog box, Set ID to IDD_DIALOG_LOGIN and modify the interface control.


3. Double-click the dialog box title, select Create a new class, and add the corresponding class named CDialogLogin.


4. ctrl + w add member binding:


5. Add the code to the ininininstance function of the app:
CDialogLogin dlglogin;
If (IDOK! = Dlglogin. DoModal () return false;
It means that the main frame window is not displayed if the login box does not return the IDOK.


Add message ing when you click OK:






// Code for implementing click Login in CDialogLogin:
Void CDialogLogin: OnOK ()
{
UpdateData (TRUE );
If (m_UserName = "123" & m_UserPwd = "123") // simulate Login
{
CDialog: OnOK (); // by default, the K dialog box is returned.
}
Else
{
AfxMessageBox ("incorrect user name or password! ");
}

}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.