CListBox用法总结

CListBox用法总结
用法
属性Style
Selection
Single — 单选
Multiple — 多选(LBS_MULTIPLESEL)
None — 不可选(LBS_NOSEL)
Sort
对应Style: LBS_SORT

Insert Item
int AddString(LPCTSTR lpszItem);
int InsertString(int nIndex,
LPCTSTR lpszItem);

Delete Item
int DeleteString(UINT nIndex);
//清空
void ResetContent();

Selection
int 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
// 获取Text
void GetText(int nIndex,
CString& rString) const;
// Get/Set item associated data
DWORD_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”));

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值