【发布时间】:2015-05-01 00:22:00
【问题描述】:
我正在使用这个 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() 在另一个应用程序之上呈现。一切正常,框被渲染,等等,但是有一个疯狂的全屏闪烁,似乎有黑色背景。我猜是当我从内存中绘制到应用程序时?
我正在使用双缓冲来避免闪烁,但它似乎使它变得更糟。有什么想法吗?
谢谢。
【问题讨论】:
-
如果您尝试在自己的应用程序窗口上绘图,它是否也会闪烁?我猜windows可能有一些问题,你只是不小心在别人的windows上画了。
-
从不是窗口所有者的线程操作窗口是不受支持的方案。所有的期望都落空了,这不会是一个可靠的解决方案。
-
你就是问this question的那个人。为什么要使用单独的帐户?你也从来没有回答过我关于如何、何时、为什么画画或你想要完成什么的问题。请回答这些问题并停止尝试在不知道您首先在做什么的情况下加倍缓冲。 (遗憾的是,有些人更喜欢回答问题而不是解决问题。)
-
是的,同一个人,我使用的是不同的计算机,无法以相同的帐户登录,因此使用了新帐户。我的应用程序加载,初始化缓冲区,使用 FindWindow() 来获取窗口句柄(在我的测试用例中,我使用“无标题 - 记事本”),使用上面的循环创建一个线程,并在该循环内绘制 64 个框在屏幕上使用 x/y 坐标,我有过渡移动每个框的坐标。
-
不使用双缓冲,一切正常,但矩形闪烁,使用双缓冲时,矩形现在平滑且不闪烁,但整个窗口闪烁黑色。