Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;GDI+;清除上一屏幕_C++_Frame_Gdi+_Flush - Fatal编程技术网

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

C++ C++;GDI+;清除上一屏幕,c++,frame,gdi+,flush,C++,Frame,Gdi+,Flush,所以我试着把光标的位置画在它旁边,到目前为止它工作得很好。预计它有时会留下前几帧的“痕迹”,所以我想知道如何清除它们?有没有快速的技巧可以做到这一点 下面是一个快速GIF: 这是我的密码: void DrawCursorCoords(Gdiplus::Graphics &graphics, Gdiplus::Font &font, Gdiplus::Brush &brush) { POINT cursorPos; GetCursorPos(&cur

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

下面是一个快速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控制台有自己的窗口和自己的绘制例程,无法访问。控制台提供了许多功能,例如允许在不同位置打印文本

例如:

#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;
}
#包括
#包括
int main()
{
HANDLE hout=GetStdHandle(标准输出句柄);
while(true)
{
COORD COORD={0,0};
设置控制台位置(hout,coord);
标准::cout