Windows Control Overview

Source: Internet
Author: User
The Windows control that will be introduced in this article refers to the standard control predefined in windows, such as button control, edit control, and list control. These predefined controls are actually a special subwindow for users to use the same Program . Like normal window classes, each predefined control defines its own appearance attributes and functions by its own window class. Windows provides the window class names of some standard controls in a predefined way. During programming, you only need to call the createwindow () function or createwindow Wex () function and pass in the pre-defined window class name as a parameter to create the corresponding control. When a user interacts with a control through a screen object, the control sends a wm_command notification message to the parent window in the form of a "notification message". The wparam parameter of the message contains the control identifier, the lpamam parameter contains the notification code and control handle in both the high and low fields, and the parent window completes message Response Processing.

Button controls

A button control is a type of control whose name is predefined by the system as a button. It has more than 10 different window styles, contains common push-down buttons, single-choice buttons, check buttons, and grouping boxes. The details are as follows:

Button style description
Bs_autocheckbox is similar to the check button. Click "select" and click "cancel" again.
Bs_autoradiobutton is similar to the single-choice button. After clicking this button, the selected icon will be moved from other single-choice buttons in the same group to the current option.
Bs_auto3state is similar to the three-state check button, but changes the status after the user clicks it.
Bs_checkbox check button
Bs_defpushbutton press button with a black border.
Bs_groupbox
Bs_lefttext is used with the same single-choice button or check button. The title is displayed on the left.
Bs_ownerdraw allows you to create an owner self-painted button.
Bs_pushbutton normal push button
Bs_radiobutton radio button
Bs_3state three-state check button

The preceding button style is usually used together with the window style. The buttons are created by the createwindow () function or the cbutton class member function create () of MFC:

// Button-type window style
DWORD style [9] = {bs_autocheckbox,
Bs_autoradiobutton,
Bs_auto3state,
Bs_checkbox,
Bs_defpushbutton,
Bs_groupbox,
Bs_pushbutton,
Bs_radiobutton,
Bs_3state };
// Dynamic creation button
For (INT I = 0; I <9; I ++)
{
M_ctrbutton [I]. create ("button", ws_visible | ws_child | ws_border | style [I], crect (10 + 110 * I, 10,100 + 110 * I, 40), this, idc_button1 + I );
}

If you need to process the message sent by the button to its parent window, you can add a message ing entry and corresponding message processing function in the class implementation of message response:

On_notification (ID, memberfxn)

Here, ID is the ID of the button for sending the notification message, and memberfxn is the message processing function. If the button uses the new operator to create a cbutton object in the heap, you must ensure that you can call Delete to destroy the object before closing the window. If the cbutton object is created on the stack, the object does not need to be explicitly destroyed. The application will be destroyed automatically when it exits.

Edit controls

