简体   繁体   中英

how to make a scrollable window in vc++ win32

basically I have a scrollbar in a custom window where we could add other controls like buttons, textboxes and so and so forth, now here is the problem I have created the scrollbar alright it scrolls down and up, but the problem is that the maximum scroll should be specified which then blocks it to scroll more than that, but there could be as much control as the user want, which for me it would not be known, I mean like to tell the problem to be more like a textbox's built-in scroll bar when you specify it with WM_VSCROLL OR HSCROLL, and then give the user the ability to scroll as much as you type on the textbox, that's exactly what I want. here is the code so far I have been able to go through:-

Code for Scrollbar:-

case WM_LBUTTONDOWN:
    {
            SCROLLINFO si = { 0 };
            si.cbSize = sizeof(SCROLLINFO);
            si.fMask = SIF_POS;
            si.nPos = 0;
            si.nTrackPos = 0;
            GetScrollInfo(hwnd, SB_VERT, &si);
            break;
    }
     case WM_VSCROLL:
    {
            auto action = LOWORD(wParam);
            HWND hScroll = (HWND)lParam;
            int pos = -1;
            if (action == SB_THUMBPOSITION || action == SB_THUMBTRACK) {
                    pos = HIWORD(wParam);
            } else if (action == SB_LINEDOWN) {
                    pos = g_scrollY + 50;
            } else if (action == SB_LINEUP) {
                    pos = g_scrollY - 50;
            } else if (action == SB_PAGEUP) {
                    GetClientRect(hwnd, &r);
                    pos = g_scrollY - r.bottom;
            } else if (action == SB_PAGEDOWN){
                    GetClientRect(hwnd, &r);
                    pos = g_scrollY + r.bottom;
            }
            if (pos == -1)
                    break;
            SCROLLINFO si = { 0 };
            si.cbSize = sizeof(SCROLLINFO);
            si.fMask = SIF_POS;
            si.nPos = pos;
            si.nTrackPos = 0;
            SetScrollInfo(hwnd, SB_VERT, &si, true);

            GetScrollInfo(hwnd, SB_VERT, &si);
            pos = si.nPos;
            POINT pt;
            pt.x = 0;
            pt.y = pos - g_scrollY;
            auto hdc = GetDC(hwnd);
            LPtoDP(hdc, &pt, 1);
            ReleaseDC(hwnd, hdc);
            ScrollWindow(hwnd, 0, -pt.y, NULL, NULL);
            g_scrollY = pos;
            return 0;
    }
case WM_SIZE:
    {
            RECT rc = { 0 };
            GetClientRect(hwnd, &rc);
            SCROLLINFO si = { 0 };
            si.cbSize = sizeof(SCROLLINFO);
            si.fMask = SIF_ALL;
            si.nMin = 0;
            si.nMax = MAX_RANGE;
            if(si.nPos > 100)
                si.nMax = 5000;
            if(SB_THUMBPOSITION == 100){

                si.nMax = MAX_RANGE;}
            si.nPage = (rc.bottom - rc.top);
            si.nPos = 0;
            si.nTrackPos = 0;
            SetScrollInfo(hwnd, SB_VERT, &si, true);
            break;
    }

as you can see si.nMax value is the maximum amount of lines it is going to be able to scroll, but I want it to be as much as there are controls.

您必须知道窗口中有多少个控件,因此可以在SCROLLINFO结构中将nMax成员设置为所需的任何值,并在添加新控件时调用SetScrollInfo()。

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