在对话框中增加一个滚动条控件,添加相应的相应消息的时候,碰到一个效果。
BOOL CTestDialogProDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
//////////////////////////////////////////////////////////////////////////
SetScrollRange(SB_HORZ, 0, 10);
return TRUE; // return TRUE unless you set the focus to a control
}
竟然出现了整个对话框的滚动条。实际上,就是我使用滚动条错误造成的,在初始化中要改。不是上面代码。
要写成下面的
((CScrollBar*)GetDlgItem(IDC_SCROLLBAR1))->SetScrollRange(0, 10);
滚动条的消息函数就参考MSDN上的示例些就好了。示例代码如下
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);
}
我的滚动条只需要做到,按住滚动条一种操作,擡起鼠标就停止。所以就把多余的给删掉了
void CTestDialogProDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
// Get the minimum and maximum scroll-bar positions.
int minpos;
int maxpos;
pScrollBar->GetScrollRange(&minpos, &maxpos);
// Get the current position of scroll box.
int curpos = pScrollBar->GetScrollPos();
// Determine the new position of scroll box.
switch (nSBCode)
{
case SB_ENDSCROLL: // End scroll.//鼠标擡起
break;
case SB_LINELEFT: // Scroll left.//按住左端按钮
if (curpos > minpos)
curpos--;
else
curpos = maxpos;
break;
case SB_LINERIGHT: // Scroll right.//按住右端按钮
if (curpos < maxpos)
curpos++;
else
curpos = minpos;
break;
}
// Set the new position of the thumb (scroll box).
pScrollBar->SetScrollPos(curpos);
CDialogEx::OnHScroll(nSBCode, nPos, pScrollBar);
}
在消息函数中,CScrollBar* pScrollBar这个参数就是对应操作的滚动条控件,如果是空,就是界面带的那个控件。MSDN说明如下
这个就提供了多个滚动条操作的方法。