c++ - GDIplus闪烁

标签 c++ winapi gdi+

我正在尝试在透明窗口上使用 GDIplus 进行绘图,但遇到了很多闪烁。我读过很多帖子,建议实现双缓冲或渲染到屏幕外表面会有所帮助,我已经这样做了,但无济于事。

知道我做错了什么以及如何解决吗? 谢谢。

#include <Windows.h>
#include <stdio.h>
#include <time.h>
#include <GdiPlus.h>

#pragma comment(lib, "GdiPlus.lib")

HWND hWnd = NULL;
WNDCLASSEX wcex;
HDC hdc = NULL;
PAINTSTRUCT ps;
HGDIOBJ hfDefault;
MSG msg;
COLORREF transKey = RGB(37,14,103);
ULONG_PTR m_gdiplusToken;

char window_title[9] = "testings";
char window_class[9] = "testings";

int window_width = GetSystemMetrics(SM_CXSCREEN);
int window_height = GetSystemMetrics(SM_CYSCREEN);

DWORD MainThread(LPVOID lpArgs)
{
    while(1)
    {
        RECT rc = {0,0,window_width,window_height};
        InvalidateRect(hWnd, &rc, false);
        UpdateWindow(hWnd);
        Sleep(50);
    }
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    if(msg == WM_CREATE)
    {
        hfDefault = (HFONT)GetStockObject(DEFAULT_GUI_FONT);

        Gdiplus::GdiplusStartupInput gdiplusStartupInput;
        Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);

        SetLayeredWindowAttributes(hWnd, transKey, 128, LWA_COLORKEY);
    }
    else if(msg == WM_PAINT)
    {
        hdc = BeginPaint(hWnd, &ps);

        Gdiplus::Graphics g(hdc);

        g.Clear(Gdiplus::Color(37,14,103));

        Gdiplus::Bitmap* curBitmap = new Gdiplus::Bitmap(window_width, window_height);
        Gdiplus::Graphics* g1 = g.FromImage(curBitmap);

        static int x=1,y=1;
        // x += 3;
        y += 2;

        Gdiplus::Pen pen(Gdiplus::Color(255, 255, 0, 255));

        g1->DrawLine(&pen, x, y, window_width/2, window_height/2);

        g.DrawImage(curBitmap, 0, 0);

        EndPaint(hWnd, &ps);
    }
    else if(msg == WM_CLOSE)
    {
        DestroyWindow(hWnd);
    }
    else if(msg == WM_DESTROY)
    {
        PostQuitMessage(0);
    }
    else
    {
        return DefWindowProc(hWnd, msg, wParam, lParam);
    }

    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    HBRUSH hbrBackground = CreateSolidBrush(transKey);

    wcex.cbSize        = sizeof(WNDCLASSEX);
    wcex.style         = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc   = WndProc;
    wcex.cbClsExtra    = 0;
    wcex.cbWndExtra    = 0;
    wcex.hInstance     = hInstance;
    wcex.hIcon         = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
    wcex.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground = hbrBackground;
    wcex.lpszMenuName  = NULL;
    wcex.lpszClassName = window_class;
    wcex.hIconSm       = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));

    if(!RegisterClassEx(&wcex))
        return 1;

    hWnd = CreateWindowEx(
        WS_EX_LAYERED | WS_EX_TRANSPARENT | WS_EX_TOPMOST,
        window_class,
        window_title,
        WS_POPUP,
        0,
        0,
        window_width,
        window_height,
        NULL,
        NULL,
        hInstance,
        NULL
    );

    if(!hWnd)
        return 1;

    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);

    CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)MainThread, NULL, NULL, NULL);

    while(GetMessage(&msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    Gdiplus::GdiplusShutdown(m_gdiplusToken);
    return msg.wParam;
}

最佳答案

您的窗口具有 WS_EX_TRANSPARENT 属性,这会导致其下方的窗口在每次您的窗口无效时重新绘制。尝试将其删除。

WS_EX_TRANSPARENT 的含义是告诉 Windows 您不会在整个窗口表面上绘制(即保留部分透明),因此它需要确保首先渲染下面的所有内容所以它可以显示出来。

关于c++ - GDIplus闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14162900/

相关文章:

c++ - 体系结构 x86_64 : error 的 undefined symbol

c - 绘制网格作为背景的更好方法

c++ - 想在同一背景上放置多个透明颜色的图像

windows - 用于判断给定二进制文件(EXE 或 DLL)是 x86、x64 还是 ia64 的 Win32 API

c - 如何使用 SetConsoleCursorPosition 函数

c# - 如何在 Windows 窗体中拖动绘制的形状

c# - 是什么导致 Calibri 在 9 到 14 pt 之间丢失 ClearType?

c++ - 在 C 代码中安全地嵌入字符串(Secure string、Secure char*)

c++ - pow 函数 C++ 错误

c++ - Linux 目录 : Getting a list of all folders inside a directory