Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. CListViewCtrl creation does not produce column header arrow indicating sort order

CListViewCtrl creation does not produce column header arrow indicating sort order

Scheduled Pinned Locked Moved ATL / WTL / STL
graphicsquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jonathan Davies
    wrote on last edited by
    #1

    I have a Dialog, inheriting from CDialogImpl, which contains a CListViewCtrl with three columns. In order to get the list items ordered correctly a custom sort function is used. It is in my handler for LVN_COLUMNCLICK< (Clicking a column header) that that the column header has the triangular bitmap indicating sort direction set. On creation of the dialog, without the workaround shown below, no sort direction arrow appears on the header. I then have to call the LVN_COLUMNCLICK handler (see code) from OnInitDialog to force a sort of the first column in the order that my LVN_COLUMNCLICK handler sets the sort order triangular bitmap in the column header.

    LRESULT CPortsDlg::OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
    {
    ...

    // Work-around to force list control to sort column 0 ascending on creation
    int idCtrl = 0;
    NMLISTVIEW nmlv;
    nmlv.iSubItem = 0;
    BOOL bHandled = FALSE;
    LPNMHDR lpNMHDR = reinterpret\_cast<LPNMHDR>(&nmlv);
    OnLvnColumnclick(idCtrl, lpNMHDR, bHandled); 
    
        return 0;
    

    }

    This doesn't seem right; am I missing some feature that will ensure that upon creation a particular list column is sorted (using my custom sort) in, say, ascending order with the appropriate column header bitmap displayed to indicate this?

    LRESULT CPortsDlg::OnLvnColumnclick(int /*idCtrl*/, LPNMHDR pNMHDR, BOOL& /*bHandled*/)
    {
    // Get some calls where m_hwnd is 0
    if(!::IsWindow(m_hWnd))
    {
    return 0;
    }

    // ListView  Notification Message structure
    LPNMLISTVIEW pNMLV = reinterpret\_cast<LPNMLISTVIEW>(pNMHDR); 
    
    // Want the format of the list column header
    HDITEM HeaderItem; 
    HeaderItem.mask = HDI\_FORMAT; 
    
    // Attach a CHeaderCtrl to this CListViewCtrl's header
    CHeaderCtrl HeaderCtrl = m\_ListViewCtrl.GetHeader(); 
    
    if(!::IsWindow(HeaderCtrl.m\_hWnd))
    {
    	return 0;
    }
    
    // For each list column...
    int nColumnCount = HeaderCtrl.GetItemCount();
    for(int nSelectedColumn = 0; nSelectedColumn < nColumnCount; nSelectedColumn++)
    {
    	// Get the format for the sub-item (column)
    	HeaderCtrl.GetItem(nSelectedColumn, &HeaderItem); 
    
    	// Remove any up/down arrow from this column
    	HeaderItem.fmt &= ~(HDF\_SORTDOWN | HDF\_SORTUP);
    
    	if(nSelectedColumn == pNMLV->iSubItem)
    	{
    		// If this is a different column from any selected before use an up-arrow otherwise
    		// base it on changing the direction from m\_S
    
    S 1 Reply Last reply
    0
    • J Jonathan Davies

      I have a Dialog, inheriting from CDialogImpl, which contains a CListViewCtrl with three columns. In order to get the list items ordered correctly a custom sort function is used. It is in my handler for LVN_COLUMNCLICK< (Clicking a column header) that that the column header has the triangular bitmap indicating sort direction set. On creation of the dialog, without the workaround shown below, no sort direction arrow appears on the header. I then have to call the LVN_COLUMNCLICK handler (see code) from OnInitDialog to force a sort of the first column in the order that my LVN_COLUMNCLICK handler sets the sort order triangular bitmap in the column header.

      LRESULT CPortsDlg::OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
      {
      ...

      // Work-around to force list control to sort column 0 ascending on creation
      int idCtrl = 0;
      NMLISTVIEW nmlv;
      nmlv.iSubItem = 0;
      BOOL bHandled = FALSE;
      LPNMHDR lpNMHDR = reinterpret\_cast<LPNMHDR>(&nmlv);
      OnLvnColumnclick(idCtrl, lpNMHDR, bHandled); 
      
          return 0;
      

      }

      This doesn't seem right; am I missing some feature that will ensure that upon creation a particular list column is sorted (using my custom sort) in, say, ascending order with the appropriate column header bitmap displayed to indicate this?

      LRESULT CPortsDlg::OnLvnColumnclick(int /*idCtrl*/, LPNMHDR pNMHDR, BOOL& /*bHandled*/)
      {
      // Get some calls where m_hwnd is 0
      if(!::IsWindow(m_hWnd))
      {
      return 0;
      }

      // ListView  Notification Message structure
      LPNMLISTVIEW pNMLV = reinterpret\_cast<LPNMLISTVIEW>(pNMHDR); 
      
      // Want the format of the list column header
      HDITEM HeaderItem; 
      HeaderItem.mask = HDI\_FORMAT; 
      
      // Attach a CHeaderCtrl to this CListViewCtrl's header
      CHeaderCtrl HeaderCtrl = m\_ListViewCtrl.GetHeader(); 
      
      if(!::IsWindow(HeaderCtrl.m\_hWnd))
      {
      	return 0;
      }
      
      // For each list column...
      int nColumnCount = HeaderCtrl.GetItemCount();
      for(int nSelectedColumn = 0; nSelectedColumn < nColumnCount; nSelectedColumn++)
      {
      	// Get the format for the sub-item (column)
      	HeaderCtrl.GetItem(nSelectedColumn, &HeaderItem); 
      
      	// Remove any up/down arrow from this column
      	HeaderItem.fmt &= ~(HDF\_SORTDOWN | HDF\_SORTUP);
      
      	if(nSelectedColumn == pNMLV->iSubItem)
      	{
      		// If this is a different column from any selected before use an up-arrow otherwise
      		// base it on changing the direction from m\_S
      
      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2

      It doesn't seem unreasonable to me that you have to tell the control how it's sorted - I suspect that setting HDF_SORTDOWN or HDF_SORTUP on the relevant column and calling m_ListViewCtrl.SortItems would be sufficient - if that's the case, maybe you should refactor the header flag setting and control sorting into a separate function, so it could be called from OnInitDialog rather than calling the OnClick handler? [edit]Maybe CSortListViewCtrl will do some of the sorting tasks for you, simplifying the overall job?[/edit]

      J 1 Reply Last reply
      0
      • S Stuart Dootson

        It doesn't seem unreasonable to me that you have to tell the control how it's sorted - I suspect that setting HDF_SORTDOWN or HDF_SORTUP on the relevant column and calling m_ListViewCtrl.SortItems would be sufficient - if that's the case, maybe you should refactor the header flag setting and control sorting into a separate function, so it could be called from OnInitDialog rather than calling the OnClick handler? [edit]Maybe CSortListViewCtrl will do some of the sorting tasks for you, simplifying the overall job?[/edit]

        J Offline
        J Offline
        Jonathan Davies
        wrote on last edited by
        #3

        Stuart, I've done a test using CSortListViewCtrl (which I didn't know about) instead of CListViewCtrl. It seems that simply calling SetSortColumn(0) both makes my handler for LVN_COLUMNCLICK redundant and removes the need to call it from InitDialog. I haven't tried this with a custom sort yet but can see I will need to call SetColumnSortType(0, LVCOLSORT_CUSTOM). Much neater. Thanks.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups