精易论坛

标题: 谁会gdiplus画不会闪烁的图形 [打印本页]

作者: ra2413    时间: 2025-4-9 16:59
标题: 谁会gdiplus画不会闪烁的图形
谁会gdiplus画不会闪烁,不会清除的图形不能用模块直接用gdiplus的函数,画个按钮,线,狐,图像,圆环,文本

作者: 呵呵仙    时间: 2025-4-9 17:55
把别人,类模块里的方法,复制出来作全局函数,就是你要的不能用模块了!
作者: ra2413    时间: 2025-4-9 18:09
呵呵仙 发表于 2025-4-9 17:55
把别人,类模块里的方法,复制出来作全局函数,就是你要的不能用模块了!

你试试看行不行。我试过了

作者: q278330360    时间: 2025-4-9 20:14
双缓冲绘制
作者: 禁回忆    时间: 2025-4-9 21:28
[C++] 纯文本查看 复制代码
#include <Windows.h>
#include <gdiplus.h>
#pragma comment(lib, "gdiplus.lib")

using namespace Gdiplus;

// 全局变量
HWND g_hWnd;
ULONG_PTR g_gdiplusToken;
Bitmap* g_pOffscreenBitmap = nullptr;
Graphics* g_pOffscreenGraphics = nullptr;

// 按钮结构
struct Button {
    Rect rect;
    bool hovered;
    bool clicked;
    WCHAR text[32];
};


// 初始化GDI+
void InitGDIPlus()
{
    GdiplusStartupInput input;
    GdiplusStartup(&g_gdiplusToken, &input, nullptr);
}

// 创建离屏位图
void CreateOffscreenBitmap(int width, int height)
{
    if (g_pOffscreenGraphics) delete g_pOffscreenGraphics;
    if (g_pOffscreenBitmap) delete g_pOffscreenBitmap;

    g_pOffscreenBitmap = new Bitmap(width, height);
    g_pOffscreenGraphics = Graphics::FromImage(g_pOffscreenBitmap);
    g_pOffscreenGraphics->SetSmoothingMode(SmoothingModeAntiAlias);
}

// 绘制图形到离屏位图
void DrawToOffscreen()
{
    if (!g_pOffscreenGraphics) return;


     g_pOffscreenGraphics->Clear(Color(255, 255, 255));


    // 绘制其他图形
    Pen redPen(Color(255, 0, 0), 3);
    Pen bluePen(Color(0, 0, 255), 2);

    // 直线
    g_pOffscreenGraphics->DrawLine(&redPen, 200, 50, 300, 100);

    // 圆弧(椭圆的一部分)
    g_pOffscreenGraphics->DrawArc(&bluePen, 200, 100, 100, 50, 45, 180);

    // 圆环(空心圆)
    g_pOffscreenGraphics->DrawEllipse(&redPen, 350, 50, 80, 80);

    // 文本
    SolidBrush brush(Color(0, 128, 0));
    Font textFont(L"宋体", 16);
    g_pOffscreenGraphics->DrawString(L"持久化绘图示例", -1, &textFont, PointF(200, 200), &brush);
}

// 窗口过程
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg)
    {
    case WM_CREATE:
        InitGDIPlus();
        break;

    case WM_SIZE:
    {
        int width = LOWORD(lParam);
        int height = HIWORD(lParam);
        CreateOffscreenBitmap(width, height);
        DrawToOffscreen();
        InvalidateRect(hWnd, nullptr, FALSE);
        break;
    }

    case WM_PAINT:
    {
        PAINTSTRUCT ps;
        HDC hdc = BeginPaint(hWnd, &ps);

        if (g_pOffscreenBitmap)
        {
            Graphics graphics(hdc);
            graphics.DrawImage(g_pOffscreenBitmap, 0, 0);
        }

        EndPaint(hWnd, &ps);
        break;
    }

    case WM_ERASEBKGND:
        return 1; // 阻止背景擦除

    case WM_DESTROY:
        if (g_pOffscreenGraphics) delete g_pOffscreenGraphics;
        if (g_pOffscreenBitmap) delete g_pOffscreenBitmap;
        GdiplusShutdown(g_gdiplusToken);
        PostQuitMessage(0);
        break;

    default:
        return DefWindowProc(hWnd, msg, wParam, lParam);
    }
    return 0;
}

// 注册窗口类
void RegisterWindowClass(HINSTANCE hInstance)
{
    WNDCLASSEXW wcex = { 0 };
    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc = WndProc;
    wcex.hInstance = hInstance;
    wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
    wcex.lpszClassName = L"GDIPlusWindow";
    RegisterClassExW(&wcex);
}

// 创建主窗口
HWND CreateMainWindow(HINSTANCE hInstance)
{
    return CreateWindowW(L"GDIPlusWindow", L"GDI+ 持久化绘图示例",
        WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, 800, 600, nullptr,
        nullptr, hInstance, nullptr);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int nCmdShow)
{
    RegisterWindowClass(hInstance);
    g_hWnd = CreateMainWindow(hInstance);
    ShowWindow(g_hWnd, nCmdShow);
    UpdateWindow(g_hWnd);

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


作者: ra2413    时间: 2025-4-9 21:54
禁回忆 发表于 2025-4-9 21:28
[mw_shl_code=cpp,true]#include
#include
#pragma comment(lib, "gdiplus.lib")

易语言代码呀
作者: 禁回忆    时间: 2025-4-9 22:02
ra2413 发表于 2025-4-9 21:54
易语言代码呀

用易语言不想用模块,不想麻烦的话就参考https://125.confly.eu.org/forum.php?mod ... p;highlight=gdiplus
作者: ra2413    时间: 2025-4-9 22:07
禁回忆 发表于 2025-4-9 22:02
用易语言不想用模块,不想麻烦的话就参考https://125.confly.eu.org/forum.php?mod=viewthread&tid=13962026&hig ...

我研究研究




欢迎光临 精易论坛 (https://125.confly.eu.org/) Powered by Discuz! X3.4