Dynamic Control refers to the need from time to time by Create () to create the controls, which pre-placed in the dialog box controls are different.
First, create dynamic controls:
In order to control, we first look at the creation of static control.
Static controls must be placed before the establishment of a container, generally dialog, and then we edit window in the dialog box, pulled out from the tool window on the necessary controls in the dialog box can be further amended as appropriate controls ID, Control attribute settings, a static controls on the creation of good, when the dialog box is displayed, the control will also be displayed.
Create static controls do not use () function to create.
And create dynamic controls are quite different, following a button as an example, look at the creation of dynamic control:
1. Establish control ID No.:
ID is the control of the logo, create controls needed to be set up for it an ID number.
Open the resources of the "String Table," in the blank line on the double-click the mouse, then will pop up a dialog ID attribute, in which the ID edit box ID, such as: IDC_MYBUTTON in control heading enter Caption or notes (Note: Caption box can not be empty for air will lead to the creation of failure), here is my input on the button to display text - Dynamic button.
2. Establish controls for:
Different types of controls should create different categories Target:
CButton button controls (including buttons, radio buttons and check button)
Edit Control CEdit
Static text control CStatic
Labelling controls CTabCtrl
Rotary controls CSpinButtonCtrl
Waterloo subscript controls CSliderCtrl
Multi-Edit Control CRichEditCtrl
Control of the progress CProgressCtrl
Scroll controls CSrcollBar
Portfolio box controls CComboBox
CListBox list box controls
Image Control List CImageCtrl
CTreeCtrl tree controls
Animation Control CAnimateCtrl
In this example we create a kind of common CButton button. Attention to the definition of CButton not directly targeted, such as: CButton m_MyBut Such a definition can only be used to control static definition of control variables, and can not be used for dynamic control.
The correct approach is to call new structure function generates a CButton examples:
CButton p_MyBut * = new CButton ();
Then CButton category Create () function to create the prototype are as follows:
BOOL Create (LPCTSTR lpszCaption, DWORD dwStyle, const RECT & rect, pParentWnd CWnd *, UINT nID);
LpszCaption button is displayed on the text; dwStyle designated button style, button style and can be a combination of window styles, values are:
Window Style:
WS_CHILD sub-window, there must be
WS_VISIBLE window visible, are generally
WS_DISABLED disable window, create the initial state can not be used for the grey button use
Tab button to select available WS_TABSTOP
WS_GROUP group for the radio button group in the first of a button
Button style:
BS_PUSHBUTTON under pressure buttons, which means that ordinary button
BS_AUTORADIOBUTTON automatically selected state of the radio button
BS_RADIOBUTTON radio button, and less frequently
BS_AUTOCHECKBOX automatically selected state-check button
BS_CHECKBOX check button is not used
BS_AUTO3STATE automatically selected with the three state-check button
BS_3STATE three-state check button is not used
Style more than the creation of a designated button type, can not be used, but there must be one.
BS_BITMAP button will be displayed bitmap
BS_DEFPUSHBUTTON set to the default button, and only under pressure-used button, a dialog box can specify a default button
Rect designated button on the size and location;
PParentWnd instructions with the parent window button, not to NULL;
NID designated button associated with the ID number, and use step in the creation of the ID.
Create different kind of controls () function is somewhat different, can refer to the relevant information.
Example: p_MyBut-> Create ( "dynamic button," WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, CRect (20,10,80,40), this, IDC_MYBUTTON);
In this way, we in the present in the dialog box (20,10), creating a wide 60, high 30, the button text for "dynamic button", and pressed buttons.
In order to create a more user-friendly process, I defined the following function:
CButton CTextEditorView *:: NewMyButton (int nID, CRect rect, int nStyle)
(
CString m_Caption;
M_Caption.LoadString (nID); / / admission buttons title
CButton p_Button * = new CButton ();
ASSERT_VALID (p_Button);
P_Button-> Create (m_Caption, WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | nStyle, rect, this, nID) / / Create button
Return p_Button;
)
Which m_Caption.LoadString (nID) from the string read button text in the table, in the creation of this ID button, the text should be set up, and nStyle parameters for style, in addition to the extra style.
Now, I call this function to create three buttons, and the designation as a button to the default button, the button ID has been set well in advance:
CButton * p_MyBut [3];
P_MyBut NewMyButton [0] = (ID_MYBUT1, CRect (10,20,50,35), BS_DEFPUSHBUTTON);
P_MyBut NewMyButton [1] = (ID_MYBUT2, CRect (55,20,95,35), 0);
P_MyBut NewMyButton [2] = (ID_MYBUT3, CRect (100,20140,35), 0);
Second, dynamic control response:
Dynamic control of the response function can not be used ClassWizard added, can only add manually. Still the button above as an example, we produced the button click response function.
1. MESSAGE_MAP add response function:
MESSAGE_MAP information in the table defines a response function, the format: Message (ID, function), when we use ClassWizard add function, it will automatically add AFX_MSG_MAP included in the interval from within, such as:
BEGIN_MESSAGE_MAP (CTextEditorView, CFormView)
/ / ((AFX_MSG_MAP (CTextEditorView)
ON_BN_CLICKED (IDC_ICONBUT0, OnIconbut0)
/ /)) AFX_MSG_MAP
END_MESSAGE_MAP ()
Do not manually add AFX_MSG_MAP added to the range to prevent ClassWizard not work, such as:
BEGIN_MESSAGE_MAP (CTextEditorView, CFormView)
/ / ((AFX_MSG_MAP (CTextEditorView)
ON_BN_CLICKED (IDC_ICONBUT0, OnIconbut0)
/ /)) AFX_MSG_MAP
ON_BN_CLICKED (ID_MYBUT1, OnMybut1)
ON_BN_CLICKED (ID_MYBUT2, OnMybut2)
ON_BN_CLICKED (ID_MYBUT3, OnMybut3)
END_MESSAGE_MAP ()
Click on the button which ON_BN_CLICKED news.
2. Added in the first function is defined in the document:
Add function with ClassWizard, will be the first document in the range of AFX_MSG add function definition, such as:
Protected:
/ / ((AFX_MSG (CTextEditorView)
Afx_msg void OnIconbut0 ();
/ /)) AFX_MSG
DECLARE_MESSAGE_MAP ()
We imitate this form is added to the definition of the function of the interval AFX_MSG trip:
Protected:
/ / ((AFX_MSG (CTextEditorView)
Afx_msg void OnIconbut0 ();
/ /)) AFX_MSG
Afx_msg void OnMybut1 ();
Afx_msg void OnMybut2 ();
Afx_msg void OnMybut3 ();
DECLARE_MESSAGE_MAP ()
3. Information on the preparation of response function:
These are in turn linked to the function and specific in the click of a button should be done after the completion of work in the function:
Void CTextEditorView:: OnMybut1 ()
(
MessageBox ( "Ha! You click a button dynamic.");
)
Void CTextEditorView:: OnMybut2 ()
(
……
)
Void CTextEditorView:: OnMybut3 ()
(
……
)
Apart from the response function buttons, you can also use the guidelines to be above the button, such as:
Modify the size and location of the button: p_MyBut [0] -> MoveWindow (……);
Laws button text: p_MyBut [0] -> SetWindowText (……);
Show / Hide button: p_MyBut [0] -> ShowWindow (……), and so on.
Third, recycling resources:
The dynamic control targets by the new generation, it will not be automatically released, it required manual release. Controls no longer in use, it can be deleted:
If (p_MyBut [0])
Delete p_MyBut [0];
These are dynamically generated button control method. Below, a look at the dynamic generation radio button issues. 4. Examples: radio button group dynamically generated
Radio button belongs CButton category, but because radio button group always used, so it in the production and use of ordinary button with a certain distinction.
There are three assumptions of a group of radio buttons, initial, the first radio button in a selected state.
Let us first look at the production of static methods: In the dialog box placed three radio button, set the following attributes:
Radio1 attributes: Visible, Group, Tab stop, Auto
Radio2 attributes: Visible, Tab stop, Auto
Radio3 attributes: Visible, Tab stop, Auto
This attribute put three radio button into a group, they only have a one selected, the dialog box if there are other group of radio buttons, will use Noninterference. But that also does not allow the state of a button is selected.
ClassWizard then used for this group of radio buttons add variables here only for a radio button to add variables. A variable called m_Radio, type int-elected. In the constructor function in the m_Radio ClassWizard value is set to -1, we put it into 0, so that procedures can be seen running a radio button selected in the state. , ClassWizard should also be used to add three radio button click response function, inside m_Radio value corresponds to amend the three radio button on it.
These are usually produced radio button group approach, we are to be dynamically generated, the main group to solve the button and click control issues. Following is the production steps:
1. Definition of the three radio button ID:
Open the resources of the "String Table," in which the added value of the three ID:
The first: ID for IDC_MYRADIO1, for radio Caption 1
The second: ID for IDC_MYRADIO2, for radio Caption 2
Third: ID for IDC_MYRADIO3, for radio Caption 3
Caption on the button for them to show that the text can be set up as needed.
2. CButton category with the Create () function generated three radio button:
For the sake of convenience, the definition of a function to generate radio button:
CButton CTextEditorView *:: NewMyRadio (int nID, CRect rect, int nStyle)
(
CString m_Caption;
M_Caption.LoadString (nID); / / admission buttons title
CButton p_Radio * = new CButton ();
ASSERT_VALID (p_Radio);
P_Radio-> Create (m_Caption, WS_CHILD | WS_VISIBLE | nStyle | WS_TABSTOP | BS_AUTORADIOBUTTON, rect, this, nID) / / Create button
Return p_Radio;
)
LoadString function () for "String Table" button text read, Create () function to set the radio button must attributes, including the Visible, Tab stop, Auto attributes.
NID parameters for the radio button ID, rect for the radio button size, nStyle addition to the necessary attributes for the other properties. Return value to the button at the new guidelines.
With this function, the creation of radio button group, as long as the order can call the function, radio button group's first radio button must be designated WS_GROUP attributes.
CButton * p_MyRadio [3];
P_MyRadio NewMyRadio [0] = (IDC_MYRADIO1, CRect (15,90,60105), WS_GROUP);
P_MyRadio NewMyRadio [1] = (IDC_MYRADIO2, CRect (15108,60123), 0);
P_MyRadio NewMyRadio [2] = (IDC_MYRADIO3, CRect (15126,60141), 0);
3. Definition radio button group of control variables, set up the first radio button selected for the state:
ClassWizard here can not add variables, and do not in DoDataExchange () add a control variable, as a dynamic component does not exist began in DoDataExchange () will cause added control variables operational errors. Here, we just arbitrarily in the first document type definition int variable as a control variables, such as:
Int m_SelRadio;
Set in the constructor function for its initial 0: m_SelRadio = 0;
In the above statement in the creation of the button with SetCheck () function set up the initial selected button:
CButton * p_MyRadio [3];
P_MyRadio NewMyRadio [0] = (IDC_MYRADIO1, CRect (15,90,60105), WS_GROUP);
P_MyRadio NewMyRadio [1] = (IDC_MYRADIO2, CRect (15108,60123), 0);
P_MyRadio NewMyRadio [2] = (IDC_MYRADIO3, CRect (15126,60141), 0);
P_MyRadio [m_SelRadio] -> SetCheck (1) / / set up a radio for selected state
In SetCheck () function, the parameters are set to select one that the state, not selected for the 0 state.
4. Add mouse click response function:
Mouse click of a button, its status has been automatically change, we need to change control here m_SelRadio the value of variables to track the radio button selected.
First MESSAGE_MAP mouse click in the information and response function link:
BEGIN_MESSAGE_MAP (CTextEditorView, CFormView)
/ / ((AFX_MSG_MAP (CTextEditorView)
ON_BN_CLICKED (IDC_ICONBUT0, OnIconbut0) / / add here ClassWizard
/ /)) AFX_MSG_MAP
ON_BN_CLICKED (IDC_MYRADIO1, OnMyRadio1) / / radio button 1
ON_BN_CLICKED (IDC_MYRADIO2, OnMyRadio2) / / radio button 2
ON_BN_CLICKED (IDC_MYRADIO3, OnMyRadio3) / / radio button 3
END_MESSAGE_MAP ()
Then in the first defined in the document MESSAGE_MAP click function:
Protected:
/ / ((AFX_MSG (CTextEditorView)
Afx_msg void OnIconbut0 () / / add here ClassWizard
/ /)) AFX_MSG
Afx_msg void OnMyRadio1 (); / / radio button 1
Afx_msg void OnMyRadio2 (); / / radio button 2
Afx_msg void OnMyRadio3 (); / / radio button 3
DECLARE_MESSAGE_MAP ()
Do not pay attention to the here and function in AFX_MSG interval, to prevent the use of influence ClassWizard.
Specific definition of the response function (this is added by hand, instead of joining the ClassWizard):
/ / Click a radio button void CTextEditorView:: OnMyRadio1 ()
(
M_SelRadio = 0;
)
/ / Click the radio button void CTextEditorView 2:: OnMyRadio2 ()
(
M_SelRadio = 1;
)
/ / Click the radio button void CTextEditorView 3:: OnMyRadio3 ()
(
M_SelRadio = 2;
)
5. Recycling resources:
Analysis of the structure function, the radio button to create recovery (can not use radio button immediately Recovery):
CTextEditorView:: ~ CTextEditorView ()
(
Int i;
For (i = 0; i <3; i + +)
(
If (p_MyRadio [i])
Delete p_MyRadio [i];
)
)
Dynamic Controls is more than the generation and response methods, a variety of controls slightly different approach, but ideas and the steps are similar to the examples above you can help. |