简体   繁体   中英

Why does the windows api scrollbars not respond?

I'm trying to create a scrollable child-window within a window. The child-window is supposed to have scrollbars ; the scrollbars appear but are totally unresponsive. Window creation code:

// "mainwindow" is the handle of the main application window.
HWND wnd = CreateWindow(WC_STATIC, NULL, WS_CHILD | WS_VISIBLE | WS_VSCROLL,
    10, 100, 300, 300, mainwindow, NULL, GetModuleHandle(0), 0);

SCROLLINFO si = { 0 };
si.cbSize = sizeof(SCROLLINFO);
si.fMask = SIF_ALL;
si.nMax = 800;

SetScrollInfo(g_wnd, SB_VERT, &si, true);

I've set a custom WNDPROC for the new child-window , but no scrolling-messages arrive. I've found numerous examples on the internet, but none of them either work or are about using scrollbars in child-windows.

As Hans Passant pointed out, a scrollable child-window needs its own windows class.

Example code:

HWND create_scroll_window(HWND parent)
{
    WNDCLASSEX wcex = { 0 };

    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc = WndProc;
    wcex.hInstance = GetModuleHandle(0);
    wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wcex.lpszClassName = TEXT("MyScrollWinClass");

    if (!RegisterClassEx(&wcex)) return 0;

    HWND hWnd = CreateWindow(_T("MyScrollWinClass"), _T(""), WS_CHILD | WS_BORDER | WS_VISIBLE | WS_VSCROLL, 20, 20,
    300, 300, parent, NULL, wcex.hInstance, NULL);

    return hWnd;
}

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