Handling The ScrollBar | CodeGuru

Handling The ScrollBar

Here is a simple example illustrating how to programmatically handle the scroll bar (and it’s messages). 1. First set up the dimensions of the control: SCROLLINFO ScrollInfo; ScrollInfo.cbSize = sizeof(ScrollInfo); // size of this structure ScrollInfo.fMask = SIF_ALL; // parameters to set ScrollInfo.nMin = 0; // minimum scrolling position ScrollInfo.nMax = 100; // maximum scrolling […]

Written By
CodeGuru Staff
CodeGuru Staff
Jul 28, 1999
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Here is a simple example illustrating how to programmatically handle
the scroll bar (and it’s messages).

1. First set up the dimensions of the control:

  SCROLLINFO ScrollInfo;
  ScrollInfo.cbSize = sizeof(ScrollInfo);     // size of this structure
  ScrollInfo.fMask = SIF_ALL;                 // parameters to set
  ScrollInfo.nMin = 0;                        // minimum scrolling position
  ScrollInfo.nMax = 100;                      // maximum scrolling position
  ScrollInfo.nPage = 40;                      // the page size of the scroll box
  ScrollInfo.nPos = 50;                       // initial position of the scroll box
  ScrollInfo.nTrackPos = 0;                   // immediate position of a scroll box that the user is dragging
  m_MyScrollBar.SetScrollInfo(&ScrollInfo);

2. Now override the WM_VSCROLL or WM_HSCROLL (for a horizontal control) function in Your ScrollBar class:

void CMyScrollBar::VScroll(UINT nSBCode, UINT nPos)
{
  SCROLLINFO ScrollInfo;
  GetScrollInfo(&ScrollInfo);  // get information about the scroll

  switch(nSBCode)
  {
    case SB_BOTTOM:         //Scrolls to the lower right.
      break;

    case SB_ENDSCROLL:      //Ends scroll.
      break;

    case SB_LINEDOWN:       //Scrolls one line down.
      SetScrollPos(GetScrollPos() + 1);
      break;

    case SB_LINEUP:         //Scrolls one line up.
      SetScrollPos(GetScrollPos() - 1);
      break;

    case SB_PAGEDOWN:       //Scrolls one page down.
      SetScrollPos(GetScrollPos() + ScrollInfo.nPage);
      break;

    case SB_PAGEUP:         //Scrolls one page up.
      SetScrollPos(GetScrollPos() - ScrollInfo.nPage);
      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.
      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.
      SetScrollPos(nPos);
      break;

    case SB_TOP:            //Scrolls to the upper left.
      break;
  }
}

3.That’s it! With this code snippet, you can easily add a scrollbar
control to your application and insert your own application specific
logic to handle the different messages sent to your
application when the user scrolls your document.

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.