ScrollBar的消息处理
ScrollBar分成窗口的ScrollBar,和单独的ScrollBar两种,处理方式有所不同,但差别不大。
如果是窗口的ScrollBar,OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
中的pScrollBar为NULL,要用GetScrollLimit(SB_VERT);
如果是控件的就用SB_CTL与之对应。
下面是CMySVView(从CView继承)的消息处理(参考MSDN)框架
void CMySVView::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
// Get the minimum and maximum scroll-bar positions.
int minpos;
int maxpos;
GetScrollRange(SB_VERT, &minpos, &maxpos);
maxpos = GetScrollLimit(SB_VERT);
// Get the current position of scroll box.
int curpos = GetScrollPos(SB_VERT);
// Determine the new position of scroll box.
switch (nSBCode)
{
case SB_TOP: // Scroll to far TOP.
curpos = minpos;
break;
case SB_BOTTOM: // Scroll to far BOTTOM.
curpos = maxpos;
break;
case SB_ENDSCROLL: // End scroll.
break;
case SB_LINEUP: // Scroll UP.
if (curpos > minpos)
curpos--;
break;
case SB_LINEDOWN: // Scroll DOWN.
if (curpos < maxpos)
curpos++;
break;
case SB_PAGEUP: // Scroll one page UP.
{
// Get the page size.
SCROLLINFO info;
GetScrollInfo(SB_VERT, &info, SIF_ALL);
if (curpos > minpos)
curpos = max(minpos, curpos - (int) info.nPage);
}
break;
case SB_PAGEDOWN: // Scroll one page DOWN.
{
// Get the page size.
SCROLLINFO info;
GetScrollInfo(SB_VERT, &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_VERT, curpos);
CView::OnVScroll(nSBCode, nPos, pScrollBar);
}
void CMySVView::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);
}
ScrollBar的消息处理
最新推荐文章于 2025-12-03 15:15:40 发布
本文深入探讨了滚动条消息处理的实现方式,包括窗口滚动条和控件滚动条的处理方法,通过CMySVView消息处理框架展示了滚动条操作的细节。
1万+

被折叠的 条评论
为什么被折叠?



