PRB: ListView with LVS_NOSCROLL Won't Display Header

ID: Q137520


The information in this article applies to:
  • Microsoft Win32 Software Development Kit (SDK)
  • Microsoft Win32s version 1.3
  • Microsoft Windows 2000


SYMPTOMS

In report view, the header control is not displayed for a ListView control created with the LVS_NOSCROLL style.


CAUSE

The ListView control positions the header control when the scrolling is updated. When the LVS_NOSCROLL style is specified, the control is never scrolled, so the header control is not positioned.


RESOLUTION

Call following function at the appropriate time to position the header control properly. To use the function, create the ListView without the LVS_NOSCROLL style, and then call this function whenever the ListView is created, resized, the view is changed, or the parent window receives the WM_SYSPARAMETERCHANGE message. Creating the control without the LVS_NOSCROLL style will ensure that the first item in the list won't be obscured by the header control. The function will automatically detect which view is currently set and act appropriately.


   /********************************************************************

      PositionHeader

      Call this function when the ListView is created, resized, the
      view is changed, or a WM_SYSPARAMETERCHANGE message is recieved

   ********************************************************************/ 

   void PositionHeader(HWND hwndListView)
   {
   HWND  hwndHeader = GetWindow(hwndListView, GW_CHILD);
   DWORD dwStyle = GetWindowLong(hwndListView, GWL_STYLE);

   /*
      To ensure that the first item will be visible, create the control
      without the LVS_NOSCROLL style and then add it here.
   */ 
   dwStyle |= LVS_NOSCROLL;
   SetWindowLong(hwndListView, GWL_STYLE, dwStyle);

   /*
      Only do this if the ListView is in report view and you were able to
      get the header hWnd.
   */ 
   if(((dwStyle & LVS_TYPEMASK) == LVS_REPORT) && hwndHeader)
      {
      RECT        rc;
      HD_LAYOUT   hdLayout;
      WINDOWPOS   wpos;

      GetClientRect(hwndListView, &rc);
      hdLayout.prc = &rc;
      hdLayout.pwpos = &wpos;

      Header_Layout(hwndHeader, &hdLayout);

      SetWindowPos(  hwndHeader,
                     wpos.hwndInsertAfter,
                     wpos.x,
                     wpos.y,
                     wpos.cx,
                     wpos.cy,
                     wpos.flags | SWP_SHOWWINDOW);

      ListView_EnsureVisible(hwndListView, 0, FALSE);
      }
   } 


STATUS

This behavior is by design.

Additional query words:

Keywords : kbCtrl kbListView kbNTOS400 kbWinOS2000 kbSDKWin32 kbGrpUser kbWinOS95 kbWinOS98
Version : WINDOWS:1.3
Platform : WINDOWS
Issue type : kbprb


Last Reviewed: February 1, 2000
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.