Bootstrap

ScrollBar控件

当在使用滚动条控件时,必须要对滚动条操作的消息进行响应
WM_HSCROLL为滚动条的消息响应..
因为我们对滚动条的操作主要是对别的控件进行控制,比如EditControl.所以滚动条的消息响应方式需要自己来实现

在响应滚动条消息之前,要对滚动条的类型和范围进行初始化

    SCROLLINFO scrollinfo = { 0 };
    scrollinfo.cbSize = sizeof(SCROLLINFO);
    scrollinfo.fMask = SIF_RANGE | SIF_PAGE;
    scrollinfo.nMin = 0;
    scrollinfo.nPage = 5;
    scrollinfo.nMax = 100 + scrollinfo.nPage - 1;
    m_scroll.SetScrollInfo(&scrollinfo);
void CScorllBarDemoDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
    // TODO: 在此添加消息处理程序代码和/或调用默认值
    SCROLLINFO scrollinfo = { 0 };
    scrollinfo.cbSize = sizeof(SCROLLINFO);
    scrollinfo.fMask = SIF_ALL;
    pScrollBar->GetScrollInfo(&scrollinfo);
    CString output;
    output.Format(L"当前POS:%d", scrollinfo.nPos);
    SetDlgItemTextW(IDC_STATIC_POS, output);

    int nNewPos = scrollinfo.nPos;
    switch (nSBCode)
    {
    case SB_THUMBTRACK:         // 用户拖动滚动条
        nNewPos = nPos;
        break;
    case SB_LINELEFT:           //左边的按钮
        nNewPos -= 1;
        break;
    case SB_LINERIGHT:          //右边的按钮
        nNewPos += 1;
        break;
    case SB_PAGELEFT:           //点击左边
        nNewPos -= scrollinfo.nPage;
        break;
    case SB_PAGERIGHT:          //点击右边
        nNewPos += scrollinfo.nPage;
        break;
    }
    if (nNewPos > scrollinfo.nMax)
        nNewPos = scrollinfo.nMax;
    if (nNewPos < scrollinfo.nMin)
        nNewPos = scrollinfo.nMin;
    scrollinfo.nPos = nNewPos;
    pScrollBar->SetScrollInfo(&scrollinfo);

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