Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

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


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

// TODO: Add extra initialization here
int ButtonWidth = GetSystemMetrics(SM_CXHSCROLL);

Scroller->Create(WS_CHILD | WS_VISIBLE |
SBS_VERT,
CRect(200, 15, 200+ButtonWidth, 148), this, 0x16);

return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}

18.4.5..Scroll Bar Methods..............................................................................


A scroll bar is based on the CScrollBar class. We already that, to programatically create
this control, you can declare a pointer to its class and initialize it using its Create()
method. Since a scroll bar is a value-range control, you must specify its minimum and
maximum values from which extremes the thumb can be navigated. To set the range of
values of a scroll bar, you can call the CScrollBar::SetScrollRange() method. Its syntax
is:

void SetScrollRange(int nMinPos, int nMaxPos, BOOL bRedraw = TRUE);

The nMinPos argument specifies the minimum value of the control. The nMaxPos
argument is the maximum value of the range. The difference between nMaxPos and
nMinPos must not be greater than 32,767. When the values have been changed, you may
want to refresh the control to reflect the change. This is the default bahavior. If for some
reason you do not want the control to be redrawn, pass a third argument as FALSE.

Here is an example:

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

m_Scroller.SetScrollRange(8, 120);

return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}

If the range of a values has already been set for a scroll bar and you want to get its
minimum and its maximum possible values, call the CScrollBar::GetScrollRange()
method. Its syntax is:

void GetScrollRange(LPINT lpMinPos, LPINT lpMaxPos) const;

This method returns two values, lpMinPos and lpMaxPos. Here is an example:

void CScrollingDlg::OnBtnInfo()
{
Free download pdf