C++MFC Programming Note day08 MFC dialog box use

Source: Internet
Author: User

An MFC dialog box
1 classification
Mode and non-modal
2 related classes
CDialog Class-The parent class is a CWnd, essentially a window, the parent class of a dialog box class.
Ccommondialog class and subclass-Common dialog box, Color dialog box, file dialog box,
Find Replacement dialog box, Font Settings dialog box, Print Setup dialog box, and
Print dialog box.
CPropertyPage Class-Property Pages dialog box.
3 in the Win32 Wizard, use the MFC class to create a dialog box program
3.1 Modal dialog box
3.1.1 Creating and Displaying dialog boxes
CDialog::D omodal ()
3.1.2 The dialog box is closed (no user processing required)
Cdialog::onok/oncancel


3.2 Non-modal dialog boxes
3.2.1 Creating and Displaying dialog boxes
Similar to the General window creation process
3.2.2 The close of the dialog box
1 override the Cdialog::onok/oncancel function in the function:
DestroyWindow ()
2 rewriting CWnd::P the Ostncdestroy function, in the function:
Delete this;
3.3 Creating a single document program, using the menu to open the mode/non-modal dialog box separately
Compare the effects of run time
3.4 Tracking the execution of the DoModal () function
3.4.1 Finding and Loading dialog resources
3.4.2 to set the parent window to a non-available state
3.4.3 Create dialog box
3.4.4 the message loop into the dialog box
3.4.5 when the OK or Cancel button is clicked, exit the loop
3.4.6 hides the dialog window and sets the parent window to the available state
3.4.7 Destroy dialog box
3.4.8 Releasing a dialog box resource
3.4.9 returns the return value of the DoModal () function
Example:
Create a new Win32 application, and then support MFC instead, creating the code manually:
DlgApp.cpp:Defines the entry point for the application.
//


#include "stdafx.h"//#include <afxwin.h>
#include "resource.h"
Class Cmydlg:public CDialog
{
Public
CMyDlg (): CDialog (IDD_DIALOG1) {}//Association Dialog Resource ID
The following 3 functions are used when using a modeless dialog box to override the implementation
virtual void PostNcDestroy ();
virtual void OnOK ();
virtual void OnCancel ();
};
void Cmydlg::onok ()
{
Cdialog::onok ();
DestroyWindow ();
}
void Cmydlg::oncancel ()
{
Cdialog::oncancel ();
DestroyWindow ();
}
void CMyDlg::P Ostncdestroy ()
{
CWnd::P Ostncdestroy ();
Delete this;
}
Class Cdlgapp:public CWINAPP
{
Public
Virtual BOOL InitInstance ();
};
Cdlgapp Theapp;
BOOL cdlgapp::initinstance ()
{
/* Modal dialog box
CMyDlg dlg;
m_pmainwnd=&dlg;
Dlg. DoModal ();//Create and display modal dialog boxes
*/
Non-modal dialog box
CMyDlg *pdlg=new CMyDlg;
Pdlg->create (IDD_DIALOG1);//Dialog Resource ID
M_pmainwnd=pdlg;
Pdlg->showwindow (Sw_show);
return TRUE;
}








Data exchange technology for two dialog boxes (DDX)
It is introduced to facilitate the manipulation of controls and to get/set the value of the control
You can bind a control to a member variable of a dialog box by manipulating the member variable of the dialog box to
The purpose of the manipulation control is reached.
1 Correlation functions
1.1 Series of binding functions
DDX_Control, a variable that binds a control type
DDX_Text, a variable that binds a value type
For example, there is an edit box control in the dialog box, id,id_edit1
Variables in dialog class, CString m_strtext;//value type
int m_ntext;//variable of value type


