1

这是我的回调函数:

int CALLBACK SortMeAscending(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
    {
        int nRetVal = 0;

        PITEMDATA pData1 = (PITEMDATA)lParam1;
        PITEMDATA pData2 = (PITEMDATA)lParam2;

        switch(lParamSort)
        {
        case 0:
            if(pData1->iSeq < pData2->iSeq)
                nRetVal = -1;
            else if(pData1->iSeq > pData2->iSeq)
                nRetVal = 1;
            else
                nRetVal = 0;
            break;

        case 1:
            nRetVal = lstrcmp(pData2->pszCueName, pData1->pszCueName);
            break;
        case 2: 
            nRetVal = lstrcmp(pData2->pszTags, pData1->pszTags);
            break;

        default:
            break;
        }

        return nRetVal;
    }

    int CALLBACK SortMeDescending(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
    {
        int nRetVal = 0;

        PITEMDATA pData1 = (PITEMDATA)lParam1;
        PITEMDATA pData2 = (PITEMDATA)lParam2;

        switch(lParamSort)
        {
        case 0:
            if(pData1->iSeq > pData2->iSeq)
                nRetVal = -1;
            else if(pData1->iSeq < pData2->iSeq)
                nRetVal = 1;
            else
                nRetVal = 0;
            break;

        case 1:
            nRetVal = lstrcmp(pData1->pszCueName, pData2->pszCueName);
            break;
        case 2: 
            nRetVal = lstrcmp(pData1->pszTags, pData2->pszTags);
            break;

        default:
            break;
        }

        return nRetVal;
    }

那是我的 OnCompareItems:

int CTest::OnCompareItems(LPARAM lParam1, LPARAM lParam2, int iColumn)
{  
    CMFCHeaderCtrl &headerCntrl = GetHeaderCtrl();

    BOOL b = 0;
    if(headerCntrl.IsAscending())
    {
        return SortItems(SortMeDescending, iColumn);
    }
    else
    {
        return SortItems(SortMeAscending, iColumn);
    }
}

如果我关注这篇文章http://support.microsoft.com/kb/250614 我没有在标题中排序的箭头。如果我使用您在上面看到的方法,我会在排序方面遇到一些差距。我点击它排序,下次它不会排序,一次又一次,请帮助我

4

1 回答 1

1

我认为您需要使用CMFCHeaderCtrl::SetSortColumn来设置列的排序顺序。每次你排序。除此之外,应该从 HDN_ITEMCLICK 处理程序调用实际的排序函数。

所以,假设你已经指定了这个:

ON_NOTIFY(HDN_ITEMCLICK, 0, OnItemclickList1)

在 OnItemclickList1 方法中编写以下代码:

void CTest::OnItemclickList1(NMHDR* pNMHDR, LRESULT* pResult) 
{  
    NMLISTVIEW *pLV = (NMLISTVIEW *) pNMHDR;
    CMFCHeaderCtrl &headerCntrl = GetHeaderCtrl();

    BOOL b = 0;
    if(headerCntrl.IsAscending())
    {
        headerCntrl.SetSortColumn(pLV->iItem, FALSE);
        SortItems(SortMeDescending, pLV->iItem);
    }
    else
    {
        headerCntrl.SetSortColumn(pLV->iItem, TRUE);
        SortItems(SortMeAscending, iColumn);
    }
    *pResult = 0;
}

抛弃你的OnCompareItems

于 2013-10-07T21:37:59.467 回答