• CListCtrl,按照某列进行排序(本地数据倒序、正序)SetItemData和GetItemData


    1、h文件中

    afx_msg void OnColumnClick(NMHDR *pNMHDR, LRESULT *pResult);
    
    int static CALLBACK CompareByTime(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort);
    
    CListCtrl m_list_;
    

      

    记得是CListCtrl类。如果用子类,记得看一下是否有OnColumnClick

    2、cpp文件中:

    BEGIN_MESSAGE_MAP(CDlgOssJson, CBCGPDialog)
    	ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST_ITEMS, &CDlgOssJson::OnLvnItemchangedList)
    	ON_NOTIFY(LVN_COLUMNCLICK, IDC_LIST_ITEMS, &CDlgOssJson::OnColumnClick)
    END_MESSAGE_MAP()
    

      

    BOOL CDlgOssJson::OnInitDialog()
    {
    	CBCGPDialog::OnInitDialog();
    
    	DWORD dwStyle = m_list_.GetExtendedStyle();
    	dwStyle |= LVS_EX_FULLROWSELECT;
    	dwStyle |= LVS_EX_GRIDLINES;
    	dwStyle |= LVS_EX_CHECKBOXES;
    	m_list_.SetExtendedStyle(dwStyle);	
    	int nCol = 0;    //列标题
    	m_list_.InsertColumn(nCol++, _T("名称"), LVCFMT_CENTER, 300);
    	m_list_.InsertColumn(nCol++, _T("上传时间"), LVCFMT_CENTER, 200);
    
    	GetItemsData(1);
    	return TRUE;
    }
    
    void CDlgOssJson::OnLvnItemchangedList(NMHDR *pNMHDR, LRESULT *pResult)
    {
    	LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
    	if (pNMLV->iSubItem == 0)
    	{
    		BOOL bChk = TRUE;
    		int nCount = m_list_.GetItemCount();
    		for(int i = 0; i < nCount; ++i)
    		{
    			bChk = m_list_.GetCheck(i);
    			if (!bChk)
    			{
    				break;
    			}
    		}
    		m_chk_all_.SetCheck(bChk);
    	}
    	*pResult = 0;
    }
    
    int CALLBACK CDlgOssJson::CompareByTime(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
    {
    	oss_json_data *cd1 = (oss_json_data*)(LPARAM)lParam1;    //SetItemData时的值
    	oss_json_data *cd2 = (oss_json_data*)(LPARAM)lParam2;
    	if (cd1 != NULL && cd2 != NULL)
    	{
    		return st_bMethod ? cd1->strTime.CompareNoCase(cd2->strTime) : cd2->strTime.CompareNoCase(cd1->strTime);
    	}
    	else
    	{
    		return int(st_bMethod);
    	}
    }
    
    void CDlgOssJson::OnColumnClick(NMHDR *pNMHDR, LRESULT *pResult)
    {
    	LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
    	*pResult = 0;
    
    	if (st_nSortColumn == pNMLV->iSubItem)
    	{
    		st_bMethod = !st_bMethod;
    		m_list_.SortItems(CompareByTime, (DWORD_PTR)&m_list_);    //(DWORD_PTR)this也可以将this对话框传入CompareByTime
    	}
    }
    

      
    重点来了
    要记得

    	m_list_.DeleteAllItems();
    	int iSize = m_vec_items_.size();
    	for (int i = 0; i < iSize; ++i)
    	{
    		CString strItem = _T("");
    		strItem.Format(_T("%d"), i + 1);
    		m_list_.InsertItem(i, strItem);    //行标题
    		m_list_.SetItemData(i, (LPARAM)(oss_json_data*)&m_vec_items_[i]);    //列排序时用
    		m_list_.SetItemText(i, 0, m_vec_items_[i].strProjName);
    		m_list_.SetItemText(i, 1, m_vec_items_[i].strTime);
    	}
    

      

  • 相关阅读:
    asp中动态include的方法
    asp存储过程使用大全
    用vb6写asp组件的简单例子
    asp中遍历一些对象(request,session,Application)
    查看ASP Session 变量的小工具
    层不能跨框架(包括TEXTAREA)显示的解决办法
    保存远程图片到本地 同时取得第一张图片并创建缩略图
    使用.Net开发asp组件
    使用ASP在IIS创建WEB站点
    解析notes自带的rtf javaapplet编辑器
  • 原文地址:https://www.cnblogs.com/XiHua/p/16158416.html
Copyright © 2020-2023  润新知