简体   繁体   中英

CScrollbar SetScrollInfo has no effect

I have similar problem to this one: How do you use MFC CScrollbar controls? 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 ).

Nevertheless the scrollbar doesn't seem to get data from the SCROLLINFO structure.

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:

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. In a view class you should do the initialization in OnInitialUpdate. This is called after window creation but before the window becomes visible.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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