Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Visual C++ and MFC Fundamentals Chapter 18: Progress-Based Controls


If the position of the thumb on a scroll bar control has changed, which happens while the
user is scrolling,, you can get the current position of the thumb by calling the
CScrollBar::GetScrollPos() method. Its syntax is:

int GetScrollPos() const;

This method returns the position of the thumb on the scroll bar.

To set the minimum and the maximum values of a scroll bar, as well as the initial
position of the thumb, you can call the CScrollBar::SetScrollInfo() method. Its syntax is:

BOOL SetScrollInfo(LPSCROLLINFO lpScrollInfo, BOOL bRedraw = TRUE);

The new values to be passed to this method are stored in a SCROLLINFO variable.
Therefore, you should first build that variable. The SCROLLINFO structure is defined as
follows:

typedef struct tagSCROLLINFO {
UINT cbSize;
UINT fMask;
int nMin;
int nMax;
UINT nPage;
int nPos;
int nTrackPos;
} SCROLLINFO, *LPSCROLLINFO;
typedef SCROLLINFO CONST *LPCSCROLLINFO;

The cbSize member variable is the size of the structure in bytes. The fMask value
specifies the type of value that you want to set. The possible values of fMask are:

Value Description

SIF_RANGE Used to set only the minimum and the maximum values
SIF_POS Used to set only the initial position of the scroll thumb
SIF_PAGE Used to set the page size to scroll when using the control
SIF_DISABLENOSCROLL Used to disable the scroll bar
SIF_ALL Used to perform all allowed operations

The nMin value is used to set the minimum value of the scroll bar while nMax specifies
its maximum value. The nPage value holds the value of the page scrolling. The nPos is
used to set the initial position of the thumb. The nTrackPos member variable must be
ignored if you are setting values for the scroll bar.

Here is an example:

BOOL CScrollingDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// TODO: Add extra initialization here
SCROLLINFO ScrInfo;

ScrInfo.cbSize = sizeof(SCROLLINFO);
ScrInfo.fMask = SIF_ALL;
ScrInfo.nMin = 25;
Free download pdf