The Edit Control created with the window class name "edit" is a rectangular area that can be used to accept user keyboard characters and can be edited in it. This control is a main means for the program to accept user character input. The input content is stored in the local heap of its parent window with limited capacity (32 KB.

The cedit class of MFC provides functions related to editing controls. The Edit control can be created on the dialog box template or throughCodeTo create a cedit object. Both methods use the constructor of cedit. The cedit class inherits some important functions from cwnd. For example, you can use the cwnd class member functions setwindowtext () and getwindowtext () to set and obtain the text in an editing control. Similar to a button control, if you want to process the notification message sent by the Edit Control to its parent window, you need to add a message ing entry and a message response function for each message to be processed in the parent window class.

When the CREATE () function is called to create an editing control, messages such as wm_nccreate, wm_nccalcsize, wm_create, and wm_getminmaxinfo are sent to the editing control in windows. By default, these messages are processed by onnccreate (), onnccalcsize (), oncreate (), ongetminmaxinfo (), and other cwnd member functions. You can use these functions as needed. As a special window, in addition to specifying the common window style, you can also select different control styles to obtain the corresponding functional effects. The optional editing styles are listed as follows:

Edit style description
Es_autohscroll automatically rolls 10 characters to the right after adding a character at the end of the row.
Es_autovscroll automatically rolls up a row when you press Enter.
Es_center is centered.
The es_left character is left aligned.
Es_lowercase is converted to lowercase letters.
Es_multiline allows multiple rows to be displayed.
Es_nohidesel hides the selection of characters when the editing loses focus. After the focus is obtained again, the selected content is displayed in Reversed colors.
Es_oemconvert converts ANSI characters to OEM characters.
Es_password is used to display characters with a star number. It is mostly used to display the password.
Es_right right
Es_uppercase is converted to uppercase letters.
Es_readonly is set to read-only.
Es_wantreturn accepts the Enter key.

The following is a sample code for creating and editing controls. This example first creates a control object using the CREATE () function and sets the characters for it using the setwindowtext () function. By adding a response code for the control notification message, you can check whether the text has changed. The main program implementation list is as follows:

// Create a widget:
// Create an editing Control
M_ctredit.create (ws_visible | ws_child | ws_border | es_wantreturn | es_multiline | es_autohscroll | es_autovscroll, crect (10, 50,250,150), this, id_edit1 );
// Set characters for the control
M_ctredit.setwindowtext ("Hello world! ");
......
// Add a response to the control notification message
// {Afx_msg (csample02view)
Afx_msg void onenchange ();
//} Afx_msg
Declare_message_map ()
......
Begin_message_map (csample02view, cview)
// {Afx_msg_map (csample02view)
On_en_change (id_edit1, onenchange)
//} Afx_msg_map
End_message_map ()
......
Void csample02view: onenchange ()
{
// Notification message response code
Afxmessagebox ("the content has changed! ");
}

List box and combo box

The list box is a pre-defined subwindow named "ListBox" in the window class. The box contains a list of bar strings that can be displayed in scrolling mode. Only one entry can be selected in the standard list box. The selected entries are highlighted in the system color color_highlight. Windows also provides a variety of standard list boxes of different styles, including the multiple choice list box, multi-Column Display list box, and the owner-draw list box that can display images. Another control related to the list box is a combo box. The predefined class of this control is "ComboBox". It is actually a combination of an editing control and a list box control related to each other. You can directly enter and edit text in the edit box or select from the options displayed in the drop-down list.

The clistbox class of MFC encapsulates the list box control. The member function create () is used to create the list box, and the control window style is specified at the same time. When the entries in the list box are selected or double-clicked, The wm_command message is sent to the parent window. If the list box adopts the lbs_notify style, the parent window can use the on_lbn message ing macro to directly map the notification message to the message processing function. The list box provides more than 10 window styles. The style and description of the list box are as follows:

Style of the list box
Lbs_standard creates a standard list box with border and vertical scroll bars. When the selection changes or the entry is double-clicked, it notifies the parent window of the standard list box. All entries are sorted alphabetically.
Lbs_sort is sorted alphabetically.
Lbs_nosel entries are visible but not optional.
Lbs_notify sends a message to the parent window when you select or double-click a string.
Lbs_disablenoscroll displays a scroll bar that does not work when there are not many entries.
Lbs_multiplesel allows multiple entries.
Lbs_extendedsel multiple entries can be selected by means of shift and mouse or specified key combination.
Lbs_multicolumn allows multiple columns to be displayed.
Lbs_ownerdrawvariable creates an owner drawing list box with different entry heights.
Lbs_ownerdrawfixed creates an owner painting list box with the same entry height.
Lbs_usetabstops allows the use of tab.
Lbs_noredraw: the list is not automatically updated after an entry is added or deleted.
Lbs_hasstrings remembers the strings added to the List.
Lbs_wantkeyboardinput sends the wm_vkeytoitem or wm_chartoitem message to the parent window when a key is pressed.
Lbs_nointegralheight: Create a list box based on the size set by the program.

By default, the list box is automatically repainted after each entry is added or deleted. If there are hundreds or even thousands of entries in the list box, this will cause serious flashes due to re-painting. You can use lbs_noredraw to disable automatic repainting of a list box. Force redraw the list box window when you need to update the display. If the lbs_noredraw style is not used during creation, you can add or delete a message to send the wm_setredraw message to the list box, and specify that the message is not repainted. After adding the message, send the wm_setredraw message again to enable the automatic re-painting style. The sample process is as follows:

Clistbox m_ctrlistbox;
// Disable automatic re-painting
M_ctrlistbox.sendmessage (wm_setredraw, false, 0 );
// Add or delete entries
......
// Allow automatic re-painting
M_ctrlistbox.sendmessage (wm_setredraw, true, 0 );

When a list box is created, it does not contain any items. You can use the clistbox member functions addstring () and insertstring () to add or Insert entries to the list box. If the list box has the lbs_sort style, the position of the newly added string is not fixed and should be sorted according to the letters of the string. If it does not have the style, the new string will be added to the end of the list box.

If necessary, you can use setitemdataptr () or setitemdata () to associate a 32-bit pointer (or a DWORD Value) with an entry in the list box, you can call getitemdataptr () or getitemdata () to obtain this information. In this way, the entries in the list box can be connected with external data. For example, you can use this method to easily associate a Data Structure Containing addresses, phone numbers, and e-mail addresses with the holders listed in the list box. When you select a person from the list box, you can get the communication information about the person at the same time.

When the list box is operated, a notification is sent to the parent window through the wm_command message. The high byte of the Message Parameter lparam contains the notification code identifier. In the MFC application, the notification messages in the list box are mapped to the class member functions through the on_lbn message ing macro. The following table lists several notification messages in the list box and the corresponding on_lbn macro. The lbn_dblclk, lbn_selchange, and lbn_selcancel notifications are sent only when the lbs_notify or lbs_standard style is used in the list box. Other notification messages do not have this restriction.

Notification code identifier on_lbn macro value meaning
Lbn_setfocus on_lbn_setfocus 4 the list box receives the input focus.
Lbn_killfocus on_lbn_killfocus 5 drop box loses the input focus
Lbn_errspace on_lbn_errspace-2 list box storage Overflow
Lbn_dblclk on_lbn_dblclk 2 double-click the entry
Lbn_selchange on_lbn_selchange 1 change the selection
Lbn_selcancel on_lbn_selcancel 3 cancel Selection

The two most frequently used notification messages are lbn_dblclk and lbn_selchange. You can use getcursel () to obtain the index value of a list box that is double-clicked. For a list box that allows multiple selections, you must use getcaretindex () instead of getcursel (). The following example shows how to use the list control:

// Create and initialize the list box
// Create a list box
M_ctrlistbox.create (ws_visible | ws_child | ws_border | lbs_standard, crect (270, 50,370,150), this, idc_list1 );
// Add an entry
Cstring item [9] = {"Item1", "item2", "item3", "item4", "item5", "item6", "item7", "item8 ", "item9 "};
For (INT I = 0; I <9; I ++)
M_ctrlistbox.addstring (item [I]);
// Select 4th entries
M_ctrlistbox.setcursel (3 );
......
// Statement of the processing function of the notification message in the parent window (in the header file)
// {Afx_msg (csample02view)
Afx_msg void onlbndblclk ();
//} Afx_msg
Declare_message_map ()
......
// The ing entry of the parent window to the notification message (in the implementation file)
Begin_message_map (csample02view, cview)
// {Afx_msg_map (csample02view)
On_lbn_dblclk (idc_list1, onlbndblclk)
//} Afx_msg_map
// Standard printing commands
End_message_map ()
......
// Processing of lbn_dblclk notification messages in the parent window
Void csample02view: onlbndblclk ()
{
// Obtain the index of the currently selected entry
Int Index = m_ctrlistbox.getcursel ();
// Obtain the content of this entry
Char text [20];
M_ctrlistbox.gettext (index, text );
// Report the result in an information box
Afxmessagebox (cstring (text ));
}

Although the combo box is actually a combination of the list box and the edit box, it is used as an independent control like other controls. The ccombobox class of MFC provides support for the function of the combo box. You can specify the style of the combo box when using the CREATE () function (see the following table ).

Window style description
Cbs_autohscroll automatically rolls the text in the edit box to the right when a character is entered at the end of the row.
Cbs_dropdown is similar to cbs_simple, but the drop-down list is displayed only when you click the drop-down icon.
Cbs_dropdownlist is similar to cbs_dropdown, but the editing box for displaying the current option is replaced by a static box.
Cbs_hasstrings: Creates a canvas containing the owner of a project consisting of strings.
Cbs_oemconvert converts the ANSI string in the combo box to an OEM character.
Cbs_ownerdrawfixed: the owner of the drop-down list box is responsible for drawing the content. The height of each item in the list box is the same.
Cbs_ownerdrawvariable: the owner of the drop-down list box is responsible for drawing the content. The height of each item in the list box can be different.
The cbs_simple drop-down list is always displayed.
Cbs_sort Automatically sorts the items in the drop-down list.
Cbs_disablenoscroll: the vertical scroll bar is displayed when the drop-down list shows less content.
When creating a control, cbs_nointegralheight precisely sets the size of the combo box with the specified size.

Operations on the combo box will also send notification messages to the parent window. The processing process is similar to that of the previous controls. The on_cbn message ing macro is used to map notification messages. The following describes the on_cbn Macros in detail:

On_cbn macro event
On_cbn_closeup close the drop-down list.
On_cbn_dblclk double-click the project in the drop-down list.
The on_cbn_dropdown drop-down list box is displayed.
The text in the on_cbn_editchange edit box is changed.
The content of the on_cbn_editupdate edit box is updated and displayed.
The on_cbn_errspace combox cannot allocate enough memory for a special request.
The on_cbn_selendcancel user's selection is canceled.
On_cbn_selendok selects a project and hides the drop-down list of the combo box by pressing the Enter key or mouse.
The on_cbn_killfocus combo box loses focus.
The on_cbn_selchange option is changed.
The on_cbn_setfocus combo box obtains the input focus.

Finally, a sample code about the combo box is provided. From the code implementation, it is not difficult to see that the combo control is similar to the list control given above in programming implementation.

// Create a combo box
// Create a list control
M_ctrcombox.create (ws_visible | ws_child | ws_border | cbs_dropdown, crect (400, 50,470,150), this, idc_combox1 );
// Add an entry
Cstring item [9] = {"Item1", "item2", "item3", "item4", "item5", "item6", "item7", "item8 ", "item9 "};
For (INT I = 0; I <9; I ++)
M_ctrcombobox.addstring (item [I]);
// Select 4th entries
M_ctrcombobox.setcursel (3 );
......
// Notification message response function declaration (in the header file)
// {Afx_msg (csample02view)
Afx_msg void oncbnselchange ();
//} Afx_msg
Declare_message_map ()
......
// Handle the response to the notification message (in the implementation file)
Begin_message_map (csample02view, cview)
// {Afx_msg_map (csample02view)
On_cbn_selchange (idc_combox1, oncbnselchange)
//} Afx_msg_map
End_message_map ()
......
Void csample02view: oncbnselchange ()
{
// Obtain the index of the currently selected entry
Int Index = m_ctrcombobox.getcursel ();
// Obtain the content of this entry
Char text [20];
M_ctrcombobox.getlbtext (index, text );
// Report the result in an information box
Afxmessagebox (cstring (text ));
}

Tree Control

A tree control is a window that displays the project list hierarchically. Its items are displayed in the control in an association mode, click a project node at a certain level to expand all projects belonging to the node in the next level. Tree control is very suitable for managing project elements with many layers and clear relations between them. In MFC, The ctreectrl class provides support for tree controls.

After creating a tree control with create (), you can use the setimagelist () function to set an image list for it. In this way, you can set icons for projects at various levels in the tree control. You can use the insertitem () function to add data items for it. The returned htreeitem type handle uniquely identifies the added project. The handle should be properly kept. Only the handle can be used to add sub-projects for this project. If the parent window handle is specified as null when a child window is created, the project is directly created in the root directory. The following code creates a tree control using the above function and adds two levels of projects to it:

// Create a tree control
M_ctrtreectrl.create (ws_visible | ws_child | ws_border | tvs_linesatroot | tvs_haslines | tvs_hasbuttons | tvs_editlabels, crect (500, 50,670,200), this, idc_tree1 );
......
TV _item tvitem;
TV _insertstruct tvinsert;
Tvitem. Mask = tvif_text; // specify that psztext members are available.
Tvitem. psztext = "item0"; // The characters displayed for the root Project
Tvinsert. hparent = tvi_root; // specifies the parent handle.
Tvinsert. Item = tvitem; // specifies the TV _item structure object.
Tvinsert. hinsertafter = tvi_last; // project Insertion Method
// Create a root project. The current project handle is saved in hitem0.
Htreeitem hitem0 = m_ctrtreectrl.insertitem (& tvinsert );
......
// Create a second-level project under the root Project
Tvitem. Mask = tvif_text;
Tvitem. psztext = "subitem0 ";
Tvinsert. hparent = hitem0;
Tvinsert. Item = tvitem;
Tvinsert. hinsertafter = tvi_last;
Htreeitem hitem3 = m_ctrtreectrl.insertitem (& tvinsert );

As an interface of the same user, the tree control will send various notification messages under different actions, you can add the on_policy_reflect macro to the Message ing of the control window or add the on_policy macro to the Message ing of the control's parent window to specify the processing function for each notification message.

Any project in the tree control can have a subproject list, which can be expanded or shrunk at any time. When it is in the expanded state, the corresponding sub-project will be displayed under the Parent Project in indent mode; when it is in the contract state, the sub-project will not be displayed. When you double-click a parent project, the corresponding sub-project list is automatically switched between expand and shrink. When the sub-project list status changes and the status changes, the tree control sends the tvn_itemexpanding and tvn_itemexpanded notifications respectively. For other notification messages and their meanings, see the following table:

Notification Message description
Tvn_begindrag
Tvn_beginlabeledit
Tvn_beginrdrag right-click and drag
Tvn_deleteitem: deletes a specified project.
Tvn_endlabeledit end Edit Tag
Tvn_getdispinfo: obtains the display information of a project.
The tvn_itemexpanded sub-project list is expanded or collapsed.
The tvn_itemexpanding sub-project list is being expanded or collapsed.
Tvn_keydown keyboard event
The selection of the tvn_selchanged project has changed.
The selection of the tvn_selchanging project will change
Tvn_setdispinfo: notifies you to update the information of a project.

Summary

This article describes how to dynamically create, set the style, and notify messages of predefined windows standard controls, such as button controls, edit controls, tree controls, list controls, and combo controls that are frequently used in VC ++ programming. specifically, through the above content, you can master the general usage of these commonly used controls. For other Windows standard controls not mentioned in this article, you can also implement them in a similar way.

Related Article

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.