简体   繁体   English

CScrollbar SetScrollInfo无效

[英]CScrollbar SetScrollInfo has no effect

I have similar problem to this one: How do you use MFC CScrollbar controls? 我对此有类似的问题: 如何使用MFC CScrollbar控件? but I figured out that my ON_WM_VSCROLL message is sending parameter nPos always equal 0. I thought that I should set the scrollbar with SetScrollInfo method or at least with SetScrollRange , and I try to do it in the PreCreateWindow() of a View class function (which is derived from CFormView ). 但是我发现我的ON_WM_VSCROLL消息正在发送参数nPos始终等于0。我认为我应该使用SetScrollInfo方法或至少使用SetScrollRange设置滚动条,然后尝试在View类函数的PreCreateWindow()进行设置(这是从CFormView派生的。

Nevertheless the scrollbar doesn't seem to get data from the SCROLLINFO structure. 但是,滚动条似乎并没有从SCROLLINFO结构获取数据。

Here are my code samples: 这是我的代码示例:

  BOOL CInterfaceView::PreCreateWindow(CREATESTRUCT& cs)
   {
    // TODO: Modify the Window class or styles here by modifying
    //  the CREATESTRUCT cs
    drawphoto=false;  //other unrelated variables;
    zoomfactor=1.0;

    info1.cbSize=sizeof(SCROLLINFO); //SCROLLINFO global variable
    info1.fMask=SIF_ALL;
    info1.nMin=0;
    info1.nMax=100;
    info1.nPage=2;
    info1.nPos=5;
    info1.nTrackPos=2;

    ScrollBar1.SetScrollInfo(&info1);   //the vertical ScrollBar
//  ScrollBar1.SetScrollRange(0,100);   //this has no effect either
    return CFormView::PreCreateWindow(cs);
}

VSCROLL message handler: VSCROLL消息处理程序:

void CInterfaceView::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
    int CurPos = pScrollBar->GetScrollPos();
//debug code:
    CString test;
    int rn,rx;
        pScrollBar->GetScrollRange(&rn,&rx);
    test.Format(_T("%d %d %d\n"),nPos,CurPos,rx-rn);
    if(pScrollBar!=NULL)
    TRACE(test+_T(" dzialamy\n"));
//end debug code
//this part found on the Internet
    // Determine the new position of scroll box.
    switch (nSBCode)
    {
    case SB_LEFT:      // Scroll to far left.
        CurPos = 0;
        break;

    case SB_RIGHT:      // Scroll to far right.
        CurPos = 100;
        break;

    case SB_ENDSCROLL:   // End scroll.
        break;

    case SB_LINELEFT:      // Scroll left.
        if (CurPos > 0)
            CurPos--;
        break;

    case SB_LINERIGHT:   // Scroll right.
        if (CurPos < 100)
            CurPos++;
        break;

    case SB_PAGELEFT:    // Scroll one page left.
        {
            // Get the page size. 
            SCROLLINFO   info;
            ScrollBar1.GetScrollInfo(&info, SIF_ALL);

            if (CurPos > 0)
                CurPos = max(0, CurPos - (int) info.nPage);
        }
        break;

    case SB_PAGERIGHT:      // Scroll one page right
        {
            // Get the page size. 
            SCROLLINFO   info;
            ScrollBar1.GetScrollInfo(&info, SIF_ALL);

            if (CurPos < 100)
                CurPos = min(100, 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).
    ScrollBar1.SetScrollPos(50);  //orignally it was CurPos
    CFormView::OnVScroll(nSBCode, 50, pScrollBar);
//  ScrollBar1.SetScrollPos(nPos);
}

So I suspect, that I try to set the scrollbar either in wrong place, or do something wrong with it? 因此,我怀疑是尝试将滚动条设置在错误的位置还是做错了什么? I appreciate any help. 感谢您的帮助。

PreCreateWindow is called before the window (and its scroll bar) have been created. 在创建窗口(及其滚动条)之前,将调用PreCreateWindow。 In a view class you should do the initialization in OnInitialUpdate. 在视图类中,您应该在OnInitialUpdate中进行初始化。 This is called after window creation but before the window becomes visible. 在创建窗口之后但在窗口变为可见之前调用此方法。

我认为PreCreateWindow()还为时过早,无法设置滚动条,“正确的”位置应该是CDocument派生的类加载/修改了会影响滚动条范围的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM