1、ListControl初始化
ListControl初始化代码添加在虚函数CDlgxxx::OnInitDialog()中:
m_MyList.ModifyStyle(0L,LVS_SHOWSELALWAYS);
DWORD dwStyle = m_MyList.GetExtendedStyle();
dwStyle |= LVS_EX_FULLROWSELECT;//选中某行使整行高亮(只适用与report风格的listctrl)
dwStyle |= LVS_EX_GRIDLINES;//网格线(只适用与report风格的listctrl)
// dwStyle |= LVS_EX_CHECKBOXES;//item前生成checkbox控件
m_MyList.SetExtendedStyle(dwStyle);
//设置列标题
m_MyList.InsertColumn(0,_T(""));
m_MyList.InsertColumn(1,_T("编号"),LVCFMT_CENCTER,80);
m_MyList.InsertColumn(2,_T("名称"),LVCFMT_LEFT,100);
m_MyList.InsertColumn(3,_T("单价"),LVCFMT_RIGHT,80);
......
//InsertColumn第三个参数可设置为LVCFMT_LEFT, LVCFMT_RIGHT, or LVCFMT_CENTER
//解决第一列始终靠左的问题
m_MyList.DeleteColumn(0);
2、在ListControl中插入数据
int nItem = m_datalist.GetItemCount();
//获得列表行数
m_Mylist.InsertItem(nItem, _T("data"));
m_Mylist.SetItemText(nItem, 1, CString);
m_Mylist.SetItemText(nItem, 2, CString);
......
3、删除选中项(包括多选)
void Dialogxxx::OnBnClickedDelete()
{
// TODO: Add your control notification handler code here
POSITION pos = m_Mylist.GetFirstSelectedItemPosition();
if (pos == NULL) {
AfxMessageBox(_T("No items were selected!\n"));
}
else {
while(pos) {
int nItem = m_Mylist.GetNextSelectedItem(pos);
m_Mylist.DeleteItem(nItem);
// you could do your own processing on nItem here
}
}
}
4、左键单击消息响应函数
给ListControl控件添加名为NM_CLICK的消息响应函数
BEGIN_MESSAGE_MAP(CDlgxxx, CDialog)
ON_NOTIFY(NM_CLICK, IDC_LIST, &CDlgxxx::OnNMClickList)
END_MESSAGE_MAP()
若编译出错在NMITEMACTIVATE后添加*
void CDlgxxx::OnNMClickList(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<NMITEMACTIVATE*>(pNMHDR);
// TODO: Add your control notification handler code here
*pResult = 0;
}
5、单击列表头实现按相应列排序
从CListCtrl继承一个新类CSortList
class CSortList : public CListCtrl
{
public:
CSortList();
~CSortList();
bool m_fAsc;
int m_nSortedCol;
};
CSortList::CSortList()
: CListCtrl()
{
m_fAsc = FALSE;
m_nSortedCol = 0;
}
CSortList::~CSortList()
{}
在对话框类中为CListCtrl控件添加成员变量
CSortList m_datalist;
添加消息相应函数
afx_msg void OnLvnColumnclickDatalist(NMHDR *pNMHDR, LRESULT *pResult);
BEGIN_MESSAGE_MAP(Dialogxxx, CDialog)
ON_NOTIFY(LVN_COLUMNCLICK, IDC_DATALIST, &DialogC10::OnLvnColumnclickDatalist)
END_MESSAGE_MAP()
void Dialogxxx::OnLvnColumnclickDatalist(NMHDR *pNMHDR, LRESULT *pResult)
{
// LPNMLISTVIEW pNMLV = reinterpret_cast(pNMHDR);
// TODO: Add your control notification handler code here
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
//为排序设置唯一标示符??
for(int i = 0; i < m_datalist.GetItemCount(); ++i)
m_datalist.SetItemData(i,i);
if(pNMListView->iSubItem == m_datalist.m_nSortedCol) {
m_datalist.m_fAsc = !m_datalist.m_fAsc;
}
else {
m_datalist.m_nSortedCol = pNMListView->iSubItem;
m_datalist.m_fAsc = FALSE;
}
m_datalist.SortItems(MyCompareProc, (DWORD)&m_datalist);//调用排序处理函数MyCompareProc
*pResult = 0;
}
添加相应处理函数
static int CALLBACK MyCompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
// lParamSort contains a pointer to the list view control.
// The lParam of an item is just its index.
CSortList* pListCtrl = (CSortList*)lParamSort;
CString strItem1 = pListCtrl->GetItemText(lParam1, pListCtrl->m_nSortedCol);
CString strItem2 = pListCtrl->GetItemText(lParam2, pListCtrl->m_nSortedCol);
//比较两个数
// LPCTSTR s1=(LPCTSTR)strItem1;
// LPCTSTR s2=(LPCTSTR)strItem2;
int n1 = _ttoi(strItem1);
int n2 = _ttoi(strItem2);
if(pListCtrl->m_fAsc)
return n1 > n2 ? 1 : -1;
else
return n1 < n2 ? 1 : -1;
}