GDI双缓冲黑色闪烁

时间:2015-02-28 07:55:29

标签: c++ winapi gdi+ gdi

我正在使用这个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()在另一个应用程序之上进行渲染。一切正常,盒子被渲染,等等,但有一个疯狂的全屏闪烁似乎有一个黑色的背景。当我从内存中抽取应用程序时,我的猜测是什么?

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

感谢。

1 个答案:

答案 0 :(得分:0)

如果您需要在现有窗口之上绘制透明内容,我会使用UpdateLayeredWindow API。