C ++ MFC添加水平滚动条以调整窗口对话框的大小

时间:2015-07-13 02:42:52

标签: c++ mfc

我想添加一个水平滚动条来调整对话框的大小。 我已经有一个垂直滚动条用于该对话框工作。 但是当我复制并粘贴相同的VScroll代码并用Dialog.cx替换所有相关的VScroll与HScroll和Dialog.cy时,它仍然无效。

任何人都可以提供帮助吗?

1 个答案:

答案 0 :(得分:0)

以下是摘录,如果这没有帮助,因为其他人已经建议您应该发布一些代码。

    CScrollBar m_HScrollBar;
    CScrollBar m_VScrollBar;
    SCROLLINFO m_horz, m_vert;

    int m_iSrcX;
    int m_iSrcY;

BEGIN_MESSAGE_MAP(YourClass, CDialogEx)
    ON_WM_VSCROLL()
    ON_WM_HSCROLL()
    ON_WM_SIZE()
END_MESSAGE_MAP()


void YourClass::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
    switch (nSBCode)
    {
    case SB_TOP:
        m_iSrcY = 0;
        break;
    case SB_BOTTOM:
        m_iSrcY = INT_MAX;
        break;
    case SB_THUMBPOSITION:
    case SB_THUMBTRACK:
        m_iSrcY = nPos;
        break;

    case SB_PAGEDOWN:
    case SB_LINEDOWN:
        if (m_iSrcY >= INT_MAX)
        {
            m_iSrcY = INT_MAX;
            return;
        }
        m_iSrcY += 10;
        break;
    case SB_PAGEUP:
    case SB_LINEUP:
        if (m_iSrcY <= (-INT_MAX))
        {
            m_iSrcY = 0;
            return;
        }
        m_iSrcY -= 10;
        break;
    }
    m_VScrollBar.SetScrollPos(m_iSrcY);
Invalidate();
    CDialogEx::OnVScroll(nSBCode, nPos, pScrollBar);
}

void YourClass::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
    switch (nSBCode)
    {
    case SB_TOP:
        m_iSrcX = 0;
        break;
    case SB_BOTTOM:
        m_iSrcX = INT_MAX;
        break;
    case SB_THUMBPOSITION:
    case SB_THUMBTRACK:
        m_iSrcX = nPos;
        break;
    case SB_PAGERIGHT:
    case SB_LINERIGHT:
        if (m_iSrcX >= INT_MAX)
        {
            m_iSrcX = INT_MAX;
            return;
        }
        m_iSrcX += 10;
        break;
    case SB_PAGELEFT:
    case SB_LINELEFT:
        if (m_iSrcX <= (-INT_MAX))
        {
            m_iSrcX = 0;
            return;
        }
        m_iSrcX -= 10;
        break;
    }

    m_HScrollBar.SetScrollPos(m_iSrcX);
    Invalidate();

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


void YourClass::OnSize(UINT nType, int cx, int cy)
{

        if (m_VScrollBar.GetSafeHwnd() && m_HScrollBar.GetSafeHwnd())
        {
            m_VScrollBar.SetWindowPos(NULL, cx - 20, 25, 20, iBottom, SWP_NOZORDER);
            m_HScrollBar.SetWindowPos(NULL, iLeft, cy - 20, cx - cx / 2 - 20, 20, SWP_NOZORDER);
        }


            //Horizontal Scroll Info Structure
            m_horz.cbSize = sizeof(SCROLLINFO);
            m_horz.fMask = SIF_ALL;
            m_horz.nMin = 0;
            m_horz.nMax = m_MaxWidth;
            m_horz.nPage = rWnd.Width() / 10;
            m_horz.nPos = m_iSrcX;
            m_horz.nTrackPos = 0;
            m_HScrollBar.SetScrollInfo(&m_horz);
            //Vertical Scroll Info Structure
            m_vert.cbSize = sizeof(SCROLLINFO);
            m_vert.fMask = SIF_ALL;
            m_vert.nMin = 0;
            m_vert.nMax = m_MaxHeight;
            m_vert.nPage = rWnd.Height() / 10;
            m_vert.nPos = m_iSrcY;
            m_vert.nTrackPos = 0;
            m_VScrollBar.SetScrollInfo(&m_vert);
}

有些事情显然是缺失/不完整的,但这应该会给你一些想法。