System buttons show when I am handling `WM_NCHITTEST` message and return `HTREDUCE` `HTZOOM` `HTCLOSE`

182 Views Asked by At

I am trying to create a custom title bar on win32 application. I handled WM_NCCLACSIZE and removed system title bar

    HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);

    case WM_NCCALCSIZE:
    {
        // get default NC size
        auto params = reinterpret_cast<NCCALCSIZE_PARAMS*>(lParam);
        const auto originalTop = params->rgrc[0].top;
        const auto originalSize = params->rgrc[0];
        const auto ret = DefWindowProc(hWnd, WM_NCCALCSIZE, wParam, lParam);
        auto newSize = params->rgrc[0];
        // remove title bar
        newSize.top = originalTop;
        if (IsZoomed(hWnd)) {
            newSize.top += 8;
            newSize.bottom -= 2;
        }
        params->rgrc[0] = newSize;
        return 0;
    }

Then I draw title bar and min, max, close button in my client area. I read the document and I would like to show layout menu like native window.

enter image description here

So I handled WM_NCHITTEST and returned HTREDUCE HTZOOM HTCLOSE instead of handling click event and ShowWindow(hwnd, SW_SHOWMAXIMIZED). But when I click the area, there is ugly system buttons in my title bar.

    case WM_NCHITTEST:
    {
        auto x = GET_X_LPARAM(lParam);
        auto y = GET_Y_LPARAM(lParam);
        RECT rect;
        GetWindowRect(hWnd, &rect);
        if (y - rect.top < 40) {

            auto width = rect.right - rect.left;
            RECT minbtn = { width - 3 * 120 ,0, width - 2 * 120 ,40 };
            RECT maxbtn = { width - 2 * 120 ,0, width - 1 * 120 ,40 };
            RECT closebtn = { width - 1 * 120 ,0, width - 0 * 120 ,40 };

            if (minbtn.left + rect.left < x && minbtn.right + rect.left > x)
                return HTREDUCE;
            if (maxbtn.left + rect.left < x && maxbtn.right + rect.left > x)
                return HTZOOM;
            if (closebtn.left + rect.left < x && closebtn.right + rect.left > x)
                return HTCLOSE;
            return HTCAPTION;
        }
        return DefWindowProc(hWnd, WM_NCHITTEST, wParam, lParam);;
    }

enter image description here

What should I do?

0

There are 0 best solutions below