绘图时防止闪烁 [英] Prevent Flickering When Drawing
本文介绍了绘图时防止闪烁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我有以下代码在屏幕上绘制矩形:
So I have this code to draw a rectangle on my screen:
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);
}
但是,当在循环中反复被调用时,它会在屏幕上闪烁.我想知道是否有办法防止这种闪烁?
However, when being called repeatedly inside a loop it flickers on the screen. I was wondering if there was a way to prevent this flicker?
任何帮助将不胜感激.
谢谢
推荐答案
这不应是答案,但我不能在注释中张贴代码:
This should not be an answer, but I can not post code in comments:
您的代码中有很多GDI泄漏.
You have many GDI leaks in your code.
复制/粘贴以下代码,并在闪烁减少时向我们报告:
Copy/paste the following code and report us if flickering diminishes:
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 = SelectObject(m_hDC, m_hBrush);
// Create a logical pen and select into the context
m_hPen = CreatePen(PS_SOLID, thickness, RGB(r, g, b));
HPEN hpOldPen = 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
SelectObject(m_hDC, hbrOldBrush); // first you must restore DC to original state
SelectObject(m_hDC, hpOldPen); // same here
DeleteObject(m_hBrush);
DeleteObject(m_hPen);
}
在MSDN上阅读有关GDI泄漏的信息.
Read on MSDN about GDI leaks.
这应该减少闪烁,但是要完全消除闪烁,您应该执行以下操作:
This should diminish flickering, but to completely remove flickering you should do the following:
- 删除
CS_VREDRAW | CS_HREDRAW
从您的窗口类定义中;
响应
- Remove the
CS_VREDRAW | CS_HREDRAW
from your window class definition; - return
1L
in your window procedure ( orTRUE
in your dialog box procedure ) in response toWM_ERASEBKGND
; - draw everything on a memory bitmap and then
BitBlt
it into yourm_hDC
-> this is called double buffering ( you can find many examples online );
这篇关于绘图时防止闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文