Share via


CWnd::OnHScroll

The framework calls this member function when the user clicks a horizontally-aligned scroll bar, spin button control, or slider control.

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

Parameters

  • nSBCode
    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.

  • nPos
    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.

  • pScrollBar
    If the scroll message came from a scroll-bar control, a spin button control, or a slider control, this parameter 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.

    If the referenced control is either a spin button or a slider, you must cast the pointer to a CScrollBar*.

Remarks

When you click a spin button control, the value of nSBCode is SB_THUMBPOSITION.

The nPos parameter is a 16-bit value and must be used only for controls whose range value uses 16 bits or fewer.

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.

Note

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.

Example

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

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

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

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

   case SB_ENDSCROLL:   // End scroll. 
      break;

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

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

   case SB_PAGELEFT:    // Scroll one page left.
   {
      // Get the page size. 
      SCROLLINFO   info;
      GetScrollInfo(SB_HORZ, &info, SIF_ALL);

      if (curpos > minpos)
      curpos = max(minpos, curpos - (int) info.nPage);
   }
      break;

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

      if (curpos < maxpos)
         curpos = min(maxpos, 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).
   SetScrollPos(SB_HORZ, curpos);

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

Requirements

Header: afxwin.h

See Also

Concepts

CWnd Members

Reference

CWnd Class

Hierarchy Chart

CWnd::SetScrollPos

CWnd::OnVScroll

WM_HSCROLL

CSpinButtonCtrl Class

CSliderCtrl Class

Change History

Date

History

Reason

Added information about CSpinButtonCtrl and CSliderCtrl ; specified nPos as 16-bit.

Customer feedback.