Hide scrollbars from a CListCtrl – Lars Werner

Hide scrollbars from a CListCtrl

 

This is an article about how you can remove the scrollbars from a CListCtrl and do not loose the ability to scroll. EnsureVisible and other functions works with this derived class.

Check it out on:
Codeproject.com
Codeguru.com
Planetsourcecode.com

Hide scrollbars from a CListCtrl

ListCtrlSBHideProject.zip (35.75 kB)
ListCtrlSBHideSource.zip (2.2 kB)

CListCtrl - Scrollbars are hidden, but they still work :)

Environment: VC++ 6.0, WinXP
Short description: Removes the scrollbars from a CListCtrl without
loosing 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 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:

 c++ | 
 
 copy code |
?

01
//(...)
02
class CCListCtrlExampleDlg : public CDialog
03
04
05
    {
06
    // Construction
07
    public:
08
    	CCListCtrlExampleDlg(CWnd* pParent = NULL);	// standard constructor
09
    	//These are the defs for each type of how we hide the scrollbars
10
    // Dialog Data
11
    	//{{AFX_DATA(CCListCtrlExampleDlg)
12
    	enum { IDD = IDD_CLISTCTRLEXAMPLE_DIALOG };
13
    	CListCtrlHiddenSB	m_list4;
14
    	CListCtrlHiddenSB	m_list3;
15
    	CListCtrlHiddenSB	m_list2;
16
    	CListCtrlHiddenSB	m_list1;
17
    	//}}AFX_DATA
18
    	// ClassWizard generated virtual function overrides
19
    	//{{AFX_VIRTUAL(CCListCtrlExampleDlg)
20
    	protected:
21
    	virtual void DoDataExchange(CDataExchange* pDX);	// DDX/DDV support
22
    	//}}AFX_VIRTUAL
23
    (...)
24
    

Just before the listctrl's are filled with data we hide the scrollbars:

25
    	m_list1.HideScrollBars(LCSB_CLIENTDATA, SB_VERT);
26
    	m_list2.HideScrollBars(LCSB_CLIENTDATA);
27
    	m_list3.HideScrollBars(LCSB_NCOVERRIDE);
28
    	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 if the listctrl shows all scrollbars or not.

 Posted by at 11:51:33

  One Response to “Hide scrollbars from a CListCtrl”

  1. […] set up Because I need to HIDE scrollbar. Here is the explanation of what I’m talking about. http://lars.werner.no/?page_id=24 But this solution does not working well for our project on Windows CE 5. I was reading an exemple […]

Leave a Reply

%d bloggers like this: