• Win32 ListCtrl控件点击列标题排序


    1.在WM_NOTIFY消息中捕获鼠标点点击列的索引

    int MyMainDialog::OnNotify(NMHDR* pNMhdr)
    {
        if (pNMhdr->idFrom == IDC_LIST1)
        {
            QListCtrl listCtrl(pNMhdr->idFrom, this);
            NMLISTVIEW* pNmListView = (NMLISTVIEW*)pNMhdr;
    
            switch (pNMhdr->code)
            {
            case LVN_COLUMNCLICK:
            {
                //获得点击的列的索引     
                int nCol = pNmListView->iSubItem;
    //获得列标题中图标的索引 int nColImage = listCtrl.GetColumnImageIndex(nCol);
    //SortItemFunc自己定义的排序回调函数,第二个参数传二个值(列索引,升序=0/降序=1)
            //这个函数就是向ListCtrl控件发送的LVM_SORTITEMS消息
    listCtrl.SortItem(SortItemFunc, MAKELPARAM(nCol, nColImage));
    //排序后把图标索引改变(就在0和1之间) listCtrl.SetColumnImageIndex(nCol, (nColImage == 0 ? 1 : 0)); }break; case LVN_DELETEITEM: //关闭窗口后删除行中保存New出来的内存地址 { if (pNmListView->lParam != 0) { delete (StudentStruct*)pNmListView->lParam; pNmListView->lParam = 0; } }break; } } return 0; }

    2.自定义回调函数SortItemFunc

    int CALLBACK SortItemFunc(LPARAM lParam1, LPARAM lParam2, LPARAM sortID)
    {
        //要比较的两行数据
      //这两行的数据是保存在行的lParam数据中
      //用SetItemData赋值的
    StudentStruct* pStu1 = (StudentStruct*)lParam1; StudentStruct* pStu2 = (StudentStruct*)lParam2; int nCol = LOWORD(sortID); //要比较的列索引 int nAcs = HIWORD(sortID); //按升序或是降序排序 int nRet = 0; switch (nCol) { case 0: //姓名 nRet = _tcscmp(pStu1->name, pStu2->name); break; case 1: //性别 nRet = _tcscmp(pStu1->sex, pStu2->sex); break; case 2: //年龄,这是字符比较,如果是数字比较就要自己处理<,=,>的结果 //nRet = _tcscmp(pStu1->age, pStu2->age); if (pStu1->age < pStu2->age) nRet = -1; if (pStu1->age == pStu2->age) nRet = 0; if (pStu1->age > pStu2->age) nRet = 1; break; case 3: //电话 nRet = _tcscmp(pStu1->tel, pStu2->tel); break; } return (nAcs == 0 ? nRet : -nRet); }

    点击性别示意图

    点击年龄示意图

    点击姓名示意图

    感觉林这个性没排对,不只为啥?

  • 相关阅读:
    POJ 3422 Kaka's Matrix Travels(最小费用最大流)
    POJ 2195 Going Home(最小费用最大流)
    POJ 3694 Network(双连通分量)
    POJ 2942 Knights of the Round Table(双连通分量)
    POJ 1275 Cashier Employment(差分约束)
    Codeforces Round #224 (Div. 2)
    POJ 2983 Is the Information Reliable?(差分约束系统)
    POJ 3159 Candies(差分约束)
    学习笔记之设计模式 | 菜鸟教程
    学习笔记之编译器的工作过程 | 菜鸟教程
  • 原文地址:https://www.cnblogs.com/greenleaf1976/p/16323145.html
Copyright © 2020-2023  润新知