CListBox用法总结

来源:互联网 发布:阿里云搭建app服务器 编辑:程序博客网 时间:2024/05/16 15:29

基本用法

属性StyleSelection  Single   --- 单选  Multiple --- 多选(LBS_MULTIPLESEL)  None     --- 不可选(LBS_NOSEL)Sort  对应Style: LBS_SORT
Insert Itemint AddString(LPCTSTR lpszItem);int InsertString(int nIndex,                 LPCTSTR lpszItem);
Delete Itemint DeleteString(UINT nIndex);//清空void ResetContent();  
Selectionint GetCurSel( ) const;int SetCurSel(int nSelect);int GetSelCount( ) const;int GetSelItems(int nMaxItems,                 LPINT rgIndex) const;代码示例:获取选中项并输出假设CListBox控件变量名为m_lbTest
// 1.Selection = Single-----------------------------------int nSelIndex = m_lbTest.GetCurSel();if (nSelIndex == LB_ERR)//no item is currently selected{AfxMessageBox(TEXT("no item is currently selected"));}else{CString cstr;m_lbTest.GetText(nSelIndex, cstr);AfxMessageBox(cstr);}// 2.Selection = Multiple----------------------------------int nSelCnt = m_lbTest.GetSelCount();if (nSelCnt == LB_ERR)    //the list box is a single-selection list box{AfxMessageBox(TEXT("the list box is a single-selection list box"));return;}if (nSelCnt == 0)//no item is currently selected{AfxMessageBox(TEXT("no item is currently selected"));return;}int* pnSelIndex = new int[nSelCnt];m_lbTest.GetSelItems(nSelCnt, pnSelIndex);for (int i=0; i<nSelCnt; ++i){CString cstr;m_lbTest.GetText(pnSelIndex[i], cstr);AfxMessageBox(cstr);}delete[] pnSelIndex;
Other// 获取Textvoid GetText(int nIndex,             CString& rString) const;// Get/Set item associated dataDWORD_PTR GetItemData(int nIndex) const;int SetItemData(int nIndex,        DWORD_PTR dwItemData);注意:1.GetItemData在没有通过SetItemData设置每一项的关联数据时返回NULL.2.对应的GetItemDataPtr,SetItemDataPtr其实和GetItemData,SetItemData本质上是一模一样的  我们可以看下源码int CListBox::SetItemDataPtr(int nIndex, void* pData){ return SetItemData(nIndex, (DWORD_PTR)(LPVOID)pData); } 看来增加这两个函数只是使意义更明确些,有点不懂微软了。

动态创建CListBox控件

黑色非标准3D边框:
CListBox *pMyListBox = new CListBox();pMyListBox->Create(WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER | LBS_NOTIFY | LBS_MULTIPLESEL,CRect(10, 10, 100, 100),this,1234);pMyListBox->SetFont(this->GetFont());pMyListBox->AddString(TEXT("123"));pMyListBox->AddString(TEXT("456"));pMyListBox->AddString(TEXT("789"));
标准3D边框:
CListBox *pMyListBox = new CListBox();pMyListBox->CreateEx(  WS_EX_CLIENTEDGE,  TEXT("LISTBOX"),  TEXT(""),WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER | LBS_NOTIFY | LBS_MULTIPLESEL,10, 10, 100, 100,this->GetSafeHwnd(),(HMENU)1234);pMyListBox->SetFont(this->GetFont());pMyListBox->AddString(TEXT("123"));pMyListBox->AddString(TEXT("456"));pMyListBox->AddString(TEXT("789"));
原创粉丝点击