Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Problems with setting sort icon in a listview columnheader


This question is not answered. Helpful answers available: 1. Correct answers available: 1.


Permlink Replies: 8 - Last Post: Mar 1, 2017 3:43 PM Last Post By: Remy Lebeau (Te...
Andy Stobirski

Posts: 18
Registered: 6/12/16
Problems with setting sort icon in a listview columnheader  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 25, 2017 10:26 AM
Hi Guys

I'm trying to set a arrow indicating sort direction on a column header in a listview when it is, and after some googling I have come across the following code:


void TForm1::setListViewSortIcon(HWND listView, int col, int sortOrder)
{
    HWND headerWnd;
    const int bufLen = 256;
    char headerText[bufLen];
    HD_ITEM item;
    int numColumns, curCol;
 
    UINT HDF_SORTUP   = 0x0400;
    UINT HDF_SORTDOWN = 0x0200;
 
    headerWnd = ListView_GetHeader(listView);
    numColumns = Header_GetItemCount(headerWnd);
 
    for (curCol=0; curCol<numColumns; curCol++)
    {
        item.mask = HDI_FORMAT | HDI_TEXT;
        item.pszText = headerText;
        item.cchTextMax = bufLen - 1;
        SendMessage(headerWnd, HDM_GETITEM, curCol, (LPARAM)&item);
 
        OutputDebugString(IntToStr(HDM_GETITEM).c_str());
 
        if ((sortOrder != 0) && (curCol==col))
        switch (sortOrder)
        {
            case 1:
                item.fmt &= !HDF_SORTUP;
                item.fmt |= HDF_SORTDOWN;
                break;
            case 2:
                item.fmt &= !HDF_SORTDOWN;
                item.fmt |= HDF_SORTUP;
                break;
        }
        else
        {
            item.fmt &= !HDF_SORTUP & !HDF_SORTDOWN;
        }
        item.fmt |= HDF_STRING;
        item.mask = HDI_FORMAT | HDI_TEXT;
 
        SendMessage(headerWnd, HDM_SETITEM, curCol, (LPARAM)&item);
    }
}
 


When I call it using

    setListViewSortIcon(ListView1->Handle
        , 0 // some column index
        , 1);


It's not adding an icon. However, if I add an icon to the column manually, running the above code causes the icon to dissapear.

Any suggestions?

Edited by: Andy Stobirski on Feb 25, 2017 12:25 PM
Valence Crearer

Posts: 64
Registered: 11/27/99
Re: Problems with setting sort icon in a listview columnheader
Helpful
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 28, 2017 2:48 PM   in response to: Andy Stobirski in response to: Andy Stobirski
switch (sortOrder)
{
case 1:
item.fmt &= !HDF_SORTUP;

Don't know... but use ~ to <not> bits.

item.fmt &= ~HDF_SORTUP; // there are a number of ! used... replace them all like this example

then try it again.
Asger Joergensen

Posts: 370
Registered: 11/18/08
Re: Problems with setting sort icon in a listview columnheader [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 28, 2017 11:56 PM   in response to: Andy Stobirski in response to: Andy Stobirski
Hi Andy

this works for me:

void TForm1::setColSortArrow( TListView* lv, int idx, TLvShowArrow showArrow)
{
   HWND hHeader = ListView_GetHeader( lv->Handle );
   if( ! hHeader )return;
 
   HDITEM hdrItem = {0};
   hdrItem.mask = HDI_FORMAT;
 
   if( Header_GetItem( hHeader, idx, &hdrItem ) )
   {
      if( showArrow == tlvShowArrowAscending )
      {
         hdrItem.fmt = (hdrItem.fmt & ~HDF_SORTDOWN) | HDF_SORTUP;
      }
      else if( showArrow == tlvShowArrowDescending )
      {
         hdrItem.fmt = (hdrItem.fmt & ~HDF_SORTUP) | HDF_SORTDOWN;
      }
      else
      {
         hdrItem.fmt = hdrItem.fmt & ~(HDF_SORTDOWN | HDF_SORTUP);
      }
 
      Header_SetItem( hHeader, idx, &hdrItem );
   }
}


Best regards
Asger
Asger Joergensen

Posts: 370
Registered: 11/18/08
Re: Problems with setting sort icon in a listview columnheader [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 1, 2017 12:55 AM   in response to: Asger Joergensen in response to: Asger Joergensen
Hi Andy

Forgot my own TLvShowArrow:
   enum TLvShowArrow { tlvShowNoArrow, tlvShowArrowAscending, tlvShowArrowDescending };


Best regards
Asger
Andy Stobirski

Posts: 18
Registered: 6/12/16
Re: Problems with setting sort icon in a listview columnheader [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 1, 2017 11:22 AM   in response to: Asger Joergensen in response to: Asger Joergensen
Thank you for taking the time to reply. Unfortunately, that code doesn't work for me.

I've been taking a look again my installation, and I see that the commctrl.h is dated 1/31/2000 and doesn't contain any definitions for HDF_SORTDOWN or HDF_SORTUP, so I've provided my own definitions.

I'm using an ancient version of Borland C++, v5, to be precise. Is what I'm trying to do possible for this particular version?
Asger Joergensen

Posts: 370
Registered: 11/18/08
Re: Problems with setting sort icon in a listview columnheader [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 1, 2017 12:31 PM   in response to: Andy Stobirski in response to: Andy Stobirski
Hi Andy

Andy Stobirski wrote:

Thank you for taking the time to reply. Unfortunately, that code doesn't work
for me.

I've been taking a look again my installation, and I see that the commctrl.h
is dated 1/31/2000 and doesn't contain any definitions for HDF_SORTDOWN or
HDF_SORTUP, so I've provided my own definitions.

I'm using an ancient version of Borland C++, v5, to be precise. Is what I'm
trying to do possible for this particular version?

Your right the I used the code in CB2009 and it seem that the sort arrows
wasn't available untill Vista, there is some information here.
But windows 2000 did have sort arrows.

You can do it your self if you use two small bitmaps or use an imagelist, then
just show the arrow to the right of the text.

Best regards
Asger
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Problems with setting sort icon in a listview columnheader [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 1, 2017 1:39 PM   in response to: Andy Stobirski in response to: Andy Stobirski
Andy wrote:

I'm using an ancient version of Borland C++, v5, to be precise.
Is what I'm trying to do possible for this particular version?

What you are attempting is a pure Win32 solution, and the VCL TListView component
is a wrapper for a standard Win32 LISTVIEW control, so this approach will
work just fine in any version of C++Builder as long as you have the relevant
Win32 declarations, whether you have to declare them manually or not.

--
Remy Lebeau (TeamB)
Asger Joergensen

Posts: 370
Registered: 11/18/08
Re: Problems with setting sort icon in a listview columnheader [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 1, 2017 3:29 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Hi Remy

Remy Lebeau (TeamB) wrote:

What you are attempting is a pure Win32 solution, and the VCL TListView component
is a wrapper for a standard Win32 LISTVIEW control, so this approach will
work just fine in any version of C++Builder as long as you have the relevant
Win32 declarations, whether you have to declare them manually or not.

That sounds right, I just tried in my BCB5 and all the functions succeed
also the last Header_SetItem( hHeader, idx, &hdrItem );
But the sort arrow isn't shown.

Do one need to add a manifest to the project, in order to get the right GUI ?

Best regards
Asger
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Problems with setting sort icon in a listview columnheader [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 1, 2017 3:43 PM   in response to: Asger Joergensen in response to: Asger Joergensen
Asger wrote:

Do one need to add a manifest to the project, in order to
get the right GUI ?

The HDF_SORTDOWN and HDF_SORTUP flags require ComCtrl32.dll v6. So, on Vista+
and WinServer 2003+, you need to use a theming manifest, yes. On earlier
Windows versions, you can subclass the ListView to handle NM_CUSTOMDRAW notifications
to custom-draw the header manually.

--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02