CEdit variables of the m_wndtext;//control type
1.2 Dialog data exchange function with 1.1 binding functions
CWnd::D Odataexchange
1.3 When there is data exchange between the value type (CString) member variable and the control, 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 placed on the control display
2 use
3 principle
3.1 Binding functions for ddx_control-control types
DDX_Control (cdataexchange* PDX, ...)
{
1 Gets the handle of the control based on the ID of the control
HWND Hwndctrl = Pdx->preparectrl (NIDC);
2 mapping a variable of a control type to a handle to a control
Rcontrol.subclasswindow (Hwndctrl)
{
Attach (Hwndctrl);
}
}
3.2 Binding functions for ddx_text-value types
DDX_Text (cdataexchange* pDX, int nIDC,...)
{
1 Gets the handle of the control based on the ID of the control
HWND Hwndctrl = Pdx->preparectrl (NIDC);
2 variables and controls are assigned 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 function of the call relationship, why we will be in Cmydlg::oninitdialog (), first
Call its parent class CDialog::OnInitDialog ()?
Only call CDialog::OnInitDialog () before the dialog box is displayed before the
The control is bound to a variable. The specific invocation relationship:
CDialog::OnInitDialog ()
{
UpdateData (FALSE);
{
DoDataExchange (PDX);
{
DDX_Control ();
DDX_Text ();
}
}
}


example, build the Win32 application, and then manually change to support MFC and then write the code:
DlgDDX.cpp:Defines the entry point for the application.
//


#include "stdafx.h"//#include <afxwin.h>
#include "resource.h"
Class Cmydlg:public CDialog
{
Public
CMyDlg (): CDialog (IDD_DIALOG1) {}//Association Dialog Resource ID
The following 3 functions, when non-modal dialog boxes are overridden, are implemented
virtual void PostNcDestroy ();
virtual void OnOK ();
virtual void OnCancel ();
Virtual BOOL OnInitDialog ();
virtual void DoDataExchange (cdataexchange* pDX);
CButton m_wndok;//controls bound with OK buttons
CString m_wndedit1;
};
void CMyDlg::D odataexchange (cdataexchange* PDX)
{
CDialog::D odataexchange (PDX);
DDX_Control (Pdx,idok,m_wndok);//interface controls are bound to member variables
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::P Ostncdestroy ()
{
CWnd::P Ostncdestroy ();
Delete this;
}
dialog box initialization
BOOL Cmydlg::oninitdialog ()
{
if (! CDialog::OnInitDialog ())
{
return FALSE;
}
cwnd* pbtn = GetDlgItem (IDCANCEL);//Get control
Pbtn->enablewindow (FALSE);//set to not available


M_wndok.movewindow (0,0,100,100,true);//action bound member variable equals action 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 ()
{
* Modal dialog box
CMyDlg dlg;
m_pmainwnd=&dlg;
Dlg. DoModal ();//Create and display modal dialog boxes
//*/


return TRUE;
}




Three implementation login function
Before the main window is displayed, the login dialog is set and the login is passed before the main window is displayed:
Cdialoglogin Dlglogin;
if (Idok!=dlglogin. DoModal ()) return false;
To add a control to a value binding:
Ctrl+w, select the member Variables tab and add the variable that corresponds to the control.
Log in to the dialog box to handle:
void Cdialoglogin::onok ()
{
UpdateData (TRUE);//update the control value into the variable
if (m_username== "123" && m_userpwd== "123")//Analog Login
{
Cdialog::onok ();
}
Else
{
AfxMessageBox ("User name or password is wrong! ");
}
}


Example:
1. Creating an MFC dialog Box application

2. Add the dialog box, ID idd_dialog_login, and modify the interface control.


3, double-click the dialog box title, select Create a new class, add the corresponding class, the class name is Cdialoglogin.


4. Ctrl+w Add member Bindings:


5. In the Inininstance function of the app, add the code:
Cdialoglogin Dlglogin;
if (Idok!=dlglogin. DoModal ()) return false;
This means that the main frame window is not displayed if the login box does not return IDOK.


To add a message map when you click the OK button:






Cdialoglogin in the implementation of the click-Login code:
void Cdialoglogin::onok ()
{
UpdateData (TRUE);
if (m_username== "123" && m_userpwd== "123")//Analog Login
{
Cdialog::onok ()///Default Close dialog box, return IDOK
}
Else
{
AfxMessageBox ("User name or password is wrong! ");
}

}




Related Article

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.