Dynamic Generation and Response of Controls in VC Programs

xiaoxiao2021-03-06  14

Dynamic controls refer to the control created by CREATE () when needed, which is different from the controls placed in the dialog box. First, create a dynamic control: For the control, let's first take a look at the creation of static controls. When you place a static control, you must first create a container, which is generally a dialog. At this time, in the dialog editing window, drag out the desired control from the Tool window in the Tool window, then appropriately modify the control ID, set the control Attributes, a static control is created, and when the dialog is displayed, the controls on which it will be displayed. Static controls do not need to invoke the create () function to create. The creation of dynamic controls is very different. The following is the button as an example, see the creation process of dynamic controls: 1. Create control ID number: The ID number is the logo of the control, and you must set a ID number for it before creating the control. Open "String Table" in the resource, double-click the mouse on the blank line, and you will pop up an ID property dialog box, enter ID in the ID editing box, such as: IDC_MYBUTTON, enter the control title or annotation in CAPTION (Note : The caption box cannot be empty, causing the flight to create a failure), here I entered the text-dynamic button to display on the button. 2. Create control objects: Different kinds of controls should create different class objects: • Button control CButton CSpinButtonCtrl · slider controls CSliderCtrl · Rich edit control CRichEditCtrl · progress bar control CProgressCtrl · slider control CSrcollBar · combo box control CComboBox · list box control CListBox · image list control CImageCtrl · tree control CTreeCtrl · animation controls in this case we CAnimateCtrl Create a normal button of a CButton class. Note You cannot directly define CButton objects, such as: CButton M_MyBut; this definition can only be used to define control variables to static controls and cannot be used for dynamic controls. The correct approach is to use the New call CButton constructor to generate an instance:

CButton * p_mybut = new cbutton ();

Then create with the CBUTTON class Create () function, which is as follows:

Bool Create (LPCTSTR LPSZCAPTION, DWORD DWSTYLE, Const Rect & Rect, CWND * PParentWnd, UINT NID);

Lpszcaption is the text displayed on the button; the dWStyle specifies the button style, which can be a combination of the button style and window style, with the value:

Window style:

· WS_CHILD sub-window, must have

· WS_Visible window visible, generally

· WS_DISABLED disables the window, use the initial state as a button that is not available.

· WS_TABSTOP can be selected by TAB button

· WS_GROUP is set, the first button in the set button for grouping

Button style:

· BS_PUSHBUTTON Under Pressing Buttons, i.e., a normal button

· BS_AUTORADIOBUTTON radio button with automatic selected status

· BS_RADIOBUTTON radio button, not commonly used

· BS_AUTOCHECKBOX contains a check button that is automatically selected

· BS_CHECKBOX check button, not commonly used

· BS_AUTO3STATE contains three-state check button · BS_3STATE three-state check button, is not commonly used

The above style specifies the created button type and cannot be used at the same time, but must have one.

· Bit map will be displayed on the BS_bitmap button

· BS_DEFPUSHBUTTON is set to the default button, only for the lower pressure button, only one default button can only be specified in a dialog

· RECT specifies the size and position of the button;

· PParentWnd Indicates the parent window with the button and cannot be null;

· NID specifies the ID number associated with the button, use the ID number created in the previous step.

Create () functions of different control classes are slightly different, and you can refer to the relevant information.

Example: p_mybut-> Create ("Dynamic Button", WS_CHILD | WS_VISIBLE | BS_PUSHB /TON, CRECT (20, 10, 80, 40), this, IDC_MYBUTTON;

In this way, we create a width 60, a high 30, a button text, a downstream button, a "Dynamic Button" in the current dialog (20, 10).

In order to make the creation process more easily and easy to use, I define the following functions:

CButton * CTextEditorView :: NewMyButton (int nID, CRect rect, int nStyle) {CString m_Caption; m_Caption.LoadString (nID); // get the button 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;}

Where m_caption.loadString (NID) is the read button text from the string table, so when the button ID is created, the text should be set, and the parameter nStyle is an additional style except the necessary style.

Hereinafter, I call this function to create three buttons and specify the first button to the default button, the button's ID is pre-set:

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 (100, 20, 140, 35), 0);

Second, the response of dynamic controls:

The response function of the dynamic control cannot be added with ClassWizard and can only be added manually. Still taking the above button as an example, we make a click response function of the button.

1. Add a response function in Message_MAP:

The message response function is defined in the Message_MAP table, which is: message name (ID, function name), when we add a function to the ClassWizard, the interval enclosed in AFX_MSG_MAP, such as:

Begin_MESSAGE_MAP (CTEXTeditorView, CFormView) // {{AFX_MSG_MAP (CTEXTEDITORVIEW) ON_BN_CLICKED (IDC_ICONBUT0, ONICONBUT0) / /}} AFX_MSG_MAPEND_MESSAGE_MAP ()

Do not add manually added to the time interval AFX_MSG_MAP prevent ClassWizard does not work, such as: BEGIN_MESSAGE_MAP (CTextEditorView, CFormView) // {{AFX_MSG_MAP (CTextEditorView) ON_BN_CLICKED (IDC_ICONBUT0, OnIconbut0) //}} AFX_MSG_MAPON_BN_CLICKED (ID_MYBUT1, OnMybut1) ON_BN_CLICKED (Id_mybut2, onmybut2) ON_BN_CLICKED (ID_MYBUT3, ONMYBUT3) end_MESSAGE_MAP ()

Where ON_BN_Clicked is the button clicked message.

2. Add a function definition in the header file:

When adding a function with ClassWizard, add a function definition in the AFX_MSG interval of the header file, such as:

protected: // {{AFX_MSG (CTexteditorView) AFX_MSG Void OnCunt0 (); //}} AFX_MSGDECLARE_MESSAGE_MAP ()

We imitate this form, just add the function definition to the AFX_MSG interval.

protected: // {{AFX_MSG (CTextEditorView) afx_msg void OnIconbut0 (); //}} AFX_MSGafx_msg void OnMybut1 (); afx_msg void OnMybut2 (); afx_msg void OnMybut3 (); DECLARE_MESSAGE_MAP ()

3. Write a message response function:

The above is to associate the messages and functions, and the work that should be done after clicking the button is completed in the function:

Void ctexteditorview :: ONMYBUT1 ("Ha! You click the Dynamic button.");} void ctexteditorview :: onmybut2 () {...} void ctexteditorview :: onmybut3 () {...

In addition to the button's response function, you can also use the pointer to the pointer to the button, such as: modify the size and location of the button: p_mybut [0] -> MoveWindow (...); Modify button text: p_mybut [0] -> setWindowText (...); Show / Hide Button: P_Mybut [0] -> ShowWindow (...); etc.

Third, recycling resources: Since the dynamic control object is generated by New, it will not be automatically released by the program, so it needs to be manually released. You can delete it when the control is no longer used:

IF (p_mybut [0]) delete p_mybut [0];

The above is a method of dynamically generated by button control. Next, look at the dynamic generation of the radio button. Fourth, instance: Dynamic generation of radio button group

The radio button is also a CBUTTON class, but because the radio button is always used, it has a certain difference with the normal button in the production and use.

Assuming that there are three radio buttons to form a group, the first radio button is selected.

Let's first look at the static production method: Place three radio buttons in the dialog box, set the properties as follows:

· Radio1 Properties: Visible, Group, Tab Stop, Auto

· Radio2 properties: Visible, Tab Stop, Auto

· Radio3 properties: Visible, Tab Stop, Auto

Such attribute settings are divided into a set of three radio buttons, and they can only have one selected. If there are other grouping radio buttons in the dialog, they will not interfere with each other. But at this time, the first button has not yet been selected. Then add a variable with ClassWizard to this set of selects, just add variables to the first radio button. Set the variable name M_Radio, the type is selected as an INT type. In the constructor, ClassWizard sets the value of m_radio to -1, we change it to 0, which can see the first radio button when running the program. After that, you should also add a click response function to three radio buttons to modify the value of M_Radio inside. You can do it.

The above is a way to create a radio button group. Now we want to change to dynamic generation, mainly to resolve the button packet and click control. The following is the production step:

1. Define the id:

Open "String Table" in the resource, add three ID values ​​thereof:

· The first: ID is IDC_MYRADIO1, CAPTION is a radio 1

· The second: ID is IDC_myradio2, caption is a single selection 2

· The third: ID is IDC_MYRADIO3, CAPTION is a radio 3

Where CAPTION is the text to be displayed on the button, you can set it as needed.

2. Generate three radio buttons with the Create () function of the CButton class:

For convenience, first define a function to generate a radio button:

CButton * CTextEditorView :: NewMyRadio (int nID, CRect rect, int nStyle) {CString m_Caption; m_Caption.LoadString (nID); // get the button 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;}

Function loadString () is used to read the button text from "String Table", and the set button must be enabled in the create () function, including Visible, Tab Stop, Auto property.

The parameter NID is the radio button ID number, RECT is a radio button size, and nStyle is other attributes that must be in addition to attribute. The return value is a pointer to the new button.

With this function, create a radio button group as long as the function is called, where the first radio button of the radio button group must specify the WS_GROUP attribute.

CButton * p_MyRadio [3]; p_MyRadio [0] = NewMyRadio (IDC_MYRADIO1, CRect (15,90,60,105), WS_GROUP); p_MyRadio [1] = NewMyRadio (IDC_MYRADIO2, CRect (15,108,60,123), 0); p_MyRadio [2 ] = Newmyradio (IDC_MYRADIO3, CRECT (15, 126, 60, 141), 0);

3. Define the control variable of the radio button group, set the first radio button to select the status:

You can't add variables with ClassWizard, or add control variables in dodataExchange (), because the dynamic control does not exist at the beginning, adding control variables in DODATAEXChange () causes an error. Here we only need to define an int type variable in the header file as the control variable, such as: int m_selradio;

Set its initial value of 0: m_selradio = 0 in the constructor;

In the statement of the creation button above, set the button in the initial selection with the setCheck () function:

CButton * p_MyRadio [3]; p_MyRadio [0] = NewMyRadio (IDC_MYRADIO1, CRect (15,90,60,105), WS_GROUP); p_MyRadio [1] = NewMyRadio (IDC_MYRADIO2, CRect (15,108,60,123), 0); p_MyRadio [2 ] = Newmyradio (IDC_MYRADIO3, CRECT (15, 126, 60, 141), 0); p_myradio [m_selradio] -> setCheck (1); // Set the first single selection is selected

In the setCheck () function, the parameter is 1 indicating setting to select a state, indicating that the unselected state is not selected.

4. Add a mouse click response function:

After the mouse click a radio button, its status is automatically changed, here we also modify the value of the control variable m_selradio to track the selected radio button.

First link the mouse in Message_Map and the response function:

BEGIN_MESSAGE_MAP (CTextEditorView, CFormView) // {{AFX_MSG_MAP (CTextEditorView) ON_BN_CLICKED (IDC_ICONBUT0, OnIconbut0) // ClassWizard added here //}} AFX_MSG_MAPON_BN_CLICKED (IDC_MYRADIO1, OnMyRadio1) // radio button 1ON_BN_CLICKED (IDC_MYRADIO2, OnMyRadio2) // Single selection button 2on_bn_clicked (idc_myradio3, onmyradio3) // radio button 3END_MESSAGE_MAP ()

Then define a click function in the message_map of the header file:

protected: // {{AFX_MSG (CTextEditorView) afx_msg void OnIconbut0 (); // ClassWizard added here} //} AFX_MSGafx_msg void OnMyRadio1 (); // radio button 1afx_msg void OnMyRadio2 (); // radio button 2afx_msg Void onmyradio3 (); // radio button 3Declare_MESSAGE_MAP ()

Be careful not to add a function to the AFX_MSG interval to prevent the use of ClassWizard.

Define specific response functions (here you are joined by manual, not to join ClassWizard):

/ / Click the radio button 1 void ctexteditorview :: onmyradio1 () {m_selradio = 0;} // click radio button 2 void ctexteditorview :: onmyradio2 () {m_selradio = 1;} // click radio button 3 Void ctexteditorview :: onmyradio3 () {m_selradio = 2;

5. Recycling resources:

In the destructor, recycle the selected radio button (you can also reclaim it when you do not use the radio button):

CTextEditorView :: ~ ctexteditorview () {INT i; for (i = 0; i <3; i ) {if (p_myradio [i]) delete p_myradio [i];}}}} is the generation and response method of dynamic controls, each Different control practices are slightly different, but the ideas and steps are similar, I hope the above instances can help you.

转载请注明原文地址:https://www.9cbs.com/read-47107.html

New Post(0)