有没有人曾经使用过"滚动箭头"在水平滚动条中滚动位置?

时间:2016-02-12 04:01:25

标签: c++ visual-c++ mfc user-controls

我希望使用滚动条两端的滚动箭头来滚动条形的位置。众所周知,滚动条不像其他控件那样有通知,所以我遇到了问题。

滚动条的代码如下 -

void CScrollBarExDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
    if(nSBCode==SB_THUMBPOSITION)
    {
        if(pScrollBar==&m_Scroll)
        {

            m_Edit=nPos;
            m_Scroll.SetScrollPos(nPos);
        }

    }
    UpdateData(FALSE);


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

1 个答案:

答案 0 :(得分:1)

您必须稍微修改MSDN example以使用滚动控件:

//add to message map
ON_WM_HSCROLL()

//initialize in OnInitDialog
SCROLLINFO info = { sizeof(SCROLLINFO) };
info.nMin = 0;
info.nMax = 100;
info.nPage = 1; 
info.fMask = SIF_ALL;
m_Scroll.SetScrollInfo(&info, TRUE);

如果info.nPage大于1,则可能会甩开范围。添加此行以修复它:

info.nMax += info.nPage - 1;

添加重载:

void CMyDialog::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
    CDialog::OnVScroll(nSBCode, nPos, pScrollBar);
    if (pScrollBar == &m_Scroll)
    {
        //get scrollbar information
        SCROLLINFO info;
        m_Scroll.GetScrollInfo(&info, SIF_ALL);
        int pos = info.nPos;

        //calculate the new position of scroll box 
        switch (nSBCode)
        {
        case SB_LEFT: pos = info.nMin; break;
        case SB_RIGHT: pos = info.nMax; break;
        case SB_LINELEFT: pos--; break;
        case SB_LINERIGHT: pos++;  break;
        case SB_PAGELEFT: pos -= info.nPage; break;
        case SB_PAGERIGHT: pos += info.nPage; break;
        case SB_THUMBPOSITION: pos = nPos; break;
        case SB_THUMBTRACK: pos = nPos; break;
        }

        //make sure the new position is within range
        if (pos < info.nMin) pos = info.nMin;

        //adjust the max value, incase we had changed it earlier in OnInitDialog
        int max = info.nMax - info.nPage + 1;
        if (pos > max) pos = max;

        //set the new position
        m_Scroll.SetScrollPos(pos);
    }
}