Hide Scrollbars from a CListCtrl

Environment: VC++ 6.0, WinXP
Short description: Removes the scrollbars from a CListCtrl without losing the ability to scroll.

Introduction

When you create a CListCtrl with the "NO SCROLL" option, it completely stop scrolling. If the data is out of the screen, you do not see anything. So, you always had to use the scrollbars, until now!

I've used this for many kiosk software solutions, and it gives you complete control of the CListCtrl.

Using the Code

The CListCtrlHiddenSB is derived from CListCtrl. It has one function to hide the scrollbars: HideScrollBars(int Type, int Which).

  • The Type variable has two const: LCSB_CLIENTDATA and LCSB_NCOVERRIDE.
  • The Which variable tells which scrollbar will be hidden; here we use the default: SB_BOTH, SB_HORZ, and SB_VERT.

The Clientdata type of hiding scrollbars must be called BEFORE any data is entered because this will change the clientrect of the control to be smaller.

In the demo project, I override the normal constructor, like this:

(...)
class CCListCtrlExampleDlg : public CDialog
{
// Construction
public:
  CCListCtrlExampleDlg(CWnd* pParent = NULL);    // standard
                                                 // constructor

  //These are the defs for each type of how we hide the scrollbars
// Dialog Data
  //{{AFX_DATA(CCListCtrlExampleDlg)
  enum { IDD = IDD_CLISTCTRLEXAMPLE_DIALOG };
  CListCtrlHiddenSB m_list4;
  CListCtrlHiddenSB m_list3;
  CListCtrlHiddenSB m_list2;
  CListCtrlHiddenSB m_list1;
  //}}AFX_DATA

  // ClassWizard generated virtual function overrides
  //{{AFX_VIRTUAL(CCListCtrlExampleDlg)
  protected:
  virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV
                                                      // support
  //}}AFX_VIRTUAL
(...)

Just before the listctrls are filled with data, we hide the scrollbars:

  m_list1.HideScrollBars(LCSB_CLIENTDATA, SB_VERT);

  m_list2.HideScrollBars(LCSB_CLIENTDATA);

  m_list3.HideScrollBars(LCSB_NCOVERRIDE);

  m_list4.HideScrollBars(LCSB_NCOVERRIDE);

Known Issues

If you have a listctrl that will just get a horizontal scrollbar and you choose to hide both of the scrollbars, the function will also remove the vertical scrollbar. But, because it is not there, some of the gfx will be cut off.

In other words, the clientarena should only be used when you KNOW how many scrollbars will be shown.

Personally, I prefer using the NcCalcSize. It removes the scrollbars pretty smoothly. It doesn't care whether the listctrl shows all scrollbars or not.

Downloads

Download demo project - 36 Kb
Download source - 3 Kb


About the Author

Lars Werner

Thnx for viewing my article...

Check out some of my projects:

http://lars.werner.no/sizeme/ - Ultimate tool for maximize your output on CD/DVDs!

http://lars.werner.no/unpacker/ - Auto extract of multiple archives (RAR/ZIP) with queue support and cleanup options!

http://lars.werner.no/vnc/ - Fullscreen captionbar modified VNC clones (RealVNC, UltraVNC and TightVNC)

Visit http://lars.werner.no/ for more info!

Comments

  • Applying "NcCalcSize" trick under CE

    Posted by Codeplug on 05/12/2004 01:24pm

    I'm running CE 2.12, which doesn't support the WM_NCCALCSIZE.
    Through trial and error, I discovered that the same trick works 
    when intercepting WM_SIZE instead of WM_NCCALCSIZE. 
    
    The only caveat is that you must call the following for the 
    style removal to take effect:
    ::SetWindowPos(hwnd, NULL, 0, 0, 0, 0,
                   SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | 
                   SWP_NOACTIVATE | SWP_FRAMECHANGED);
    
    Or in terms of MFC, you would call:
    ModifyStyle(WS_HSCROLL | WS_VSCROLL, 0, SWP_FRAMECHANGED);
    
    gg

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: May 6, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT While you likely have very good reasons for remaining on WinXP after end of support -- an estimated 20-30% of worldwide devices still are -- the bottom line is your security risk is now significant. In the absence of security patches, attackers will certainly turn their attention to this new opportunity. Join Lumension Vice President Paul Zimski in this one-hour webcast to discuss risk and, more importantly, 5 pragmatic risk mitigation techniques …

  • Download the Information Governance Survey Benchmark Report to gain insights that can help you further establish business value in your Records and Information Management (RIM) program and across your entire organization. Discover how your peers in the industry are dealing with this evolving information lifecycle management environment and uncover key insights such as: 87% of organizations surveyed have a RIM program in place 8% measure compliance 64% cannot get employees to "let go" of information for …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds