On the things your can do with a scroll bar is to use
its position and involve it for some other operation of your application.
For example, you can use a scroll bar to navigate through a list of items,
the same way you would proceed for a list box. We are going to use a
scroll bar to navigate a list of names and display them in text boxes.
- On the dialog, add a Static Text control and set its Caption to
Country
- On the right side of the static label, add an Edit Box control and
set its ID to IDC_COUNTRY
- Press Ctrl + W to call the ClassWizard dialog box.
- From the Member Variables property sheet, double-click IDC_COUNTRY.
- Set the Member Variable Name to m_Country
- Make sure its Category is Value and its Variable Type is CString
- Click OK to close the ClassWizard dialog box.
- In the Workspace, click the ClassView property sheet. Expand the
ScrollBar1 Classes folder and double-click the CScrollBar folder to
access its header file.
- Create a list of countries as follows:
// ScrollBarDlg.h : header file
//
#if !defined(AFX_SCROLLBARDLG_H__D4577C45_C8B6_4C81_B2D2_D4E7AE3E40C7__INCLUDED_)
#define AFX_SCROLLBARDLG_H__D4577C45_C8B6_4C81_B2D2_D4E7AE3E40C7__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
static char* Country[] = { "Afghanistan", "Lesotho", "Argentina", "Italy",
"New Zealand", "Madagascar", "Sri Lanka", "Brazil", "Gambia", "Chili",
"Greece", "Thailand", "Cuba", "Ghana", "Canada", "Mexico", "Uganda",
"Australia", "Panama", "Qatar", "Senegal", "Turkey", "Uzbekistan",
"Algeria", "Bolivia", "Cameroon", "Finland", "United States" };
/////////////////////////////////////////////////////////////////////////////
// CScrollBarDlg dialog
class CScrollBarDlg : public CDialog
{
// Construction
public:
|
- Change the OnInitDialog() functions as follows:
BOOL CScrollBarDlg::OnInitDialog()
{
CDialog::OnInitDialog();
... // Set small icon
// TODO: Add extra initialization here
m_ScrollBar.SetScrollRange(0, 27);
return TRUE; // return TRUE unless you set the focus to a control
}
|
- Change the OnVScroll() function as follows:
void CScrollBarDlg::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
// TODO: Add your message handler code here and/or call default
UpdateData();
int CurPos = m_ScrollBar.GetScrollPos();
// Determine the new position of scroll box.
switch (nSBCode)
{
case SB_LEFT: // Scroll to far left.
CurPos = 0;
break;
case SB_RIGHT: // Scroll to far right.
CurPos = 27;
break;
case SB_ENDSCROLL: // End scroll.
break;
case SB_LINELEFT: // Scroll left.
if (CurPos > 0)
CurPos--;
break;
case SB_LINERIGHT: // Scroll right.
if (CurPos < 27)
CurPos++;
break;
case SB_PAGELEFT: // Scroll one page left.
{
// Get the page size.
SCROLLINFO info;
m_ScrollBar.GetScrollInfo(&info, SIF_ALL);
if (CurPos > 0)
CurPos = max(0, CurPos - (int) info.nPage);
}
break;
case SB_PAGERIGHT: // Scroll one page right
{
// Get the page size.
SCROLLINFO info;
m_ScrollBar.GetScrollInfo(&info, SIF_ALL);
if (CurPos < 27)
CurPos = min(27, CurPos + (int) info.nPage);
}
break;
case SB_THUMBPOSITION: // Scroll to absolute position. nPos is the position
CurPos = nPos; // of the scroll box at the end of the drag operation.
break;
case SB_THUMBTRACK: // Drag scroll box to specified position. nPos is the
CurPos = nPos; // position that the scroll box has been dragged to.
break;
}
// Set the new position of the thumb (scroll box).
m_ScrollBar.SetScrollPos(CurPos);
CString szPosition;
szPosition.Format("%d", CurPos);
SetDlgItemText(IDC_POSITION, szPosition);
m_Country.Format("%s", Country[CurPos]);
UpdateData(FALSE);
CDialog::OnVScroll(nSBCode, nPos, pScrollBar);
}
|
- Execute your program and test the scroll bar. Notice that the
content of the edit box changes as you scroll up or down.
|