Behavior of Layered Window Size Changes

Asked 2 years ago, Updated 2 years ago, 81 views

I'm studying win32 api.You are trying to create a window with a variable transparent size.

Transparency was achieved by specifying WS_EX_LAYERED as the style in the CreateWindowEx function, but if you change the size with the mouse, you will not be able to operate the window edge with the mouse.What should I do if I do not specify WS_EX_LAYERED?

#include<windows.h>

LRESULT CALLBACK WndProc (HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) {
    switch(msg){
    caseWM_CREATE:
        SetLayedWindowAttributes(hwnd, RGB (255, 255, 255), 0, LWA_COLORKEY);
        return 0;
    caseWM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProc(hwnd, msg, wp, lp);
}

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR lpCmdLine, intnCmdShow) {
    HWND hwnd;
    MSG msg;
    WNDCLASS winc;

    winc.style=CS_HREDRAW | CS_VREDRAW;
    winc.lpfnWndProc=WndProc;
    winc.cbClsExtra=winc.cbWndExtra=0;
    winc.hInstance=hInstance;
    winc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
    winc.hCursor=LoadCursor(NULL,IDC_ARROW);
    winc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
    winc.lpszMenuName = NULL;
    winc.lpszClassName=TEXT("Test");

    if(!RegisterClass(&winc)) return-1;

    hwnd = CreateWindowEx(
# if 1
        WS_EX_LAYERED | WS_EX_TOOLWINDOW,
# else
        WS_EX_TOOLWINDOW,
#endif
        TEXT ("Test"), TEXT ("Test"),
        WS_OVERLAPPEDWINDOW | WS_VISIBLE,
        CW_USEDEFAULT, CW_USEDEFAULT,
        300, 200,
        NULL, NULL,
        hInstance, NULL
        );
    if(hwnd==NULL)return-1;


    while(GetMessage(&msg,NULL,0,0)){
        TranslateMessage (&msg);
        DispatchMessage (&msg);
    }
    return msg.wParam;
}

c++ winapi

2022-09-30 11:19

2 Answers

To the MSDN's SetLayedWindowAttributes reference

If you call the SetLayeredWindowAttributes function with a specific layered window, the window fails to call the UpdateLayeredWindow function until you clear and reset the multilayered style bit.

There is a description that says, but isn't it suspicious around here?


2022-09-30 11:19

The reason for the inactivity seems to be that the background color of the window is the same as the color key specified in SetLayeredWindowAttributes.

But it probably worked out as expected.

#include<windows.h>

LRESULT CALLBACK WndProc (HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) {
    switch(msg){
    caseWM_CREATE:
        SetLayedWindowAttributes(hwnd, RGB (255, 255, 255), 0, LWA_COLORKEY);
        return 0;
    caseWM_PAINT:
        {
            PAINTSTRUCTps;
            BeginPaint (hwnd, & ps);
            auto hbr = CreateSolidBrush (RGB (255, 255, 255));
            FillRect(ps.hdc, & ps.rcPaint, hbr);
            DeleteObject(hbr);
            EndPaint (hwnd, & ps);
            return 0;
        }
    caseWM_NCHITTEST:
    {
        autol=DefWindowProc(hwnd,msg,wp,lp);
        OutputDebugStringA(std::to_string(l).c_str());
        returnl;
    }
    caseWM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProc(hwnd, msg, wp, lp);
}

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR lpCmdLine, intnCmdShow) {
{
    HWND hwnd;
    MSG msg;
    WNDCLASS winc;

    winc.style=CS_HREDRAW | CS_VREDRAW;
    winc.lpfnWndProc=WndProc;
    winc.cbClsExtra=winc.cbWndExtra=0;
    winc.hInstance=hInstance;
    winc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
    winc.hCursor=LoadCursor(NULL,IDC_ARROW);
    winc.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
    winc.lpszMenuName = NULL;
    winc.lpszClassName=TEXT("Test");

    if(!RegisterClass(&winc)) return-1;

    hwnd = CreateWindowEx(
# if 1
        WS_EX_LAYERED | WS_EX_TOOLWINDOW,
# else
        WS_EX_TOOLWINDOW,
#endif
        TEXT ("Test"), TEXT ("Test"),
        WS_OVERLAPPEDWINDOW | WS_VISIBLE,
        CW_USEDEFAULT, CW_USEDEFAULT,
        300, 200,
        NULL, NULL,
        hInstance, NULL
    );
    if(hwnd==NULL)return-1;


    while(GetMessage(&msg,NULL,0,0)){
        TranslateMessage (&msg);
        DispatchMessage (&msg);
    }
    return msg.wParam;
}


2022-09-30 11:19

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.