列表控件排序_上位机VC
列表控件排序_上位机VC
点击列表表头,可以让对应整列进行排序,当前例程实现这一功能
1.新建一对话框工程,添加列表控件,关联变量 CListCtrl m_ctrlList;
2.程序初始化的函数OnInitDialog()中初始化控件
- //设置列表控件的报表显示方式
- m_ctrlList.ModifyStyle(LVS_ICON | LVS_SMALLICON | LVS_LIST, LVS_REPORT);
- //在列表控件中插入列
- for (int n = 0; n < 4; n++)
- {
- CString strColumnHeading = _T("");
- strColumnHeading.Format(_T("Column %d"), n);
- m_ctrlList.InsertColumn(n, strColumnHeading, LVCFMT_LEFT, 100);
- }
- //在列表控件中插入行
- for (int m = 0; m < 10; m++)
- {
- CString strItem = _T("");
- strItem.Format("Item %d", m);
- m_ctrlList.InsertItem(m, strItem);
- for (int n = 1; n < 4; n++)
- {
- CString strText = _T("");
- strText.Format(_T("SubItem %d %d"), m, n);
- m_ctrlList.SetItemText(m, n, strText);
- m_ctrlList.SetItemData(m, m);
- }
- }
复制代码 3.添加列表控件的LVN_COLUMNCLICK消息处理函数
- void CGkbc8Dlg::OnColumnclickList1(NMHDR* pNMHDR, LRESULT* pResult)
- {
- NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
- // TODO: Add your control notification handler code here
- m_ctrlList.SortItems(CompareFunc, LPARAM(&m_ctrlList));
- *pResult = 0;
- }
复制代码 4.上面CompareFunc()为自定义回调函数
- int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
- {
- //获得列表控件
- CListCtrl* pListCtrl = (CListCtrl*)lParamSort;
- CString strItem1 = pListCtrl->GetItemText(lParam1, 0);
- CString strItem2 = pListCtrl->GetItemText(lParam2, 0);
- return strcmp(strItem2, strItem1);
- }
复制代码
如果您认可,可联系功能定制! 如果您着急,充值会员可直接联系发您资料!
|