PRB: ListView with LVS_NOSCROLL Won't Display Header

Last reviewed: September 30, 1995
Article ID: Q137520
The information in this article applies to:
  • Microsoft Win32 Software Development Kit (SDK) versions 3.51, 4.0
  • Microsoft Win32s version 1.3

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 reference words: 4.00 1.30
KBCategory: kbprg kbui kbprb kbcode
KBSubcategory: UsrCtl



THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 30, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.