The ListBox window is used to list a series of texts, each of which occupies one line. To create a list window, you can use the member function:
BOOL CListBox: Create (LPCTSTR lpszText, DWORD dwStyle, const RECT & rect, CWnd * pParentWnd, UINT nID = 0 xffff );
DwStyle indicates the style of the window. In addition to the commonly used style WS_CHILD and WS_VISIBLE of the sub-window, you can specify a special style for the list control.
- LBS_MULTIPLESEL indicates that multiple rows can be selected simultaneously in the list box.
- LBS_EXTENDEDSEL you can press Shift/Ctrl to select multiple rows
- All LBS_SORT rows are sorted alphabetically.
After the list box is generated, you must add or delete rows to or from the list box. You can use:
Int AddString (LPCTSTR lpszItem) to Add rows,
Int DeleteString (UINT nIndex) deletes a specified row,
Int InsertString (int nIndex, LPCTSTR lpszItem) inserts the row into the specified position.
Void ResetContent () can delete all rows in the list box.
Call int GetCount () to obtain the number of rows in the current list box.
If you want to obtain/Set the currently selected row. You can call int GetCurSel ()/int SetCurSel (int iIndex ). If you specify the multi-row style, you need to call int GetSelCount () to obtain the number of selected rows, and then int GetSelItems (int nMaxItems, LPINT rgIndex) obtain all the selected rows. The rgIndex parameter is an array that stores the selected rows. Call int GetLBText (int nIndex, LPTSTR lpszText) to obtain the string of the specified row in the list box.
In additionInt FindString (int nStartAfter, LPCTSTR lpszItem) can be used to locate the specified character transfer position in all current rows. nStartAfter indicates to start searching from that row.
Int SelectString (int nStartAfter, LPCTSTR lpszItem) can be used to select rows containing the specified string.
InThe CCheckListBox class is added to MFC 4.2, which is derived from CListBox and has all functions of CListBox. The difference is that a check box can be added before each row. Note that the LBS_OWNERDRAWFIXED or LBS_OWNERDRAWVARIABLE style must be specified during creation.
PassVoid SetCheckStyle (UINT nStyle)/UINT GetCheckStyle () can be used to set/obtain the style of the check box. For details about the style of the check box, see 4.1 Button. You can use void SetCheck (int nIndex, int nCheck)/int GetCheck (int nIndex) to set and obtain the check status of a row. For details about the check box status, see section 4.1 Button.
At last, we will introduce several common message ing Macros in the list box:
- ON_LBN_DBLCLK double-click
- ON_EN_ERRSPACE is generated when the input box cannot allocate memory
- ON_EN_KILLFOCUS/ON_EN_SETFOCUS is generated when the input box loses/gets the input focus
- The row selected by ON_LBN_SELCHANGE changes.
The preceding message ing methods are used to define the prototype, for example:Afx_msg void memberFxn (); function, and defines message ing in the form of ON_Notification (id, memberFxn. If the list box is used in the dialog box, Class Wizard automatically lists related messages and generates message ing code.