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

时间:2018-07-12 02:41:16

标签: c++ frame gdi+ clear 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;
}

1 个答案:

答案 0 :(得分:2)

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

您也不能在控制台上绘制。 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进行绘画,并处理其他窗口消息。

有关Windows编程的介绍,请参见Walkthrough: Creating Windows Desktop Applications,或有关Windows编程的介绍性书。