I am trying to track an object's position with GDI in C++ and to draw that position over application's display by creating a rectangle. The object changes position slightly quite often and while the object is standing still or moving the rectangle I'm drawing is flickering.
Initialization:
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));
This function is called in a 5ms loop:
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);
}
Any idea what's going on? The rectangle in copied correctly from the backbuffer to frontbuffer, but the flickering is still there.
Thanks!