Click here to Skip to main content
15,881,715 members
Articles / Desktop Programming / MFC
Article

Scrollbars, using the WS_HSCROLL and WS_VSCROLL styles

Rate me:
Please Sign up or sign in to vote.
4.76/5 (17 votes)
13 Jun 2002CPOL3 min read 175.6K   3.5K   38   19
How to use a CWnd's default scrollbars and to setup the thumb size correctly

Overview

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; 
    }   SCROLLINFO, *LPSCROLLINFO; 

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.

    Updates

  • 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)


    Written By
    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]http://www.ryushinkan.co.uk/[/url] Karate at this time of my life, training from 4-6 times a week and recently achieved my 1st Dan after 6 years.

    Comments and Discussions

     
    GeneralSubject Pin
    Javene Mcgowan29-Oct-21 17:11
    Javene Mcgowan29-Oct-21 17:11 
    GeneralMy vote of 2 Pin
    jonclegg10-Jan-11 5:59
    jonclegg10-Jan-11 5:59 
    Generalplease help me [modified] Pin
    Mausumi10-Jan-07 0:55
    Mausumi10-Jan-07 0:55 
    GeneralNot scrolling all the way Pin
    Budric B.6-Jul-05 8:18
    Budric B.6-Jul-05 8:18 
    GeneralCatching Scroll Bar Events Pin
    Humphrey Chakma6-Apr-04 19:51
    Humphrey Chakma6-Apr-04 19:51 
    QuestionHow to apply on a dialog ? Pin
    Brian van der Beek31-Mar-03 10:15
    Brian van der Beek31-Mar-03 10:15 
    AnswerRe: How to apply on a dialog ? Pin
    SteveTheHalfB7-Apr-03 5:52
    SteveTheHalfB7-Apr-03 5:52 
    I have the same problem, did you get anywhere with it?
    GeneralRe: How to apply on a dialog ? Pin
    Brian van der Beek7-Apr-03 20:53
    Brian van der Beek7-Apr-03 20:53 
    GeneralGood work, but how to Pin
    Anonymous26-Sep-02 17:57
    Anonymous26-Sep-02 17:57 
    GeneralScrolling flickering Pin
    hantouan29-Aug-02 1:47
    hantouan29-Aug-02 1:47 
    GeneralRe: Scrolling flickering Pin
    Roger Allen29-Aug-02 3:21
    Roger Allen29-Aug-02 3:21 
    GeneralFinally... Pin
    alex.barylski10-Aug-02 13:18
    alex.barylski10-Aug-02 13:18 
    GeneralFlikering too much while resizing! Pin
    11-Jun-02 9:35
    suss11-Jun-02 9:35 
    GeneralRe: Flikering too much while resizing! Pin
    Roger Allen14-Jun-02 6:24
    Roger Allen14-Jun-02 6:24 
    GeneralRe: Flikering too much while resizing! Pin
    14-Jun-02 10:42
    suss14-Jun-02 10:42 
    GeneralSo how's about a demo :) Pin
    Chris Maunder16-May-02 2:38
    cofounderChris Maunder16-May-02 2:38 
    GeneralA demo of this is now available Pin
    Roger Allen10-Jun-02 3:22
    Roger Allen10-Jun-02 3:22 
    GeneralRe: A demo of this is now available Pin
    Chris Maunder10-Jun-02 13:22
    cofounderChris Maunder10-Jun-02 13:22 
    GeneralRe: A demo of this is now available Pin
    vbmanvi16-Jan-09 18:08
    vbmanvi16-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.