【发布时间】:2018-12-20 03:36:42
【问题描述】:
所以我试图在它旁边绘制我的光标位置,到目前为止它工作得很好。期望它有时会从以前的帧中留下“痕迹”,所以我想知道如何清除它们?有什么快速的技巧吗?
这是我的代码:
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;
}
【问题讨论】: