我试图用C ++中的GDI跟踪对象的位置,并通过创建一个矩形在应用程序的显示上绘制该位置。物体稍微经常改变位置,当物体静止不动或移动矩形时,我的画面闪烁。
初始化:
TargetWnd = FindWindow(nullptr, "app name");
frontbuffer_HDC = GetDC(TargetWnd);
backbuffer_HDC = CreateCompatibleDC(frontbuffer_HDC);
frontbuffer_bitmap = CreateCompatibleBitmap(frontbuffer_HDC, 1920, 1080);
SelectObject(backbuffer_HDC, frontbuffer_bitmap);
Brush = CreateSolidBrush(RGB(255, 0, 0));
此函数在5ms循环中调用:
DrawBorderBox(coords[0], coords[1], x, y, 1);
void DrawBorderBox(int x, int y, int w, int h, int thickness)
{
DrawFilledRect(x, y, w, thickness);
BitBlt(frontbuffer_HDC, x, y, w, thickness, backbuffer_HDC, x, y, SRCCOPY);
DrawFilledRect(x, y, thickness, h);
BitBlt(frontbuffer_HDC, x, y, thickness, h, backbuffer_HDC, x, y, SRCCOPY);
DrawFilledRect((x + w), y, thickness, h);
BitBlt(frontbuffer_HDC, x+w, y, thickness, h, backbuffer_HDC, x, y, SRCCOPY);
DrawFilledRect(x, y + h, w + thickness, thickness);
BitBlt(frontbuffer_HDC, x, y+h, w+thickness, thickness, backbuffer_HDC, x, y, SRCCOPY);
}
void DrawFilledRect(int x, int y, int w, int h)
{
RECT rect = { x, y, x + w, y + h };
FillRect(backbuffer_HDC, &rect, Brush);
}
知道发生了什么事吗?从后备缓冲区到前缓冲区正确复制了矩形,但闪烁仍在那里。
谢谢!