Click here to Skip to main content
Click here to Skip to main content

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

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)

    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]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

     
    GeneralMy vote of 2 Pinmemberjonclegg10-Jan-11 5:59 
    Generalplease help me [modified] PinmemberMausumi10-Jan-07 0:55 
    GeneralNot scrolling all the way PinmemberBudric B.6-Jul-05 8:18 
    GeneralCatching Scroll Bar Events PinmemberHumphrey Chakma6-Apr-04 19:51 
    QuestionHow to apply on a dialog ? PinmemberBrian van der Beek31-Mar-03 10:15 
    AnswerRe: How to apply on a dialog ? PinmemberSteveTheHalfB7-Apr-03 5:52 
    GeneralRe: How to apply on a dialog ? PinmemberBrian van der Beek7-Apr-03 20:53 
    GeneralGood work, but how to PinsussAnonymous26-Sep-02 17:57 
    GeneralScrolling flickering PinsussAntonis29-Aug-02 1:47 
    GeneralRe: Scrolling flickering PinmemberRoger Allen29-Aug-02 3:21 
    GeneralFinally... PinmemberHockey10-Aug-02 13:18 
    GeneralFlikering too much while resizing! PinmemberAnonymous11-Jun-02 9:35 
    GeneralRe: Flikering too much while resizing! PinmemberRoger Allen14-Jun-02 6:24 
    GeneralRe: Flikering too much while resizing! PinmemberAnonymous14-Jun-02 10:42 
    GeneralSo how's about a demo :) PinadminChris Maunder16-May-02 2:38 
    GeneralA demo of this is now available PinmemberRoger Allen10-Jun-02 3:22 
    GeneralRe: A demo of this is now available PinadminChris Maunder10-Jun-02 13:22 
    GeneralRe: A demo of this is now available Pinmembervbmanvi16-Jan-09 18:08 

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

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

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