CMFCListCtrl自动排序的实现

   由于工作中要用到CMFCListCtrl控件中的排序功能,但是MFC下的类CMFCListCtrl中只提供了虚函数CompareItems,因此这个函数在我们自己的代码中是要自己实现的。我想从CMFCListCtrl继承下来实现自己的CXSLMFCListCtrl。她的功能就有自动排序的,根据其ListCtrl控件上的内容来判断数据类型,然后再作排序的实现。优先排序顺序为整形、时间、IP、字符串。

 

  以下是实现的类:

// XSLMFCListCtrl.cpp : implementation file // #include "stdafx.h" #include "XSLMFCListCtrl.h" // CXSLMFCListCtrl IMPLEMENT_DYNAMIC(CXSLMFCListCtrl, CMFCListCtrl) CXSLMFCListCtrl::CXSLMFCListCtrl() { m_bJugedType = FALSE; } CXSLMFCListCtrl::~CXSLMFCListCtrl() { } BOOL CXSLMFCListCtrl::CanStrToInt(CString strParam) { try { CString szItemData = strParam; ENSURE(szItemData != ""); for (int i = 0; i < strParam.GetLength(); i++) { if (szItemData[i] < '0' || szItemData[i] > '9') { return FALSE; } } return TRUE; } catch (CException*) { return FALSE; } } BOOL CXSLMFCListCtrl::CanStrToDateTime(CString strParam) { try { COleVariant vtime(strParam); vtime.ChangeType(VT_DATE); return TRUE; } catch (CException*) { return FALSE; } } BOOL CXSLMFCListCtrl::CanStrToIP(CString strIP) { int nPos, nCount = 0; CString strRemain, strNum; while (strIP != "" && nCount < 4) { strIP.Trim(); if (nCount < 3) { nPos = strIP.Find(_T('.')); if (-1 == nPos || 0 == nPos) { return FALSE; } strNum = strIP.Mid(0, nPos); } if ((nCount < 3) && (atoi(strNum) < 0 || atoi(strNum) > 255)) { return FALSE; } else if (atoi(strIP) < 0 || atoi(strIP) > 255) { return FALSE; } strRemain = strIP.Mid(nPos+1, strIP.GetLength()-nPos-1); strIP = strRemain; nCount++; } return TRUE; } int CXSLMFCListCtrl::OnCompareItems(LPARAM lParam1, LPARAM lParam2, int iColumn) { ENSURE(lParam1 != NULL); ENSURE(lParam2 != NULL); WsfItemData *pData1; pData1 = (WsfItemData*)lParam1; UINT nLineIndex1 = pData1->m_nLineIndex; WsfItemData *pData2; pData2 = (WsfItemData*)lParam2; UINT nLineIndex2 = pData2->m_nLineIndex; CString ItemData1 = GetItemText(nLineIndex1 < nLineIndex2 ? nLineIndex1 : nLineIndex2, iColumn); CString ItemData2 = GetItemText(nLineIndex1 < nLineIndex2 ? nLineIndex2 : nLineIndex1, iColumn); int nItem1, nItem2; COleVariant OleTime1(ItemData1); COleVariant OleTime2(ItemData2); if (!m_bJugedType) { if (CanStrToInt(ItemData1) && CanStrToInt(ItemData2)) { m_nColumnType = XSL_LIST_INT; } else if (CanStrToDateTime(ItemData1) && CanStrToDateTime(ItemData2)) { m_nColumnType = XSL_LIST_DATETIME; } else if (CanStrToIP(ItemData1) && CanStrToIP(ItemData2)) { m_nColumnType = XSL_LIST_IP; } else { m_nColumnType = XSL_LIST_STRING; } m_bJugedType = TRUE; } switch (m_nColumnType) { case XSL_LIST_INT: nItem1 = _ttoi(ItemData1); nItem2 = _ttoi(ItemData2); if (nItem1 == nItem2) { return 0; } else { return nItem1 < nItem2 ? -1 : 1; } break; case XSL_LIST_DATETIME: OleTime1.ChangeType(VT_DATE); OleTime2.ChangeType(VT_DATE); if (OleTime1 == OleTime2) { return 0; } else { return OleTime1 < OleTime2 ? -1 :1; } break; case XSL_LIST_IP: if (inet_addr(ItemData1) == inet_addr(ItemData2)) { return 0; } else { return inet_addr(ItemData1) > inet_addr(ItemData2) ? -1:1; } return 0; break; case XSL_LIST_STRING: return ItemData1.Compare(ItemData2); break; default: return 0; break; } } BEGIN_MESSAGE_MAP(CXSLMFCListCtrl, CMFCListCtrl) END_MESSAGE_MAP() // CXSLMFCListCtrl message handlers

你可能感兴趣的:(CMFCListCtrl自动排序的实现)