VC Programming Technology (ii) define a simple dialog box

Source: Internet
Author: User

I. Define dialog box Resources

   
Continue with the program example used in the previous section. As shown in, add a custom dialog box in the program resource view:

 

   
The dialog box is relatively simple. It only adds an edit box control and a static text control that displays the prompt text. The OK button and the cancel button are automatically added by the system.

Ii. Define dialog box

   
With the dialog box resources, you must define a dialog box class associated with it to use it. As shown in, define the dialog box class:

 

 

   
With the dialog box class, to allow other objects (such as csysmenuview objects) to obtain control information in the dialog box, you need to add a public member variable for the dialog box class to save the input value of the edit box. Here, a string variable name is defined, as shown in:

 

 

   
There are multiple ways to edit the input value of the control in the dialog box. You can define the associated variables of the edit box control. The member variables of the control can be of two types: value variables, save User input. One is the control variable, which represents the current control object) m_name to get the input value:

 

 

   
You can also define the control variable m_edit in the edit box, as shown in:

 

   
After a member variable is added to the control, the system automatically generates the following code:

   
// Mydialog. h

   ......

   Class
Mydialog: Public cdialog
   {
      
// Construction
      
Public:
         
Cstring name; // The Public member variable of the mydialog class defined earlier. It is used to obtain data information outside the dialog box.
         
Mydialog (cwnd * pparent = NULL ); 
// Standard Constructor

      
// Dialog data
         
// {Afx_data (mydialog)
         
Enum {IDD = idd_dialog1 };
         CeditM_edit;
         CstringM_name;
         
//} Afx_data

    ......

   
// Mydialog. cpp

   ......

   
Mydialog: mydialog (cwnd * pparent)
       
: Cdialog (mydialog: IDD, pparent)
   {
      
// {Afx_data_init (mydialog)
      M_name = _ T ("");// Initialize the control member variable in the constructor
      
//} Afx_data_init
   }

   
// Dialog box data exchange function
   Void
Mydialog: dodataexchange (cdataexchange * PDX)
   {
      
Cdialog: dodataexchange (PDX );
      
// {Afx_data_map (mydialog)

      
// Associate the editing control and its member variables

      Ddx_control (PDX, idc_edit1, m_edit );
      Ddx_text (PDX, idc_edit1, m_name);
      
//} Afx_data_map
   }

   ......

   
The following shows how to add a message processing function for the OK button in the dialog box. You can add Message Processing for the OK button control (ID: idok) in the classwizard Class Wizard, or you can double-click the OK button in the dialog box resource diagram to add a message. The message processing function code of the OK button is as follows:

   Void
Mydialog: onok ()
   {

      
// The first method to obtain the input value of the editing box (idc_edit1 is the ID of the editing box number), and getdlgitem is the integration of the MFC class cdialog.

      
// Win32 Function
      
// Getdlgitem (idc_edit1)-> getwindowtext (name );

      
// Or:

      
// Getdlgitemtext (idc_edit1, name );

      
// Method 2
      
// Updatedata (true); // update the data so that m_name can obtain the input value of the edit box.
      
// Name = m_name;

      
// Method 3: Use the control variable m_edit in the edit box to read the input value.

      
M_edit.getwindowtext (name ); 

     

      
Cdialog: onok ();
    }

Iii. Use of the custom dialog box class

   
First, add a button in the Toolbar Control of the main window. Click this button to bring up a custom dialog box. As shown in, add a button bitmap in the toolbar resource map:

   
Double-click this button to edit its control numbers and other attributes.

   
As shown in, add the button message processing function to the csysmenuview class:

   
Add the following code to the generated onbuttonname () function:

   Void
Csysmenuview: onbuttonname ()
   {
      
// Todo: add your command handler code here
      
Mydialog mydia; // defines the dialog box Class Object
      
Mydia. domodal (); // display the custom dialog box
      
Cclientdc mydc (this );
      
Mydc. textout (10,100, mydia. Name); // obtain and edit the control value in the dialog box displayed in the client area of the window.
   }

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.