Click here to Skip to main content
15,896,453 members
Articles / Desktop Programming / MFC
Article

Hide scrollbars from a CListCtrl

Rate me:
Please Sign up or sign in to vote.
3.36/5 (21 votes)
1 Jul 2003CPOL1 min read 199.4K   2.7K   36   38
Removes the scrollbars from a CListCtrl without loosing the ability to scroll

Sample Image - ListCtrlSBHide.jpg

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!

Background

I used this on 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 that will be hidden, here we use the default: SB_BOTH, SB_HORZ and SB_VERT.

Clientdatatype of hiding scrollbars must be called BEFORE any data is entered, because this will change the clientrect of the ctrl 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 listctrl's 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 an listctrl that will just get a horiz-scrollbar, and you choose to hide both of the scrollbars. The function will also remove the vert-scrollbar, but since 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 of the scrollbars that will be shown.

Personally I prefer using the NcCalcSize. It removes the scrollbars pretty smooth. It's not dependent on whether or not the listctrl shows all scrollbars or not.

History

First release - Version 1.0. It will be updated when more solutions are found...

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer A/S Norske Shell (Dutch Shell)
Norway Norway
----------------------------------
Visit http://lars.werner.no/ for my blog!
----------------------------------
Retired programmer, Norway never had the jobs I wanted Smile | :)

Comments and Discussions

 
GeneralRe: another way to hide scroll bars Pin
glip1-Jul-03 23:03
glip1-Jul-03 23:03 
GeneralRe: another way to hide scroll bars Pin
Lars [Large] Werner1-Jul-03 23:11
professionalLars [Large] Werner1-Jul-03 23:11 
GeneralRe: another way to hide scroll bars Pin
glip2-Jul-03 1:26
glip2-Jul-03 1:26 
GeneralRe: another way to hide scroll bars Pin
Lars [Large] Werner2-Jul-03 1:28
professionalLars [Large] Werner2-Jul-03 1:28 
GeneralRe: another way to hide scroll bars Pin
Magnus Houska21-Sep-03 5:54
sussMagnus Houska21-Sep-03 5:54 
GeneralRe: another way to hide scroll bars Pin
Lars [Large] Werner21-Sep-03 6:12
professionalLars [Large] Werner21-Sep-03 6:12 
GeneralRe: another way to hide scroll bars Pin
sudhakar_Chavali8-Jan-04 1:38
susssudhakar_Chavali8-Jan-04 1:38 
GeneralRe: another way to hide scroll bars Pin
sudhakar_Chavali8-Jan-04 1:39
susssudhakar_Chavali8-Jan-04 1:39 
GeneralRe: another way to hide scroll bars Pin
Lars [Large] Werner8-Jan-04 1:46
professionalLars [Large] Werner8-Jan-04 1:46 
GeneralRe: another way to hide scroll bars Pin
JinHyuck Jung.28-May-04 1:57
JinHyuck Jung.28-May-04 1:57 
GeneralRe: another way to hide scroll bars Pin
Ted Pederson4-Apr-05 9:07
Ted Pederson4-Apr-05 9:07 
Generalnice Pin
Matthias Hänel29-Jun-03 12:59
Matthias Hänel29-Jun-03 12:59 
GeneralRe: nice Pin
Lars [Large] Werner30-Jun-03 11:45
professionalLars [Large] Werner30-Jun-03 11:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.