Home > Mobile >  WinAPI - ShowWindow() fails to maximize the window
WinAPI - ShowWindow() fails to maximize the window

Time:11-24

My window doesn't use a caption bar hence I want the window to maximize when I double click. So I use ShowWindow(hwnd, SW_MAXIMIZE), however it doesn't seem to work. I even tried SendMessage(hwnd, WM_SYSCOMMAND, SC_MAXIMIZE, 0). On WM_NCLBUTTONDBLCLK it prints the statement in it, but doesn't execute the command as you can see in this video - https://imgur.com/a/r0oVD5W

Here is my minimum reproduced code:

#include <dwmapi.h>
#include <stdio.h>
#include <windows.h>

#define border_size 4
#define w_width 1000
#define w_height 500
#define caption_size 30
#define corner_rounding 7

char* w_name = "test title";
char* app_name = "test";

HWND hwnd;

ATOM MyRegisterClass(HINSTANCE hInst, char* name, UINT styles, COLORREF bkg_colour, WNDPROC proc)
{
    WNDCLASSEX wc;
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = styles;
    wc.lpfnWndProc = proc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInst;
    wc.hIcon = LoadIconA(hInst, IDI_APPLICATION);
    wc.hCursor = LoadCursorA(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)CreateSolidBrush(bkg_colour);
    wc.lpszMenuName = NULL;
    wc.hIconSm = LoadIconA(hInst, IDI_APPLICATION);
    wc.lpszClassName = name;

    return RegisterClassExA(&wc);
}

LRESULT CALLBACK Caption(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) // returns HTTRANSPARENT
{
    switch (msg)
    {
    case WM_NCHITTEST:;
        return HTTRANSPARENT;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    }
    return DefWindowProcA(hwnd, msg, wp, lp);
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
{
    PAINTSTRUCT ps;
    HDC hdc;
    switch (msg)
    {
    case WM_NCLBUTTONDBLCLK:
        printf("works\n");
        ShowWindow(hwnd, SW_MAXIMIZE);
        break;
    case WM_NCHITTEST:;
        return HTCAPTION;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    }
    return DefWindowProcA(hwnd, msg, wp, lp);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    MSG msg;

    MyRegisterClass(hInstance, app_name, CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS, RGB(255, 0, 0), WndProc);
    MyRegisterClass(hInstance, "caption_wind", CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS, RGB(255, 255, 0), Caption);

    hwnd = CreateWindowExA(WS_EX_LAYERED | WS_EX_NOINHERITLAYOUT, app_name, "main", WS_POPUP, 0, 0, w_width   2 * border_size   corner_rounding / 2, w_height   2 * border_size   corner_rounding / 2, NULL, NULL, hInstance, NULL);
    HWND hcaption = CreateWindowExA(0, "caption_wind", NULL, WS_CHILD | WS_VISIBLE, border_size, border_size, w_width, w_height, hwnd, NULL, hInstance, NULL);

    HRGN mrgn = CreateRoundRectRgn(border_size, border_size, w_width, w_height, corner_rounding, corner_rounding);
    HRGN trgn = CreateRectRgn(0, caption_size   border_size, w_width, w_height);
    CombineRgn(mrgn, mrgn, trgn, RGN_DIFF);
    DeleteObject(trgn);
    SetWindowRgn(hcaption, mrgn, 1);
    SetLayeredWindowAttributes(hwnd, 0, 255, LWA_ALPHA);

    ShowWindow(hwnd, SW_MAXIMIZE);
    // ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    while (GetMessageA(&msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&msg);
        DispatchMessageA(&msg);
    }
    return msg.wParam;
}

Any help will be greatly appreciated.

CodePudding user response:

I agree with ElderBug, you should try to use ShowWindowAsync function instead of ShowWindow function. As follow:

case WM_NCLBUTTONDBLCLK:
    printf("works\n");

    ShowWindowAsync(hwnd, SW_MAXIMIZE);
    break;

The first time an application calls ShowWindow, it should use the WinMain function's nCmdShow parameter as its nCmdShow parameter. I suggest you could try to use nCmdShow instead of SW_MAXIMIZE.

  • Related