奇怪的分裂器问题

时间:2015-06-09 23:19:02

标签: winapi visual-c++ splitter

我在两次编辑之间为一个分隔条借了一些代码,但是当我尝试在底部编写或通过移动条形图来调整它时,我会变得很奇怪。通常情况下,底部编辑完全消失,滚动条会跳舞。

最终,我的目标是将窗口划分为5种不同的编辑,以实现与此类似的外观:http://www.codeproject.com/KB/tree/Win32TreeList/TreeList.gif

我是以正确的方式去做的吗?

相关代码:

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    HINSTANCE   hInst;
    RECT        rect;
    static HCURSOR  hCursor;
    static BOOL bSplitterMoving;
    static DWORD    dwSplitterPos;
    static HWND hWnd1, hWnd2;

    switch (uMsg)
    {
        case WM_CREATE:
        {
            hInst = ((LPCREATESTRUCT)lParam)->hInstance;

            hWnd1 = CreateWindowEx(WS_EX_CLIENTEDGE,
                L"edit", NULL,
                WS_CHILD | WS_VISIBLE | ES_MULTILINE | WS_VSCROLL,
                0, 0, 0, 0,
                hWnd, (HMENU)1,
                hInst, NULL);

            hWnd2 = CreateWindowEx(WS_EX_CLIENTEDGE,
                L"edit", NULL,
                WS_CHILD | WS_VISIBLE | ES_MULTILINE | WS_VSCROLL,
                0, 0, 0, 0,
                hWnd, (HMENU)2,
                hInst, NULL);

            hCursor = LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZENS));
            bSplitterMoving = FALSE;

            dwSplitterPos = 130;

//

    case WM_SIZE:
        if ((wParam != SIZE_MINIMIZED) && (HIWORD(lParam) < dwSplitterPos))
            dwSplitterPos = HIWORD(lParam) - 10;

        /* Adjust the children's size and position */
        MoveWindow(hWnd1, 0, 0, LOWORD(lParam), dwSplitterPos - 1, TRUE);
        MoveWindow(hWnd2, 0, dwSplitterPos + 2, LOWORD(lParam), HIWORD(lParam) - dwSplitterPos - 2, TRUE);
        return 0;

    case WM_MOUSEMOVE:
        if (HIWORD(lParam) > 10) // do not allow above this mark
        {
            SetCursor(hCursor);
            if ((wParam == MK_LBUTTON) && bSplitterMoving)
            {
                GetClientRect(hWnd, &rect);
                if (HIWORD(lParam) > rect.bottom)
                    return 0;

                dwSplitterPos = HIWORD(lParam);
                SendMessage(hWnd, WM_SIZE, 0, MAKELPARAM(rect.right, rect.bottom));
            }
        }
        return 0;

    case WM_LBUTTONDOWN:
        SetCursor(hCursor);
        bSplitterMoving = TRUE;
        SetCapture(hWnd);
        return 0;


    case WM_LBUTTONUP:
        ReleaseCapture();
        bSplitterMoving = FALSE;
        return 0;

//

显示正在发生的事情的图片:

http://imgur.com/a/OcdSx

1 个答案:

答案 0 :(得分:0)

简单的WNDCLASSEX wcx声明有时会起作用,但有时它会使程序崩溃。

您应始终使用零初始化结构: WNDCLASSEX wcx = { 0 };

另一个问题是,你没有正确处理WM_COMMAND,你必须在完成后返回0或中​​断:

case WM_COMMAND:
{
    switch (LOWORD(wParam))
    {
    case IDM_EXIT:
        PostQuitMessage(0);
        break;
    case IDM_ABOUT:
        break;
    default:
        break;
    }
    return 0;//** THIS WAS MISSING
}