为什么windows api滚动条没有响应?

时间:2015-03-27 17:51:31

标签: c winapi scrollbar

我正在尝试在窗口中创建一个可滚动的child-window。子窗口应该有scrollbars;滚动条出现但完全没有响应。窗口创建代码:

// "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);

我为新WNDPROC设置了自定义child-window,但没有滚动消息到达。我在互联网上找到了很多例子,但它们都不起作用或者在子窗口中使用scrollbars

1 个答案:

答案 0 :(得分:1)

正如Hans Passant指出的那样,可滚动的子窗口需要自己的窗口类。

示例代码:

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;
}