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:
List box style |
Description |
Lbs_standard |
Create a standard list box with border and vertical scroll bars. When the selection changes or the entry is double-clicked, a standard list box with the parent window will be notified. All entries are sorted alphabetically. |
Lbs_sort |
Sort by letter. |
Lbs_nosel |
The entry is visible but not optional. |
Lbs_policy |
When you select or double-click a string, the parent window of the notification will be sent. |
Lbs_disablenoscroll |
Scroll bars that do not work are still displayed when there are not many entries. |
Lbs_multiplesel |
You can select multiple entries. |
Lbs_extendedsel |
You can select multiple entries by combining shift and mouse or specified key. |
Lbs_multicolumn |
Multiple columns are allowed to be displayed. |
Lbs_ownerdrawvariable |
Create an owner drawing list box with different entry heights. |
Lbs_ownerdrawfixed |
Create an owner drawing list box with the same entry height. |
Lbs_usetabstops |
Tab labels are allowed. |
Lbs_noredraw |
When an entry is added or deleted, the list is not automatically updated. |
Lbs_hasstrings |
The strings added to the list are memorized. |
Lbs_wantkeyboardinput |
When a key is pressed, the message wm_vkeytoitem or wm_chartoitem is sent to the parent window. |
Lbs_nointegralheight |
Create a list box by 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 |
Description |
Lbn_setfocus |
On_lbn_setfocus |
4 |
The list box receives the input focus. |
Lbn_killfocus |
On_lbn_killfocus |
5 |
The drop box loses the input focus. |
Lbn_errspace |
On_lbn_errspace |
-2 |
List box storage Overflow |
Lbn_dblclk |
On_lbn_dblclk |
2 |
Double-click an entry |
Lbn_selchange |
On_lbn_selchange |
1 |
Change Selection |
Lbn_selcancel |
On_lbn_selcancel |
3 |
Deselect |
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 |
When a character is entered at the end of a row, the text in the edit box is automatically rolled to the right. |
Cbs_dropdown |
Similar to the cbs_simple style, the drop-down list is displayed only when you click the drop-down icon. |
Cbs_dropdownlist |
Similar to cbs_dropdown, the editing box for displaying the current option is replaced by a static box. |
Cbs_hasstrings |
Create an owner canvas containing a string. |
Cbs_oemconvert |
Convert 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. |
Cbs_simple |
The drop-down list is always displayed. |
Cbs_sort |
Projects in the drop-down list are automatically sorted. |
Cbs_disablenoscroll |
When the drop-down list shows less content, the vertical scroll bar is displayed. |
Cbs_nointegralheight |
When creating a control, you can precisely set 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 |
Corresponding event |
On_cbn_closeup |
Close the drop-down list. |
On_cbn_dblclk |
Double-click the project in the drop-down list. |
On_cbn_dropdown |
The drop-down list box is displayed. |
On_cbn_editchange |
The text in the edit box is changed. |
On_cbn_editupdate |
The content in the edit box is updated and displayed. |
On_cbn_errspace |
The combo box cannot allocate enough memory for a special request. |
On_cbn_selendcancel |
The user selection is canceled. |
On_cbn_selendok |
Select a project and press the Enter key or the mouse to hide the drop-down list of the combo box. |
On_cbn_killfocus |
The combo box loses focus. |
On_cbn_selchange |
Select change. |
On_cbn_setfocus |
The 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 )); } |