Dynamically creating controls to support event responses and to save and read

Source: Internet
Author: User
Tags ini static class

The creation of dynamic controls in vc++6.0 is a relatively deviating knowledge and has some difficulty. Its full functionality is to dynamically create a control and then dynamically respond to events in the control, both of which are complete.

Here I'll show you a complete dynamic control example that dynamically creates controls and then dynamically responds to control events, and saves control information to the INI configuration file and then reads the control information based on the INI file to dynamically create the control. Believe it can solve many of the problems you encounter in dynamic controls.

Of course, there are many ways to control the dynamic, and I'm just showing what I think is better.

Here, take the vc++6.0 Create dialog project as an example, add a menu, add a subkey button, a text box, a label. You know, VC basically have to rely on handwriting, so, here first write three control of the creation of other controls basically consistent.

The first step, of course, is to create a global class (oh this is just my personal preference) with full global shared data, which I will put into a *global.h file. There is also a control class that contains the attributes that each control needs, such as the name, size, coordinates, and additional of the control.

In addition to those two classes, the most important is the control class, because it can be cumbersome to add a response to an event by using the system's control class (implementation is not too difficult, mainly bad management). Add a method I think we all know that is to use the system's Add Class Wizard to generate three classes (we now only do three different types of controls) one inherits from the Button class (CButton)-the button one inherits from the Edit box class (CEdit)-The text box, one inherits from the static Class (CStatic)-label, Would you mind naming Cmybutton,cmyedit,cmylabel separately? If you use manual additions, the powerful event Class wizard will not work.

There should be two system classes, three control classes, right? Of course, the control classes also add their header and program files to the project. The following is the design control class. I have considered using multiple inheritance to make these three types of custom control classes inherit the control class (the control class added in the first step is temporarily called the control main class as good) but it is more convenient not to use it. Now just declare the three custom controls in the control's main class and then add some common public information from the beginning as follows:////////////////
//控件主类
////////////////
class _myControl
{
public:
//共公信息
  CString caption;//标题
  CRect rect;//坐标大小
  int type;//类型
//动态控件
  CMyButton myButton;
  CMyEdit myEdit;
  CMyLabel myLabel;
  _myControl()
  {
    caption="Control";
    type=0;//默认为按钮
    rect=CRect(10, 80, 100, 120);//初始坐标大小
  }
};

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.