Original URL: http://www.cnblogs.com/tiwlin/archive/2013/05/08/3067966.html
dialog boxes and controls
dialog boxes are a common resource in Windows applications, and their primary function is to output information and receive input from users. A control is a special small window embedded in a dialog box or other parent window that is used to complete different input and output functions. Dialogs are closely related to controls and generally have controls on each dialog box that rely on these controls to interact with the user for information.
In MFC, the function of the dialog box is encapsulated in the CDialog class, and the CDialog class is a derived class of the CWnd class. As a window, a dialog box has all the functions of a window, just like any other window. A typical application of a dialog box is to open a dialog box with a menu command or toolbar button, and of course, the dialog can be used as the main interface for a program.
MFC provides a series of dialog box classes, and implements the dialog message response and processing mechanism. The CDialog class is the most important class in a dialog class, and the dialog class that we create in the program is generally a derived class of the CDialog class. The CDialog class is also the base class for all other dialog class classes, and its derived relationships are as follows:cobject>ccmdtarget>cwnd> CDialog. Common handler functions for dialog boxes
- Cdialog::cdialog () Defines a dialog box based on the dialog resource template by calling the derived class constructor.
- CDialog::D omodal () Activates the modal dialog box, which displays the dialog window.
- Cdialog::create () Creates a non-modal dialog window based on the dialog resource template. If the dialog box is not a visible property, you also need to display the Non-modal dialog window by calling the CWnd::ShowWindow () function.
- Cdialog::onok () When you click the OK button, the function is called, the Receive dialog box enters data, and the dialog box closes.
- Cdialog::oncancel () When you click the Cancel button or press ESC, the function is called, and the dialog box is closed without receiving dialog input data.
- The CDialog::OnInitDialog () Wm_initdialog message handler function, which sends a WM_INITDIALOG message when the DoModal or create function is called, and invokes the function to initialize before the dialog box is displayed.
- The Cdialog::enddialog () is used to close the modal dialog box window.
- CWnd::ShowWindow () Show or hide the dialog box window
- CWnd::D Estroywindow () Close and destroy the non modal dialog box
- CWnd::UpdateData () Sets or gets the data for a dialog box control by calling DoDataExchange ()
- CWnd::D odataexchange () is called by UpdateData () to implement dialog data exchange and cannot be called directly.
- Cwnd::getwindowtext () Gets the caption of the dialog window
- Cwnd::setwindowtext () Modify the caption of the dialog window
- Cwnd::getdlgitemtext () Gets the text content of the control in the dialog box
- Cwnd::setdlgitemtext () Sets the text content of the control in the dialog box
- Cwnd::getdlgitem () Gets a pointer to a control or child window
- Cwnd::movewindow () To Move the dialog box window
- Cwnd::enablewindow () Make the window disabled or available
General dialog box Workflow
Once you have defined a dialog box class, you can use this dialog class to declare a dialog object, a dialog box that can be displayed on the screen.
After declaring a dialog box object, you can invoke the dialog box class's member function DoModal () To create a dialog window and display a dialog box. For example, suppose you define a dialog class named CMyDialog, in order to display a dialog box on the screen, you can write the following code: CMyDialog Mydlg; Mydlg.domodal ();
The relationship between the function OnOK (), OnCancel () and DoModal ():
The dialog box generally has the OK and Cancel buttons, the Cdialog::onok () function is called when the OK button is clicked, and the Cdialog::oncancel () function is called when the Cancel button is clicked. Both the OnOK () function and the oncancal () function end the DoModal () function call, but DoModal () returns a different value. The OnOK () function enables DoModal () to return the Idok,oncancel () function so that DoModal () returns idcancal. Users can make different choices based on the return value of DoModal (). such as: if (mydlg.domodal () = = IDOK) {...}
The initialization of a dialog box can be performed in a function called in three different stages:
- dialog box class constructor;
- Wm_create message processing function;
- Wm_initdialog the message handler function.
It is usually initialized in the message handler function OnInitDialog () of the message wm_initdialog. When you receive the Wm_init-dialog message, the Frame of the dialog box is created, and each control in the dialog box is also set up, but they are not yet displayed on the screen. You can naturally set or optimize the appearance, size, position, and other properties of individual controls in the dialog box. The function OnInitDialog () is called by the member function DoModal ().
Common dialog box
To use a common dialog box in an MFC application, MFC provides classes that encapsulate these common dialog boxes. These common dialog class classes are derived from the Ccommondialog class, and the Ccommondialog class is also a derived class of the CDialog class.
MFC common dialog Box class
- CColorDialog Color Settings dialog box, select a different color
- CFileDialog File Access dialog box to open or save a file
- CFindReplaceDialog Find and Replace dialog boxes, find and replace text strings
- CFontDialog Font Settings dialog box, select a different font.
- Cpagessetupdialog Page Setup dialog box, set page number, header, etc.
- CPrintDialog Standard Print dialog box for print and print settings
- COleDialog the class and its derived classes are used to build the OLE dialog box
Standard controls
Windows provides controls in two categories: standard controls and common controls.
Standard controls: Static controls, edit boxes, buttons, list boxes, combo boxes, scroll bars, and so on. Standard controls are used to meet the requirements of most user interface programming.
Common controls: Sliders, progress bars, list-dependent controls, tree-view controls, and label controls, and the use of common controls to achieve a variety of application user interface styles.
A control is a standalone widget that Windows provides to accomplish a specific feature, making it easier to design the application dialog functionality, and acting as the primary role in the interaction between the dialog and the user, to complete the output of user input and program running. Control corresponds to an object of a CWnd-derived class, which is actually a window that allows you to move, display, or hide, disable, or make available controls by calling member functions of the window class, or you can reset properties such as their size and style. MFC encapsulates standard and common controls in the form of classes, most of which derive directly from the CWnd class.
Common MFC control classes
Instance
File dialog box CFileDialog, animation control CAnimateCtrl, ListBox control CListBox, its key code
void Cdlgcontroldlg::D odataexchange (cdataexchange* PDX) {//CAnimateCtrl mwndanimate;//CListBox mListFileName;// CString mstrfilepath;//CString Mstrfilename; CDialog::D odataexchange (PDX);DD X_control (PDX, idc_animate1, mwndanimate);//animation control's associated control variable DDX_Control (PDX, Idc_list_ FILENAME, mlistfilename);//list box control associated control variable ddx_lbstring (PDX, Idc_list_filename, mstrfilename);//string variable associated with ListBox control DDX_Text (PDX, Idc_edit_filepath, Mstrfilepath);//edit box control associated string Variable}//button event, open AVI file dialog box event void Cdlgcontroldlg::o Nbnclickedbuttonopenfile () {//cfiledialog--Open or Save the dialog box, the constructor's parameter Lpszfilterd format//1. Filter strings with "| |" As Terminator//2. Filter the different lines in the string to "|" Split//3. Each line has been divided into two parts, with "|" Split, (characters displayed in the following boxes | filter character information) TCHAR szfilter[]=l "video file (*.avi) |*.avi| All Files (*. *) |*.*| |"; CFileDialog dlg (True,null,null,ofn_hidereadonly,szfilter); if (Idok!=dlg. DoModal ()) {return;} Mstrfilepath=dlg. GetPathName ();//Displays the file path on the edit box control UpdateData (FALSE);//Displays the file path to the edit box control CString Strname=dlg. GetFileName (); if (lb_err==mlistfilename.findstring ( -1,strname)) {int nitem=mlistfilename.addstring (strName);// Save Path cstring*Ppath=new Cstring;*ppath=mstrfilepath;mlistfilename.setitemdata (Nitem, (DWORD) ppath);} Play ();} Double-click the list box event void Cdlgcontroldlg::onlbndblclklistfilename () {int Nsel=mlistfilename.getcursel ();//Gets the current selection if (nsel==lb _err) {return;} cstring* ppath= (cstring*) mlistfilename.getitemdata (Nsel);//Gets the file path saved in the list box Mstrfilepath=*ppath; UpdateData (FALSE); Play ();} Animation play member function void Cdlgcontroldlg::P lay () {Mwndanimate.open (Mstrfilepath); Mwndanimate.play (0,-1,-1);}
The result:
"Go" MFC dialog boxes and controls