Fluent Blur Window C Example

警告:不推荐使用,建议等 WinUI 3

本文参考 walterlv 的一篇博客

示例效果

// gcc -m32 -mwindows a.c
#include <windows.h>
#define new(x) (x*)malloc(sizeof(x))
#define struct(x) typedef struct x x; struct x

struct(AccentPolicy) {
    int state, flags, color, anime;
};

struct(WindowCompositionAttributeData) {
    int attr;
    AccentPolicy *data;
    int size;
};

void EnableBlur(HWND h) {
    AccentPolicy a = {
        .state = 3,
        .color = 0xA0E6D8AD
    };
    WindowCompositionAttributeData d = {
        .attr = 19,
        .data = &a,
        .size = sizeof(AccentPolicy)
    };
    HINSTANCE hModule = LoadLibrary("user32.dll");
    typedef BOOL(WINAPI*pf)(HWND, WindowCompositionAttributeData*);
    pf SetWindowCompositionAttribute = (pf)GetProcAddress(hModule, "SetWindowCompositionAttribute");
    SetWindowCompositionAttribute(h, &d);
    FreeLibrary(hModule);
}

LRESULT CALLBACK WndProc(HWND h, UINT m, WPARAM w, LPARAM l) {
    switch (m) {
        case WM_CREATE:
            EnableBlur(h);
            break;
        case WM_CLOSE:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(h, m, w, l);
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE h, HINSTANCE p, LPSTR cmd, int n) {
    MSG msg = {};
    WNDCLASS wc = {
        .lpfnWndProc = WndProc,
        .hInstance = h,
        .hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH),
        .lpszClassName = "myapp"
    };
    if (!RegisterClass(&wc)) return 1;
    if (!CreateWindow(
        wc.lpszClassName,
        "Minimal Window",
        WS_OVERLAPPEDWINDOW|WS_VISIBLE,
        0,0,640,480,0,0,h,0
    )) return 2;
    while (GetMessage(&msg, 0, 0, 0) > 0)
        DispatchMessage(&msg);
    return 0;
}

© 2019 hyrious