MFC ScrollBar Control Example

The most common scrollbar messages are

SB_LINEUP – is sent when the scrollbar moves up one position
SB_LINEDOWN – is sent when the scrollbar moves down one position
SB_PAGEUP – is sent when the scrollbar is moved up one page
SB_PAGEDOWN – is sent when the scrollbar is moved down one page
SB_LINELEFT – is sent when the scrollbar is moved left one position
SB_LINERIGHT – is sent when the scrollbar is moved right one position
SB_PAGELEFT – is sent when the scrollbar is moved one page left
SB_PAGERIGHT – is sent when the scrollbar is moved one page right
SB_THUMBPOSITION – is sent after the thumbbar is dragged to a new position
SB_THUMBTRACK – is sent while the thumbbar is dragged to a new position

The CScrollbar member function SetScrollRange() is used to set the minimum and maximum scroll box positions and the GetScrollPos() and SetScrollPos() member functions retrieves/sets the current position of the scroll box (thumb).

Scrollbar information is passed to the parent windows by use of the ON_WM_VSCROLL and ON_WM_HSCROLL message macros


#include <afxwin.h>
#define ID_SCROLLBAR 1000
#define ID_STATIC 1001
class CSimpleApp : public CWinApp
{
public:
BOOL InitInstance();
};

class CMainFrame : public CFrameWnd
{
public:
CMainFrame();
afx_msg void SetLabel(int );
afx_msg void OnHScroll(UINT , UINT nPos, CScrollBar* );
DECLARE_MESSAGE_MAP()
CScrollBar wScrollbar;//instantiate scrollbar
CStatic wStatic;
};


BOOL CSimpleApp::InitInstance(){
m_pMainWnd = new CMainFrame();
m_pMainWnd->ShowWindow(m_nCmdShow);
return TRUE;
}

CMainFrame::CMainFrame()
{
Create(NULL, _T("MFC scrollbar example"), WS_OVERLAPPEDWINDOW ,CRect(25,25,450,170));

wStatic.Create(_T(""),WS_CHILD | WS_VISIBLE | WS_BORDER , CRect(25,60,75,90), this, ID_STATIC);
wScrollbar.Create(WS_CHILD | WS_VISIBLE | SBS_HORZ, CRect(10, 10, 410, 50), this, ID_SCROLLBAR );
wScrollbar.SetScrollRange(1,100,true);//set scroll bar range
int p=wScrollbar.GetScrollPos();
SetLabel(p);
}
BEGIN_MESSAGE_MAP(CMainFrame,CFrameWnd)
ON_WM_HSCROLL()
END_MESSAGE_MAP()
CSimpleApp MFCApp1;

//deals with scrollbar scrolling
afx_msg void CMainFrame::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{

int minpos;
int maxpos;
pScrollBar->GetScrollRange(&minpos, &maxpos);
int curpos = pScrollBar->GetScrollPos();
switch(nSBCode)
{
case SB_LEFT: //Scrolls to the lower right.
curpos = minpos;
break;

case SB_RIGHT: //Scrolls to the lower right.
curpos = maxpos;
break;

case SB_ENDSCROLL: //Ends scroll.
break;

case SB_LINEDOWN: //Scrolls one line down.
curpos++;
break;

case SB_LINEUP: //Scrolls one line up.
curpos--;
break;

case SB_PAGEDOWN: //Scrolls one page down.
curpos+=5;
break;

case SB_PAGEUP: //Scrolls one page up.
curpos-=5;
break;

case SB_THUMBPOSITION: //The user has dragged the scroll box (thumb) and released the mouse button. The nPos parameter indicates the position of the scroll box at the end of the drag operation.
curpos = nPos;
break;

case SB_THUMBTRACK: //The user is dragging the scroll box. This message is sent repeatedly until the user releases the mouse button. The nPos parameter indicates the position that the scroll box has been dragged to.
curpos = nPos;
break;
}

pScrollBar->SetScrollPos(curpos);
SetLabel(curpos);
}

//display value of scollbar in label
afx_msg void CMainFrame::SetLabel(int newvalue)
{
CString conv;
conv.Format(_T("%d"), newvalue);
wStatic.SetWindowText (_T (conv)); }