Weird splitter issue

122 views Asked by At

I borrowed a bit of code for a splitter bar in between two edits, but I'm getting weirdness when I try to either write on the bottom one or resize them by moving the bar. Often, the bottom edit disappears completely and the scrollbar dances around.

Ultimately, my goal is to have the window divided up into 5 different edits for a look similar to this: http://www.codeproject.com/KB/tree/Win32TreeList/TreeList.gif

Am I going about it the right way?

Relevant code:

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;

//

Images showing what's happening:

https://i.stack.imgur.com/SBDjt.jpg

1

There are 1 answers

0
Barmak Shemirani On

A simple WNDCLASSEX wcx declaration will work sometimes, but sometimes it crashes the program.

You should always initialize structures with zero: WNDCLASSEX wcx = { 0 };

Another problem, you didn't handle WM_COMMAND properly, you have to return 0 or break when it is done:

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