Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Expand Minimize
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.


The framework calls this member function when the user clicks a window's horizontal scroll bar.

afx_msg void OnHScroll(
   UINT nSBCode,
   UINT nPos,
   CScrollBar* pScrollBar 



Specifies a scroll-bar code that indicates the user's scrolling request. This parameter can be one of the following:

  • SB_LEFT   Scroll to far left.

  • SB_ENDSCROLL   End scroll.

  • SB_LINELEFT   Scroll left.

  • SB_LINERIGHT   Scroll right.

  • SB_PAGELEFT   Scroll one page left.

  • SB_PAGERIGHT   Scroll one page right.

  • SB_RIGHT   Scroll to far right.

  • SB_THUMBPOSITION   Scroll to absolute position. The current position is specified by the nPos parameter.

  • SB_THUMBTRACK   Drag scroll box to specified position. The current position is specified by the nPos parameter.


Specifies the scroll-box position if the scroll-bar code is SB_THUMBPOSITION or SB_THUMBTRACK; otherwise, not used. Depending on the initial scroll range, nPos may be negative and should be cast to an int if necessary.


If the scroll message came from a scroll-bar control, contains a pointer to the control. If the user clicked a window's scroll bar, this parameter is NULL. The pointer may be temporary and should not be stored for later use.

The SB_THUMBTRACK scroll-bar code typically is used by applications that give some feedback while the scroll box is being dragged.

If an application scrolls the contents controlled by the scroll bar, it must also reset the position of the scroll box with the SetScrollPos member function.


This member function is called by the framework to allow your application to handle a Windows message. The parameters passed to your function reflect the parameters received by the framework when the message was received. If you call the base-class implementation of this function, that implementation will use the parameters originally passed with the message and not the parameters you supply to the function.

void CMyView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
   // Get the minimum and maximum scroll-bar positions.
   int minpos;
   int maxpos;
   pScrollBar->GetScrollRange(&minpos, &maxpos); 
   maxpos = pScrollBar->GetScrollLimit();

   // Get the current position of scroll box.
   int curpos = pScrollBar->GetScrollPos();

   // Determine the new position of scroll box.
   switch (nSBCode)
   case SB_LEFT:      // Scroll to far left.
      curpos = minpos;

   case SB_RIGHT:      // Scroll to far right.
      curpos = maxpos;

   case SB_ENDSCROLL:   // End scroll.

   case SB_LINELEFT:      // Scroll left.
      if (curpos > minpos)

   case SB_LINERIGHT:   // Scroll right.
      if (curpos < maxpos)

   case SB_PAGELEFT:    // Scroll one page left.
      // Get the page size. 
      SCROLLINFO   info;
      pScrollBar->GetScrollInfo(&info, SIF_ALL);
      if (curpos > minpos)
      curpos = max(minpos, curpos - (int) info.nPage);

   case SB_PAGERIGHT:      // Scroll one page right.
      // Get the page size. 
      SCROLLINFO   info;
      pScrollBar->GetScrollInfo(&info, SIF_ALL);

      if (curpos < maxpos)
         curpos = min(maxpos, curpos + (int) info.nPage);

   case SB_THUMBPOSITION: // Scroll to absolute position. nPos is the position
      curpos = nPos;      // of the scroll box at the end of the drag operation.

   case SB_THUMBTRACK:   // Drag scroll box to specified position. nPos is the
      curpos = nPos;     // position that the scroll box has been dragged to.

   // Set the new position of the thumb (scroll box).

   CView::OnHScroll(nSBCode, nPos, pScrollBar);

Community Additions

© 2015 Microsoft