VC dynamic control creation and dynamic creation

Source: Internet
Author: User
Dynamic controls refer Create () The created control, which is different from the control pre-placed in the dialog box.
1. Create a dynamic control:
For comparison, let's take a look at the creation of static controls.
When you place a static control, you must first create a container, usually a dialog box. In this case, drag the required control from the tool window in the dialog box editing window, and then modify the control as appropriate. ID , Set control properties. A static control is created. When the dialog box is displayed, the control on it is also displayed.
Static controls do not need to be called Create () Function.
The creation of dynamic controls is quite different. The following uses buttons as an example to describe how to create dynamic controls:
1. Create a widget ID No:
ID ID is the identifier of the control. You must set ID .
Open "String table" , Double-click the mouse on the blank line. ID In the Properties dialog box ID Enter in the edit box ID Such: Idc_mybutton , In Caption Enter the control title or annotation (note: Caption The box cannot be blank. If it is null, the creation will fail.) Here, I enter the text to be displayed on the button. -- Dynamic Button.
2. Create a control object:
Different types of controls should create different class objects:
Button Control
Cbutton
(Including common buttons, single-choice buttons, and check buttons)
Edit controls
Cedit
Static text Control
Cstatic
Tag Control
Ctabctrl
Rotate controls
Cspinbuttonctrl
Slide Control
Csliderctrl
Multi-information editing Control
Cricheditctrl
Progress bar Control
Cprogressctrl
Scroll bar Control
Csrcollbar
Combo Control
Ccombobox
List box Control
Clistbox
Image list control
Cimagectrl
Tree Control
Ctreectrl
Animation Control
Canimatectrl
In this example, we create Cbutton Class. Note that it cannot be defined directly Cbutton Objects, such: Cbutton m_mybut; This definition can only be used to define control variables for static controls, but not dynamic controls.
The correct method is to use NewCallCbuttonThe constructor generates an instance:
Cbutton * p_mybut = new cbutton ();
Then use CbuttonClassCreate ()Create a function. The prototype of this function is as follows:
Bool create (lstrap: lpszcaption, DWORD dwstyle, const rect & rect, cwnd * pparentwnd, uint NID );
Lpszcaption Is the text displayed on the button;
Dwstyle Specifies the button style, which can be a combination of the button style and window style. values include:
Window style:
Ws_child
Subwindow, which must have
Ws_visible
The window is visible and generally has
Ws_disabled
Disable window, used when creating a button in the initial gray unavailable state
Ws_tabstop
Available Tab Key Selection
Ws_group
GROUP: The first button in the group radio button.
Button style:
Bs_pushbutton
Push Button, that is, normal button
Bs_autoradiobutton
Radio button with auto-selected status
Bs_radiobutton
Single button, not commonly used
Bs_autocheckbox
Check button with auto-selected status
Bs_checkbox
Check button, not commonly used
Bs_auto3state
Three-state check button with auto-selected status
Bs_3state
Three-state check button, not commonly used
The above style specifies the button type to be created and cannot be used at the same time, but must have one.
Bs_bitmap
A bitmap is displayed on the button.
Bs_defpushbutton
Set as the default button. It is only used for the push-down button. Only one default button can be specified in a dialog box.
Rect Specify the button size and position;
Pparentwnd Indicates the parent window with buttons. It cannot be Null ;
Nid Specify ID Id. ID .
The Create () The functions are slightly different. For more information, see.
Example: P_mybut-> Create (" Dynamic Button
", Ws_child into ws_visible into bs_pushbutton, crect (20, 10, 80, 40), this, idc_mybutton );
In this way, in the current dialog box (20, 10) Width 60 , Height 30 The button text is " Dynamic Button " .
To make the creation process easier and easier, I have defined the following functions:

Cbutton * ctexteditorview: newmybutton (int nid, crect rect, int nstyle)
{
Cstring m_caption;
M_caption.loadstring (NID );// Button title
Cbutton * p_button = new cbutton ();
Assert_valid (p_button );
P_button-> Create (m_caption, ws_child paiws_visible paibs_pushbutton implements nstyle, rect, this, NID );// Create button
Return p_button;
}
Where M_caption.loadstring (NID) Is to read the button text from the string table, so in the create button ID The text should be set, Parameters Nstyle It is an extra style except a required style.
Below, I call this function to create three buttons and specify the first button as the default button. ID Pre-configured:

Cbutton * p_mybut [3];
P_mybut [0] = newmybutton (id_mybut1, crect (10, 20, 50, 35), bs_defpushbutton );
P_mybut [1] = newmybutton (id_mybut2, crect (55,20, 95,35), 0 );
P_mybut [2] = newmybutton (id_mybut3, crect (,), 0 );

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.