【发布时间】:2015-02-27 01:23:27
【问题描述】:
所以我有这段代码可以在我的屏幕上绘制一个矩形:
LOGBRUSH m_LogBrush;
HBRUSH m_hBrush;
HPEN m_hPen;
HDC m_hDC;
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);
SelectObject(m_hDC, m_hBrush);
// Create a logical pen and select into the context
m_hPen = CreatePen(PS_SOLID, thickness, RGB(r, g, b));
SelectObject(m_hDC, m_hPen);
// Draw the rectangle
Rectangle(m_hDC, (x - size / 2), (y - size / 2), (x + size / 2), (y + size / 2));
// Remove the object
DeleteObject(m_hBrush);
DeleteObject(m_hPen);
}
但是,当在循环内重复调用时,它会在屏幕上闪烁。我想知道是否有办法防止这种闪烁?
任何帮助将不胜感激。
谢谢
【问题讨论】:
-
我该怎么做?
-
你不应该需要双缓冲。您没有显示相关代码。显示更多。显示闪烁的完整程序。
-
更准确地说,你是如何、何时、在哪里绘制的?你的目标是什么?
-
很久以前,当我做Win32编程时,所有的绘图都必须在响应OnPaint消息时完成,否则会闪烁
-
@sp2:这完全是假的。没有
OnPaint消息。 MFC 或 WTL 具有由框架调用以响应WM_PAINT消息的OnPaint方法。闪烁是在绘制周期中多次更改像素的结果,无论是否在WM_PAINT消息处理程序之外。为了防止闪烁,每个像素在一个绘制周期中最多只能更改一次。双缓冲是实现这一目标的一种方式。