在對話框中增加一個滾動條控件,添加相應的相應消息的時候,碰到一個效果。
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說明如下
這個就提供了多個滾動條操作的方法。