CListBoxST,一个派生自 CListBox 的控件






4.81/5 (23投票s)
2002年3月22日
2分钟阅读

246368

8656
处理图标和禁用项目的 CListBox 派生类
摘要
CListBoxST
是一个派生自 CListBox
的类,试图重现相同控件的外观和感觉
当一张没有 autorun.inf 的 CD 被插入到 CD-Rom 驱动器时,它会出现在 Windows XP 系统中。
每个列表框项目可以被启用或禁用,可以在左侧有一个图标,其文本可以是多行的。
即使使用标准的 CListCtrl
控件也可以获得几乎相同的视觉效果,但是存在
一些使 CListBoxST
更好的区别
- 易于使用
- 标准 CListBox 方法
- 已禁用的项目
- 多行文本
- 上下移动项目、置顶、置底的方法
- 不同的高亮显示样式
如何在您的应用程序中集成 CListBoxST
在你的项目中包含以下文件
- ListBoxST.h
- ListBoxST.cpp
使用对话框编辑器创建一个标准的列表框,例如,IDC_LBXITEMS
。 您需要设置以下属性

Notify
属性不是必需的,但需要捕获来自列表框的消息,例如,双击
事件。
然后为这个列表框创建一个成员变量
CListBoxST m_lbxItems;
// Create a image list to hold icons
CImageList m_ImageList;
现在将列表框附加到 CListBoxST
。 对于基于对话框的应用程序,在您的 OnInitDialog
中
// Call the base-class method CDialog::OnInitDialog(); // Create the IDC_LBXITEMS list box m_lbxItems.SubclassDlgItem(IDC_LBXITEMS, this);或者在您的
DoDataExchange
中// Call the base method CDialog::DoDataExchange(pDX); // Create the IDC_LBXITEMS list box DDX_Control(pDX, IDC_LBXITEMS, m_lbxItems);
要支持图标,必须将一个图像列表与控件关联起来
BOOL bRetValue = FALSE; HICON hIcon = NULL; // Create image list bRetValue = m_ImageList.Create(32, 32, ILC_COLOR32 | ILC_MASK, 5, 1); ASSERT(bRetValue == TRUE); // Add some icons hIcon = AfxGetApp()->LoadIcon(IDI_SHELL32_203); m_ImageList.Add(hIcon); hIcon = AfxGetApp()->LoadIcon(IDI_SHELL32_141); m_ImageList.Add(hIcon); // Associate image list to list box m_lbxItems.SetImageList(&m_ImageList); // At this point the list box is ready to be used. Add some items m_lbxItems.AddString(_T("First item"), 0); m_lbxItems.AddString(_T("Second item\nThis is multi-line"), 1);
类方法
AddString
向列表框添加一个字符串。
// Parameters: // [IN] lpszItem // Points to the null-terminated string that is to be added. // [IN] nImage // Image to be associated with the string. // Pass -1L to associate no image. // // Return value: // The zero-based index of the string in the list box. // The return value is LB_ERR if an error occurs; the return value // is LB_ERRSPACE if insufficient space is available to store the new string. // int AddString(LPCTSTR lpszItem, int nImage = -1L)
InsertString
在列表框中的特定位置插入一个字符串。
// Parameters: // [IN] nIndex // Specifies the zero-based index of the position to insert the string. // If this parameter is -1, the string is added to the end of the list. // [IN] lpszItem // Pointer to the null-terminated string that is to be inserted. // [IN] nImage // Image to be associated with the string. // Pass -1L to associate no image. // // Return value: // The zero-based index of the position at which the string was inserted. // The return value is LB_ERR if an error occurs; the return value // is LB_ERRSPACE if insufficient space is available to store the new string. // int InsertString(int nIndex, LPCTSTR lpszString, int nImage = -1L)
DeleteString
从列表框中删除一个字符串。
// Parameters: // [IN] nIndex // Specifies the zero-based index of the string to be deleted. // // Return value: // A count of the strings remaining in the list box. // The return value is LB_ERR if nIndex specifies an index greater than // the number of items in the list. // int DeleteString(int nIndex)
ReplaceString
替换列表框中特定位置的字符串。
// Parameters: // [IN] nIndex // Specifies the zero-based index of the position to replace the string. // [IN] lpszItem // Pointer to the null-terminated string that is to be replaced. // [IN] nImage // Image to be associated with the string. // Pass -1L to associate no image. // // Return value: // The zero-based index of the position at which the string was replaced. // The return value is LB_ERR if an error occurs; the return value // is LB_ERRSPACE if insufficient space is available to store the new string. // int ReplaceString(int nIndex, LPCTSTR lpszString, int nImage = -1L)
ResetContent
清除列表框中的所有条目。
void ResetContent()
SetImageList
设置要在列表框中使用的图像列表。
// Parameters: // [IN] pImageList // Pointer to an CImageList object containing the image list // to use in the list box. Pass NULL to remove any previous // associated image list. // void SetImageList(CImageList* pImageList)
SetImage
设置要在列表框项目中使用的图像。
// Parameters: // [IN] nIndex // Specifies the zero-based index of the string. // [IN] nImage // Specifies the zero-based index of the image // inside the imagelist to use. // [IN] bRepaint // If TRUE the control will be repainted. // void SetImage(int nIndex, int nImage, BOOL bRepaint = TRUE)
GetImage
返回与列表框项目关联的图像索引。
// Parameters: // [IN] nIndex // Specifies the zero-based index of the string. // [OUT] lpnImage // Pointer to a int variable that will receive the index // of the image inside the imagelist. // This variable will be set to -1L if no image is associated. // void GetImage(int nIndex, LPINT lpnImage)
SetItemData
设置与列表框项目关联的 32 位值。
// Parameters: // [IN] nIndex // Specifies the zero-based index of the item. // [IN] dwItemData // Specifies the value to be associated with the item. // // Return value: // LB_ERR if an error occurs. // int SetItemData(int nIndex, DWORD dwItemData)
GetItemData
返回与列表框项目关联的 32 位值。
// Parameters: // [IN] nIndex // Specifies the zero-based index of the item. // // Return value: // The 32-bit value associated with the item, or LB_ERR if an error occurs. // DWORD GetItemData(int nIndex)
SetItemDataPtr
设置指向列表框项目的指针。
// Parameters: // [IN] nIndex // Specifies the zero-based index of the item. // [IN] pData // Specifies the pointer to be associated with the item. // // Return value: // LB_ERR if an error occurs. // int SetItemDataPtr(int nIndex, void* pData)
GetItemDataPtr
返回列表框项目的指针。
// Parameters: // [IN] nIndex // Specifies the zero-based index of the item. // // Return value: // Pointer associated with the item, or -1 if an error occurs. // void* GetItemDataPtr(int nIndex)
MoveUp
将列表框项目向上移动一个位置。
// Parameters: // [IN] nIndex // Specifies the zero-based index of the item. // [IN] bSetCurSel // If TRUE the item will be highlighted // // Return value: // The zero-based index of the position at which the string was moved. // The return value is LB_ERR if an error occurs; the return value // is LB_ERRSPACE if insufficient space is available to store the string. // int MoveUp(int nIndex, BOOL bSetCurSel = TRUE)
MoveDown
将列表框项目向下移动一个位置。
// Parameters: // [IN] nIndex // Specifies the zero-based index of the item. // [IN] bSetCurSel // If TRUE the item will be highlighted // // Return value: // The zero-based index of the position at which the string was moved. // The return value is LB_ERR if an error occurs; the return value // is LB_ERRSPACE if insufficient space is available to store the string. // int MoveDown(int nIndex, BOOL bSetCurSel = TRUE)
MoveTop
将列表框项目移动到最顶部的位置。
// Parameters: // [IN] nIndex // Specifies the zero-based index of the item. // [IN] bSetCurSel // If TRUE the item will be highlighted // // Return value: // The zero-based index of the position at which the string was moved. // The return value is LB_ERR if an error occurs; the return value // is LB_ERRSPACE if insufficient space is available to store the string. // int MoveTop(int nIndex, BOOL bSetCurSel = TRUE)
MoveBottom
将列表框项目移动到最底部的位置。
// Parameters: // [IN] nIndex // Specifies the zero-based index of the item. // [IN] bSetCurSel // If TRUE the item will be highlighted // // Return value: // The zero-based index of the position at which the string was moved. // The return value is LB_ERR if an error occurs; the return value // is LB_ERRSPACE if insufficient space is available to store the string. // int MoveBottom(int nIndex, BOOL bSetCurSel = TRUE)
EnableItem
启用或禁用列表框项目。
// Parameters: // [IN] nIndex // Specifies the zero-based index of the item. // [IN] bEnable // Specifies whether the given item is to be enabled or disabled. // If this parameter is TRUE, the item will be enabled. // If this parameter is FALSE, the item will be disabled. // [IN] bRepaint // If TRUE the control will be repainted. // void EnableItem(int nIndex, BOOL bEnable = TRUE, BOOL bRepaint = TRUE)
IsItemEnabled
指定列表框项目是否已启用。
// Parameters: // [IN] nIndex // Specifies the zero-based index of the item. // // Return value: // TRUE if the item is enabled; otherwise FALSE. // BOOL IsItemEnabled(int nIndex)
SetRowSelect
设置如何高亮显示选定的列表框项目。
// Parameters: // [IN] byRowSelect // Selection type. Can be one of the following values: // ST_FULLROWSELECT Hilight full list box item (Default) // ST_FULLTEXTSELECT Hilight half list box item (Part containing text) // ST_TEXTSELECT Hilight only list box text // [IN] bRepaint // If TRUE the control will be repainted. // void SetRowSelect(BYTE byRowSelect = ST_FULLROWSELECT, BOOL bRepaint = TRUE)
GetVersionI
以短数值形式返回类版本。
// Return value: // Class version. Divide by 10 to get actual version. // static short GetVersionI()
GetVersionC
以字符串值的形式返回类版本。
// Return value: // Pointer to a null-terminated string containig the class version. // static LPCTSTR GetVersionC()
历史
- v1.0 (2002 年 3 月 13 日) 首次发布
免责声明
软件和随附文件按“原样”分发,没有任何明示或暗示的保证。 对于可能造成的损坏甚至功能问题,概不负责。 用户必须承担使用本软件的全部风险。