GDI双缓冲区黑色闪烁

GDI Double Buffer Black Flicker

本文关键字:闪烁 黑色 双缓冲区 GDI      更新时间:2023-10-16

我正在使用这个BitBlt包装器:

http://www.codeproject.com/Articles/21426/Double-Buffering-in-a-Win-API-Program

我在main()中初始化它:

biop = new Bitmap_Operations();
biop->Initialize_Buffers(m_hDC, m_hWND, 1);
biop->Create_Buffer(0);

助手功能:

void CreateBuffer()
{
    biop->Create_Buffer(0);
}
void Render()
{
    biop->Copy_to_Screen(0);
}
void ClearBuffer()
{
    biop->Free_Buffer(0);
}
void DrawBox(int x, int y, int r, int g, int b, int size, int thickness)
{
    // Brush style to hollow
    m_LogBrush.lbStyle = BS_NULL;
    // Create a logical brush and select into the context
    m_hBrush = CreateBrushIndirect(&m_LogBrush);
    HBRUSH hbrOldBrush = (HBRUSH)SelectObject(biop->Get_DC_Buffer(0), m_hBrush);
    // Create a logical pen and select into the context
    m_hPen = CreatePen(PS_SOLID, thickness, RGB(r, g, b));
    HPEN hpOldPen = (HPEN)SelectObject(biop->Get_DC_Buffer(0), m_hPen);
    // Draw the rectangle
    Rectangle(biop->Get_DC_Buffer(0), (x - size / 2), (y - size / 2), (x + size / 2), (y + size / 2));
    // Remove the object
    SelectObject(biop->Get_DC_Buffer(0), hbrOldBrush);  // first you must restore DC to original state
    SelectObject(biop->Get_DC_Buffer(0), hpOldPen);     // same here
    DeleteObject(m_hBrush);
    DeleteObject(m_hPen);
}

我生成一个线程来渲染数据:

// Inside a thread
while (1)
{
    CreateBuffer();
    for (int i = 0; i < 1028; i++)
    {
        //
        DrawBox()
        //
    }
    Render();
    ClearBuffer();
}

我正在使用FindWindow()在另一个应用程序上进行渲染。一切都很好,方框被渲染,等等,但有一个疯狂的全屏闪烁,似乎有一个黑色的背景。我猜是什么时候从内存中提取到应用程序?

我使用双重缓冲来避免闪烁,但它似乎让情况变得更糟。有什么想法吗?

谢谢。

如果需要在现有窗口上绘制透明的东西,我会使用UpdateLayeredWindow API。