简体   繁体   中英

Scroll bar moves with client in MFC

I'm fairly new to MFC. I implemented a scroll bar to scroll the client area. When I scroll down the scroll bar also moves with the client. I want the scroll bar to stay where it is and just move the client.

I tried clipping the scroll bar and just moving the client but I can't get it to work. I used the ScrollWindow() to do so but I don't know how to clip the scroll bar. I don't know if I need the coordinates/dimensions of the scroll bar or what to get it to clip so for right now I have those parameters as NULL. I was focusing on just using the up and down buttons first to get the client to move correctly.

int UpPos;
    if ((pScrollBar == (CScrollBar *)&mMotorScrollBar)) {

        // Determine the new position of scroll box.
        int CurPos = mMotorScrollBar.GetScrollPos();

        switch (nSBCode)
        {
        case SB_TOP:      // Scroll to top.
            CurPos = 0;
            break;

        case SB_BOTTOM:    // Scroll to bottom.
            CurPos = 122;
            break;

        case SB_ENDSCROLL: // End scroll.
            break;

        case SB_LINEUP: // Scroll up when arrow up button is clicked.
            if (CurPos > 0)
                CurPos--;
                UpPos = -CurPos;
                ScrollWindow(0, UpPos, NULL, NULL);
            break;

        case SB_LINEDOWN:   // Scroll down when arrow down button is clicked.
            if (CurPos < 122)
                CurPos++;
                ScrollWindow(0, CurPos, NULL, NULL);
            break;

Application before I scroll

Application after I scroll.

Window Properties

As you can see the scroll bar also moved with the client.

Remove the scrollbar control and add vertical scroller using SetScrollInfo . SetScrollInfo has to be called when window is created, it will add a scroll bar and it adjusts the client area.

Additionally, you can edit dialog box and set the vertical scroll option. This option doesn't actually do anything except it adjusts the position of dialog controls in dialog editor. The actual scroll bar has to be created using SetScrollInfo during run time. Example:

BOOL CMyPropertyPage::OnInitDialog()
{
    BOOL res = CPropertyPage::OnInitDialog();
    SCROLLINFO si = { sizeof(si) };
    si.fMask = SIF_ALL;
    si.nMax = 122; //<- this should be calculated dynamically for DPI compatibility
    si.nPage = 1;
    SetScrollInfo(SB_VERT, &si, TRUE);
    return res;
}

void CMyPropertyPage::OnVScroll(UINT sbCode, UINT, CScrollBar*)
{
    SCROLLINFO si = { sizeof(si) };
    GetScrollInfo(SB_VERT, &si, SIF_ALL);
    int save = si.nPos;
    switch(sbCode)
    {
    case SB_LINEDOWN:      si.nPos += 1; break;
    case SB_LINEUP:        si.nPos -= 1; break;
    case SB_PAGEDOWN:      si.nPos += si.nPage; break;
    case SB_PAGEUP:        si.nPos -= si.nPage; break;
    case SB_TOP:           si.nPos = 0; break;
    case SB_BOTTOM:        si.nPos = si.nMax; break;
    case SB_THUMBTRACK:    si.nPos = si.nTrackPos; break;
    default:break;
    }
    if(si.nPos < 0) si.nPos = 0;
    if(si.nPos > si.nMax) si.nPos = si.nMax;
    SetScrollPos(SB_VERT, si.nPos); //update scrollbar position
    ScrollWindow(0, save - si.nPos); //scroll the window
}

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