C++ GDI+ 清除上一个屏幕

标签 c++ frame gdi+ flush

所以我试图在它旁边绘制光标的位置,到目前为止它工作得很好。预计它有时会在前一帧中留下“痕迹”,所以我想知道如何清除它们?有没有快速的技巧可以做到这一点?

这是一个快速 GIF:https://i.imgur.com/uVrzi0m.gif

这是我的代码:

void DrawCursorCoords(Gdiplus::Graphics &graphics, Gdiplus::Font &font, Gdiplus::Brush &brush)
{
    POINT cursorPos;
    GetCursorPos(&cursorPos);

    std::wstring x = std::to_wstring(cursorPos.x);
    std::wstring y = std::to_wstring(cursorPos.y);

    graphics.DrawString(x.c_str(), x.length(), &font, Gdiplus::PointF(cursorPos.x, cursorPos.y), &brush);
    graphics.DrawString(y.c_str(), y.length(), &font, Gdiplus::PointF(cursorPos.x, cursorPos.y + 50), &brush);
}


int main()
{
    // Start GDI+
    Gdiplus::GdiplusStartupInput gdiplusStartupInput;
    ULONG_PTR gdiplusToken;
    Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

    // Initialize graphics, brushes etc...
    HWND hWnd = GetDesktopWindow();
    HDC hdc = GetDC(hWnd);
    Gdiplus::Graphics * gfx = new Gdiplus::Graphics(hdc);
    Gdiplus::Pen * pen = new Gdiplus::Pen(Gdiplus::Color(255, 0, 255));
    Gdiplus::Font * cursorFont = new Gdiplus::Font(L"Consolas", 16);
    Gdiplus::SolidBrush * cursorBrush = new Gdiplus::SolidBrush(Gdiplus::Color(255, 0, 0, 150));
    Gdiplus::SolidBrush * clearBrush = new Gdiplus::SolidBrush(Gdiplus::Color::Transparent);

    while (!GetAsyncKeyState(VK_INSERT))
    {
        printf("Drawing!\n");

        DrawCursorCoords(*gfx, *cursorFont, *cursorBrush);

        // 1. Super slow + just fills the screen black
        //gfx->Clear(Gdiplus::Color::Transparent);

        // 2. Doesn't "flush" anything?
        //gfx->Flush();

        // 3. Super slow + does nothing
        //gfx->FillRectangle(clearBrush, Gdiplus::Rect(0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN)));
    }

    printf("Stopped drawing!\n");

    delete gfx;
    delete pen;
    delete cursorFont;
    delete cursorBrush;
    delete clearBrush;
    ReleaseDC(hWnd, hdc);
    Gdiplus::GdiplusShutdown(gdiplusToken);

    return 0;
}

最佳答案

你不应该在桌面上画画。您应该只在自己的窗口中绘制。如果您在桌面或其他任何人的窗口上绘制,它将在该窗口的下一个绘制周期中被清除。这是无法控制的。

您也无法在控制台上绘图。 Windows 控制台有自己的窗口和自己的绘制例程,但无法访问。控制台确实提供了许多功能,例如 SetConsoleCursorPosition允许在不同位置打印文本。

示例:

#include <Windows.h>
#include <iostream>

int main()
{
    HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
    while(true)
    {
        COORD coord = { 0, 0 };
        SetConsoleCursorPosition(hout, coord);
        std::cout << "Move the mouse and click a key\n";
        system("pause");

        POINT pt;
        GetCursorPos(&pt);
        HWND hwnd = GetConsoleWindow();

        RECT rc;
        GetClientRect(hwnd, &rc);
        ScreenToClient(hwnd, &pt);

        CONSOLE_SCREEN_BUFFER_INFO inf;
        GetConsoleScreenBufferInfo(hout, &inf);

        coord.X = (SHORT)MulDiv(pt.x, inf.srWindow.Right, rc.right);
        coord.Y = (SHORT)MulDiv(pt.y, inf.srWindow.Bottom, rc.bottom);

        SetConsoleCursorPosition(hout, coord);
        std::cout << "X";
    }
    return 0;
}

控制台的绘图选项有限。对于实际的绘制,您必须使用CreateWindow创建一个窗口,以及一个窗口消息循环来响应WM_PAINT进行绘制,以及处理其他窗口消息。

参见Walkthrough: Creating Windows Desktop Applications有关 Windows 编程的介绍,或参阅 Windows 编程的入门书籍。

关于C++ GDI+ 清除上一个屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51296587/

相关文章:

java - 关闭 Swing Frame,用户与系统启动关闭。如何区分?

c++ - GDI+ 字体渲染,尤其是在分层窗口中

vb.net - 如何绘制圆形渐变?

c++ - lua 脚本和应用程序的不同路径

c++ - 将指针与引用进行比较

C++ - 可以跟踪结构(x 和 y)和值以避免重复的数据结构/算法

java单框架应用程序

ios - 修改单元格 subview 的框架

gdi+ - 应用程序验证程序停止 0202 : Freeing heap block containing an active critical section during GdiPlusShutdown

python - 在 C++ 中嵌入 Python : Failing to import from the same directory