CWnd::OnHScroll
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 );
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. |
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); }
{
// Where your CYourView class inherits from CScrollView.
// Provide full 32 bit position value of scroll bar thumb to replace 16 bit value provided in message.
// Code from: social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/ec6ec25a-884a-475e-97ee-b9695f786aad
if ( nSBCode == SB_THUMBTRACK || nSBCode == SB_THUMBPOSITION )
{
// First determine if the user scrolled a scroll bar control on the window or scrolled the window itself
HWND hWndScroll;
if ( pScrollBar == NULL )
{
hWndScroll = m_hWnd;
}
else
{
hWndScroll = pScrollBar->m_hWnd;
}
// Get the scroll info from MFC.
SCROLLINFO info;
info.cbSize = sizeof(SCROLLINFO);
info.fMask = SIF_TRACKPOS;
::GetScrollInfo(hWndScroll, SB_HORZ, &info);
// Extract the current scroll position (32 bit).
nPos = info.nTrackPos;
}
CScrollView::OnHScroll(nSBCode, nPos, pScrollBar);
} // end OnHScroll()
- 11/16/2011
- PeeeeetL
Note