所以我有这个代码在我的屏幕上绘制一个矩形:
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);
}
但是,当在循环内反复调用时,它会在屏幕上闪烁。我想知道是否有办法防止这种闪烁?
任何帮助都将不胜感激。
由于
答案 0 :(得分:2)
这不应该是答案,但我不能在评论中发布代码:
您的代码中有许多GDI泄漏。
复制/粘贴以下代码,如果闪烁减少,则报告我们:
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泄漏的信息。
这可以减少闪烁,但要完全消除闪烁,您应该执行以下操作:
CS_VREDRAW | CS_HREDRAW
; 1L
(或在对话框过程中TRUE
)以回复WM_ERASEBKGND
; BitBlt
将其绘制到m_hDC
- >中这被称为双缓冲(你可以在网上找到很多例子); 答案 1 :(得分:0)
/*Hi You May Change Your Code To This*/
LOGBRUSH m_LogBrush;
//HBRUSH m_hBrush;
//HPEN m_hPen;
//HDC m_hDC;
HWND m_hWND; //Your WindowHandle instead of your DC
//
void DrawBox(int x, int y, int r, int g, int b, int size, int thickness)
{
// Lock & Get Forground DC From m_hWND
HDC m_hDC = GetDC(m_hWND);
if (m_hDC != 0) //Make Sure It's ok
{
// Double Buffering Begins Here
// Create Background DC From m_hDC
HDC mem_m_hDC = CreateCompatibleDC(m_hDC);
// Calculate Window Bounds
RECT ClientRect = { 0 };
GetClientRect(m_hWND, &ClientRect);
// Create Background Buffer Frame
BITMAPINFO bmi = { 0 };
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = ClientRect.right - ClientRect.left;
bmi.bmiHeader.biHeight = ClientRect.bottom - ClientRect.top;
bmi.bmiHeader.biBitCount = 32;
bmi.bmiHeader.biCompression = BI_RGB;
bmi.bmiHeader.biPlanes = 1;
HBITMAP memBMP = CreateDIBSection(mem_m_hDC, &bmi, DIB_RGB_COLORS, 0, 0, 0);
// Select Background Buffer Frame
SelectObject(mem_m_hDC, memBMP);
// Brush style to hollow
m_LogBrush.lbStyle = BS_NULL;
// Create a logical brush and select into the context
HBRUSH m_hBrush = CreateBrushIndirect(&m_LogBrush);
HGDIOBJ oldHGDIOBJ1 = SelectObject(m_hDC, m_hBrush); //Save Old Seleteed GDI Object To oldHGDIOBJ1
// Create a logical pen and select into the context
HPEN m_hPen = CreatePen(PS_SOLID, thickness, RGB(r, g, b));
HGDIOBJ oldHGDIOBJ2 = SelectObject(m_hDC, m_hPen); //Save Old Seleteed GDI Object To oldHGDIOBJ2
// Draw the rectangle in Background Memory DC
Rectangle(mem_m_hDC, (x - size / 2), (y - size / 2), (x + size / 2), (y + size / 2));
// Copy Background DC To Forground DC
BitBlt(m_hDC, 0, 0, bmi.bmiHeader.biWidth, bmi.bmiHeader.biHeight, mem_m_hDC, 0, 0, SRCCOPY);
// Delete Background Buffer Frame
DeleteObject(memBMP);
// Delete Background DC
DeleteDC(mem_m_hDC);
// Double Buffering Ends Here
// Unlock Forground DC
ReleaseDC(m_hWND, m_hDC);
// Remove the objects
DeleteObject(m_hBrush);
DeleteObject(m_hPen);
}
}