Prevent Flickering When Drawing

折月煮酒 提交于 2019-12-11 14:24:59

问题


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?

Any help would be appreciated.

Thanks


回答1:


This should not be an answer, but I can not post code in comments:

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);

}

Read on MSDN about GDI leaks.

This should diminish flickering, but to completely remove flickering you should do the following:

  • Remove the CS_VREDRAW | CS_HREDRAW from your window class definition;
  • return 1L in your window procedure ( or TRUE in your dialog box procedure ) in response to WM_ERASEBKGND;
  • draw everything on a memory bitmap and then BitBlt it into your m_hDC -> this is called double buffering ( you can find many examples online );



回答2:


    /*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);

    }
    }


来源:https://stackoverflow.com/questions/28755972/prevent-flickering-when-drawing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!