Click here to Skip to main content
13,089,400 members (56,948 online)
Click here to Skip to main content
Add your own
alternative version


36 bookmarked
Posted 15 May 2002

Scrollbars, using the WS_HSCROLL and WS_VSCROLL styles

, 13 Jun 2002
Rate this:
Please Sign up or sign in to vote.
How to use a CWnd's default scrollbars and to setup the thumb size correctly


All CWnd derived classes have access to system supplied scrollbars by using the window styles WS_HSCROLL and WS_VSCROLL, but these are no ordinary scrollbars! For starters, you cannot get a CScrollBar* pointer to them, so they cannot be subclassed.

Included above is a project that makes use of these standard scrollBar objects got by using these window styles. Its a view which creates a window derived from CWnd which has the necessary functions to enable/disable the scrollbars as required. As you re-size the view, this re-sizes the window which decides whether the scrollbars should be shown.

Windows acts oddly when enabling/disabling the scrollbars. When you decide you need to show one or both of the scrollbars by using the CWnd::ShowScrollBar method, a WM_SIZE message is automatically generated and called immediately telling your window the new size of the client area (which does not include the area taken by the scrollbars if visible), so if you choose to show/hide the scrollbars in the OnSize handler, the function needs to avoid problems due to recursion.

User interface

Sample Image - Scroll.jpg

Scrollbars are a standard part of the windows user interface. I was trying to use one of these in a control and needed the scrollbar thumb to show how much of the total area was displayed. The standard MSDN documentation on how to do this was sadly lacking (or I was just dumb). I spent many hours in frustration which ended in a rant in the Soapbox. From a sympathetic reply there, I was given the clue on how to do what I had been attempting.

Scrollbars can be setup using a SCROLLINFO object to set the range and thumb size. Its just that the flags you can set and the variables are very badly documented. So here is an overview of what I have learnt. The SCROLLINFO structure looks like this:

typedef struct tagSCROLLINFO { 
    UINT cbSize; 
    UINT fMask; 
    int  nMin; 
    int  nMax; 
    UINT nPage; 
    int  nPos; 
    int  nTrackPos; 

A point by point description of these are:

  • cbSize - Needs to be set to the size of the structure si.cbSize = sizeof(SCROLLINFO)
  • fMask - A bit field variable which lets windows know which other parts of the structure contain valid numbers.
  • nMin - Used with the SIF_RANGE flag, it sets the scroll bars minimum value.
  • nMax - Used with the SIF_RANGE flag, it sets the scroll bars maximum value.
  • nPage - Used with the SIF_PAGE flag, it sets how much of the total scroll area is visible - this sets the size of the thumb in the scroll bar.
  • nPos - Used with the SIF_POS flag, it sets the current scroll position.
  • nTrackPos - Used with the SIF_TRACKPOS flag and the GetScrollPos call, to get the current thumb position while the user is dragging it.
  • My problem was I was trying to get the thumb to be proportional, which is good programming procedure for all new windows applications. This allows more information to be supplied to the user in how much data is not being shown. As another CP user replied to me: I hate programmers who don't do this - they should be taken out and shot!, or words to that effect.


  • 14-6-2002 Made the example flicker free
  • License

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


    About the Author

    Roger Allen
    Software Developer (Senior) Sirius Analytical Instruments
    United Kingdom United Kingdom
    A research and development programmer working for a pharmaceutical instrument company for the past 17 years.

    I am one of those lucky people who enjoys his work and spends more time than he should either doing work or reseaching new stuff. I can also be found on playing DDO on the Cannith server (Send a tell to "Maetrim" who is my current main)

    I am also a keep fit fanatic, doing cross country running and am seriously into [url][/url] Karate at this time of my life, training from 4-6 times a week and recently achieved my 1st Dan after 6 years.

    You may also be interested in...


    Comments and Discussions

    GeneralMy vote of 2 Pin
    jonclegg10-Jan-11 5:59
    memberjonclegg10-Jan-11 5:59 
    Generalplease help me [modified] Pin
    Mausumi10-Jan-07 0:55
    memberMausumi10-Jan-07 0:55 
    GeneralNot scrolling all the way Pin
    Budric B.6-Jul-05 8:18
    memberBudric B.6-Jul-05 8:18 
    GeneralCatching Scroll Bar Events Pin
    Humphrey Chakma6-Apr-04 19:51
    memberHumphrey Chakma6-Apr-04 19:51 
    QuestionHow to apply on a dialog ? Pin
    Brian van der Beek31-Mar-03 10:15
    memberBrian van der Beek31-Mar-03 10:15 
    AnswerRe: How to apply on a dialog ? Pin
    SteveTheHalfB7-Apr-03 5:52
    memberSteveTheHalfB7-Apr-03 5:52 
    GeneralRe: How to apply on a dialog ? Pin
    Brian van der Beek7-Apr-03 20:53
    memberBrian van der Beek7-Apr-03 20:53 
    GeneralGood work, but how to Pin
    Anonymous26-Sep-02 17:57
    sussAnonymous26-Sep-02 17:57 
    GeneralScrolling flickering Pin
    Antonis29-Aug-02 1:47
    sussAntonis29-Aug-02 1:47 
    GeneralRe: Scrolling flickering Pin
    Roger Allen29-Aug-02 3:21
    memberRoger Allen29-Aug-02 3:21 
    GeneralFinally... Pin
    Hockey10-Aug-02 13:18
    memberHockey10-Aug-02 13:18 
    GeneralFlikering too much while resizing! Pin
    Anonymous11-Jun-02 9:35
    memberAnonymous11-Jun-02 9:35 
    GeneralRe: Flikering too much while resizing! Pin
    Roger Allen14-Jun-02 6:24
    memberRoger Allen14-Jun-02 6:24 
    GeneralRe: Flikering too much while resizing! Pin
    Anonymous14-Jun-02 10:42
    memberAnonymous14-Jun-02 10:42 
    GeneralSo how's about a demo :) Pin
    Chris Maunder16-May-02 2:38
    adminChris Maunder16-May-02 2:38 
    GeneralA demo of this is now available Pin
    Roger Allen10-Jun-02 3:22
    memberRoger Allen10-Jun-02 3:22 
    GeneralRe: A demo of this is now available Pin
    Chris Maunder10-Jun-02 13:22
    adminChris Maunder10-Jun-02 13:22 
    GeneralRe: A demo of this is now available Pin
    vbmanvi16-Jan-09 18:08
    membervbmanvi16-Jan-09 18:08 

    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.

    Permalink | Advertise | Privacy | Terms of Use | Mobile
    Web01 | 2.8.170813.1 | Last Updated 14 Jun 2002
    Article Copyright 2002 by Roger Allen
    Everything else Copyright © CodeProject, 1999-2017
    Layout: fixed | fluid