Dynamic Generation and Response of Controls in VC Programs

xiaoxiao2021-03-06  23

Developer Network> Development Tools> Development Columns> VC> Text

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 · BS_CHECKBOX check button, not commonly used

· BS_AUTO3STATE three-state check button with automatically selected state

· 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:

Full text reading: Dynamic generation and response of controls in VC program

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

New Post(0)