具有不透明背景的 C++ Windows 窗口没有显示

问题描述 投票:0回答:0

我试图创建一个背景真正不透明的窗口(只有背景不透明,里面没有任何东西)。

This is what i expecting to be

但是在我的项目中,当我启动程序时,没有任何显示!!!有什么问题吗?

我在

WM_PAINT
中尝试创建兼容的 DC 和位图,我尝试用绿色画笔绘制它然后我尝试使用
UpdateLayeredWindow
但甚至没有绿色屏幕显示!

#include <iostream>
#include <windows.h>
#include <wingdi.h>
#include <gdiplus.h>

LRESULT CALLBACK
winproc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);

INT32 WINAPI
WinMain(HINSTANCE hInstance,
        HINSTANCE hPrevInstance,
        LPSTR lpCmdLine,
        INT32 nCmdShow) {
    ULONG_PTR token;
    static const Gdiplus::GdiplusStartupInput input;
    Gdiplus::GdiplusStartup(&token, &input, nullptr);

    static WNDCLASSEXW window_class;
    window_class.cbSize = sizeof(WNDCLASSEXW);
    window_class.style = CS_HREDRAW|CS_VREDRAW;
    window_class.hInstance = hInstance;
    window_class.hCursor = LoadCursor(nullptr, IDC_ARROW);
    window_class.cbWndExtra = 0;
    window_class.cbClsExtra = 0;
    window_class.lpszMenuName = nullptr;
    window_class.hIconSm = nullptr;
    window_class.hIcon = nullptr;
    window_class.lpszClassName = L"MyClass";
    window_class.lpfnWndProc = winproc;
    window_class.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
    if(!RegisterClassExW(&window_class)) {
        printf("RegisterClassExW() failed.");
        return 0;
    }
    HWND window_handle = CreateWindowExW(
            WS_EX_LAYERED,
            L"MyClass",
            L"Hello",
            WS_POPUP,
            (GetSystemMetrics(SM_CXSCREEN) - 700) / 2,
            (GetSystemMetrics(SM_CYSCREEN) - 600) / 2,
            700,
            600,
            nullptr,
            nullptr,
            hInstance,
            nullptr);
    if(window_handle==nullptr) {
        printf("CreateWindowExW() failed.");
        return 0;
    }
    ShowWindow(window_handle, SW_SHOWNORMAL);
    MSG msg;
    while (GetMessage(&msg, nullptr, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return 0;
}

LRESULT CALLBACK
winproc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
    switch (msg) {
        case WM_CREATE:
            SetLayeredWindowAttributes(hwnd, 0, 128, LWA_COLORKEY);
            break;
        case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc, hdc_m;
            HBITMAP bmp_m, bmp_old;
            RECT *rect;
            SIZE size;
            hdc=BeginPaint(hwnd, &ps);
            rect=&ps.rcPaint;
            size={(rect->right-rect->left), (rect->bottom-rect->top)};
            hdc_m=CreateCompatibleDC(ps.hdc);
            bmp_m=CreateCompatibleBitmap(ps.hdc, size.cx, size.cy);
            bmp_old=(HBITMAP)SelectObject(hdc_m, bmp_m);
            FillRect(hdc_m, rect, CreateSolidBrush(0x00FF00));
            BLENDFUNCTION blend = {0};
            blend.BlendOp = AC_SRC_OVER;
            blend.SourceConstantAlpha = 128;
            blend.AlphaFormat = AC_SRC_ALPHA;
            POINT ptPos = {0, 0};
            POINT ptSrc = {0, 0};
            UpdateLayeredWindow(hwnd, hdc, &ptPos, &size, hdc_m, &ptSrc, 0, &blend, ULW_ALPHA);
            SelectObject(hdc, bmp_old);
            DeleteObject(bmp_m);
            DeleteDC(hdc_m);
            EndPaint(hwnd, &ps);
        }
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProcW(hwnd, msg, wParam, lParam);
    }
    return 0;
}

我尝试将

WS_EX_LAYERED
SetLayeredWindowAttributes
UpdateLayeredWindow
一起使用 但没有用。

c++ c gdi+ gdi
© www.soinside.com 2019 - 2024. All rights reserved.