Solved

CListCtrl.SortItems question...

Posted on 1998-10-20
1
340 Views
Last Modified: 2013-11-20
CListCtrl.SortItems(CompareFunc, dwData)
int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lpara);

I use debugger to step inside the 'CompareFunc' and find out
lParam1 and lParam2 are both 0!!!!! However, lpara has the correct value. WHY????
0
Comment
Question by:stephenkong
1 Comment
 
LVL 8

Accepted Solution

by:
Answers2000 earned 0 total points
Comment Utility
lParam1 and lParam2 are the item data associated with the two items being compared.

If you don't set item data then it's zero

To set item data with an item - use SetItemData member of CListCtrl.

The normal way way to do this is
1. In dwData pass a "pseudo" this ptr to the class
SortItems( CompareFunc, (LPARAM)this ) ;

2. In CompareFunc convert this back to a CListCtrl *, or a ptr to your derived class
int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lpara)
{
 CListCtrl * pList = (CListCtrl *)lpara ;
}

3. To get non-zero item data
EITHER
(a) Set the row-id as item data for each item in the list before doing SortItems - then in CompareFunc you the item data as a param to GetItemText
OR
(b) Set the item data to point to some structure (you allocate) containing comparison data which the sort function uses

4. BTW if you want CompareFuncs to be a class member, make it a static member


Here's an example - that uses method 3(b).  Previous to this I have set item data with a record id for an AAInfo record (which is my data structure).  GetAA is a function I wrote to get an AAInfo record.  m_nSortColumn and m_bSortReverse are an int and BOOL class member

void CTComply::SortList()
{
      CWaitCursor oWait ;
      m_list.SortItems( CTComply::CompareFunc, (DWORD)this ) ;
}

nt CALLBACK CTComply::CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
      CTComply * pComply = (CTComply *)lParamSort ;
      ASSERT_VALID( pComply ) ;

      AAInfo oInfo1 ;
      AAInfo oInfo2 ;

      // get 2 items
      VERIFY( GetAA( lParam1, oInfo1 ) ) ;
      VERIFY( GetAA( lParam2, oInfo2 ) ) ;


      // now get 2 strings
      CString str1 ;
      CString str2 ;

      switch ( pComply->m_nSortColumn )
      {
      default :
      case 0 :
            str1 = oInfo1.m_strManufacturer ;
            str2 = oInfo2.m_strManufacturer ;
            break ;

      case 1 :
            str1 = oInfo1.m_strProduct ;
            str2 = oInfo2.m_strProduct ;
            break ;

      case 2 :
            str1 = oInfo1.m_strVersion ;
            str2 = oInfo2.m_strVersion ;
            break ;

      case 3 :
            str1 = oInfo1.m_strProductType ;
            str2 = oInfo2.m_strProductType ;
            break ;

      case 4 :
            str1 = oInfo1.GetStatus() ;
            str2 = oInfo2.GetStatus() ;
            break ;
      }


      // do comparison
      int nn = 0 ;

      if ( str1 < str2 ) nn = -1 ;
      if ( str1 > str2 ) nn = 1 ;

      if ( pComply->m_bSortReverse )
      {
            nn = 0 - nn ;
      }

      return nn ;
}

void CTComply::OnColumnclickList1(NMHDR* pNMHDR, LRESULT* pResult)
{
      NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;

      int nNewHeader = pNMListView->iSubItem ;

      if ( nNewHeader == m_nSortColumn )
      {
            m_bSortReverse = !m_bSortReverse ;
      }
      m_nSortColumn = nNewHeader ;
      SortList() ;

      *pResult = 0;
}





0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now