开发者

How to avoid excessive screen flickering with GDI

I'm sort of new to rendering graphics with GDI...

I made a paint program, and it works fine, it's just that it causes a lot of annoying screen flickering. I will admit my paint code is not really optimized (lack of time), but it shouldn't be super inefficient either, so I'm puzzled.

What I'm basically doing is creating a compatible DC on init, then create a compatible bitmap. Then I select it into the compatible DC, and paint to the compatible DC. Then I use BitBlit() to copy it to the window hDC...

Could anyone tell me the possible causes for this screen tearing? EDIT: btw, screen flickering only occurs during the drawing of a path (before the path gets drawn to the hMemDC, it gets drawn to the hDC of the window)

Code samples: (EDIT: If you need to see any more code that you think is relevant, comment and I'll edit)


Path::DrawTo(HDC)

bool Path::DrawTo(HDC hDC)
{
    if(hDC == NULL || m_PointVector.size() <= 0) {
        return false;
    }

    switch (m_Tool) {
    case Tool_Pen:
        {
            Point2D p = m_PointVector.at(0);

            if(m_PointVector.size() > 1) {
                HPEN oldPen = (HPEN)SelectObject(hDC,m_hPen);

                MoveToEx(hDC, p.x, p.y, nullptr);

                for(UINT i = 1; i < m_PointVector.size(); ++i) {
                    p = m_PointVector.at(i);
                    LineTo(hDC,p.x,p.y);
                }

                SelectObject(hDC,oldPen);
                break;
            } //else

            SetPixel(hDC,p.x-1,p.y,m_Col);
            SetPixel(hDC,p.x,p.y,m_Col);
            SetPixel(hDC,p.x+1,p.y,m_Col);
            SetPixel(hDC,p.x,p.y-1,m_Col);
            SetPixel(hDC,p.x,p.y+1,m_Col);
            break;
        }
    case Tool_Line:
        {
            if(m_PointVector.size() > 1) {
                Point2D p = m_PointVector.at(0);
                HPEN oldPen = (HPEN)SelectObject(hDC,m_hPen);

                MoveToEx(hDC, p.x, p.y, nullptr);

                for(UINT i = 1; i < m_PointVector.size(); ++i) {
                    p = m_PointVector.at(i);
                    LineTo(hDC,p.x,p.y);
                }

                SelectObject(hDC,oldPen);
            }
            break;
        }
    case Tool_Ellipse:
        {
            if(m_PointVector.size() > 1) {
                HPEN oldPen = (HPEN)SelectObject(hDC,m_hPen);
                SelectObject(hDC,m_hBrush);

                Point2D p1 = m_PointVector.at(0);
                Point2D p2 = m_PointVector.at(1);

                if(p1.x > p2.x) {
                    int iTemp = p1.x;
                    p1.x = p2.x;
                    p2.x = iTemp;
                }
                if(p1.y > p2.y) {
                    int iTemp = p1.y;
                    p1.y = p2.y;
                    p2.y = iTemp;
                }

                Ellipse(hDC,p1.x,p1.y,p2.x,p2.y);

                SelectObject(hDC,oldPen);
            }
            break;
        }
    case Tool_Rectangle:
        {
            if(m_PointVector.size() > 1) {
                HPEN oldPen = (HPEN)SelectObject(hDC,m_hPen);
                SelectObject(hDC,m_hBrush);

                Point2D p1 = m_PointVector.at(0);
                Point2D p2 = m_PointVector.at(1);

                if(p1.x > p2.x) {
                    int iTemp = p1.x;
                    p1.x = p2.x;
                    p2.x = iTemp;
                }
                if(p1.y > p2.y) {
                    int iTemp = p1.y;
                    p1.y = p2.y;
                    p2.y = iTemp;
                }

                Rectangle(hDC,p1.x,p1.y,p2.x,p2.y);

                SelectObject(hDC,oldPen);
            }
            break;
        }
    case Tool_LineTrack:
        {
            HPEN oldPen = (HPEN)SelectObject(hDC,m_hPen);
            SelectObject(hDC,m_hBrush);

            int vSize = (int)m_PointVector.size();
            Point2D p = m_PointVector.at(0);

            if (vSize <= 1) {
                Ellipse(hDC,p.x-10,p.y-10,p.x+10,p.y+10);
            }
            else {
                //draw LineTrack
                Point2D pTemp = m_PointVector.at(1);
                MoveToEx(hDC,p.x,p.y,nullptr);

                for (int i = 1; i < vSize; ++i) {
                    p = m_PointVector.at(i);
                    pTemp = m_PointVector.at(i-1);
                    LineTo(hDC,p.x,p.y);
                    Ellipse(hDC,pTemp.x-10,pTemp.y-10,pTemp.x+10,pTemp.y+10);
                }

                Ellipse(hDC,p.x-10,p.y-10,p.x+10,p.y+10);
            }

            SelectObject(hDC,oldPen);
            break;
        }
    }

    return true;
}

WndProc(HWND, UINT, WPARAM, LPARAM)

LRESULT MyApp::WndProc(HWND hWnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
    if(iMsg == WM_CREATE)
    {
        CREATESTRUCT *pCS = (CREATESTRUCT*)lParam;
        SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG)pCS->lpCreateParams);

    }
    else
    {
        //retrieve the stored "this" pointer
        MyApp* pApp = (MyApp*)GetWindowLongPtr(hWnd, GWLP_USERDATA);

        switch (iMsg)
        {
            case WM_PAINT:
                {
                pApp->Paint();
                return 0;
                }

            case WM_COMMAND:
            {
                int wmId    = LOWORD(wParam);
                int wmEvent = HIWORD(wParam);

                // Parse the menu selections:
                switch (wmId)
                {
                case IDM_NEW:
                    {
                        ////
                        return 0;
                    }
                    return 0;
                case IDM_LOAD:
                    {
                        //////
                        return 0;
                    }
                case IDM_SAVE:
                    {
                    //////
                    return 0;
                    }
                case IDM_SAVEAS:
                    {
                        //////
                        return 0;
                    }
                case IDM_COLOURMAIN:
                    {
                        COLORREF col;
                        if(MyWin32Funcs::OnColorPick(col)) {
                            pApp->m_pPath->SetColor1(col);
                        }
                    return 0;
                    }
                case IDM_COLOURSECONDARY:
                    {
                    COLORREF col;
                        if(MyWin32Funcs::OnColorPick(col)) {
                            pApp->m_pPath->SetColor2(col);
                        }
                    return 0;
                    }
                case IDM_PEN:
                    {
                        pApp->m_pPath->SetTool(Tool_Pen);
                        return 0;
                    }
                case IDM_LINE:
                    {
                        pApp->m_pPath->SetTool(Tool_Line);
                        return 0;
                    }
                case IDM_ELLIPSE:
                    {
                        pApp->m_pPath->SetTool(Tool_Ellipse);
                        return 0;
                    }
                case IDM_RECTANGLE:
                    {
                        pApp->m_pPath->SetTool(Tool_Rectangle);
                        return 0;
                    }
                case IDM_LINETRACK:
                    {
                        pApp->m_pPath->SetTool(Tool_LineTrack);
                        return 0;
                    }
                default:
                    {
                    //////
                    return 0;
                    }
                }
            }

            case WM_LBUTTONUP:
                {
                    //////
                    Point2D p;
                    p.x = LOWORD(lParam); 
                    p.y = HIWORD(lParam);

                    switch(pApp->m_pPath->GetTool()) {
                        case Tool_Pen:
                            {
                                pApp->m_bPaintToBitmap = true;
                                InvalidateRect(pApp->m_hWnd,NULL,true);
                                break;
                            }
                        case Tool_Ellipse:
                            {
                                pApp->m_bPaintToBitmap = true;
                                InvalidateRect(pApp->m_hWnd,NULL,true);
                                break;
                            }
                        case Tool_Rectangle:
                            {
                                pApp->m_bPaintToBitmap = true;
                                InvalidateRect(pApp->m_hWnd,NULL,true);
                                break;
                            }
                        case Tool_Line:
                            {
                                pApp->m_pPath->AddPoint(p,pApp->m_pBitmapPainter->GetBmpSize(),pApp->m_pBitmapPainter->GetBmpOffset());
                                InvalidateRect(pApp->m_hWnd,NULL,false);
                                break;
                            }
                        case Tool_LineTrack:
                            {
                                pApp->m_pPath->AddPoint(p,pApp->m_pBitmapPainter->GetBmpSize(),pApp->m_pBitmapPainter->GetBmpOffset());
                                InvalidateRect(pApp->m_hWnd,NULL,false);
                                break;
                            }
                    }

                    return 0;
                }

            case WM_RBUTTONUP:
                {
                    //////
                    int x = LOWORD(lParam);
                    int y = HIWORD(lParam);

                    switch(pApp->m_pPath->GetTool()) {
                        case Tool_Line:
                            {
                                pApp->m_bPaintToBitmap = true;
                                InvalidateRect(pApp->m_hWnd,NULL,true);
                                break;
                            }
                        case Tool_LineTrack:
                            {
                                pApp->m_bPaintToBitmap = true;
                                InvalidateRect(pApp->m_hWnd,NULL,true);
                                break;
                            }
                    }

                    return 0;
                }
            case WM_LBUTTONDOWN:
                {
                    Point2D p;
                    p.x = LOWORD(lParam);
                    p.y = HIWORD(lParam);
                    switch(pApp->m_pPath->GetTool()) {
                    case Tool_Pen:
                        pApp->m_pPath->AddPoint(p,pApp->m_pBitmapPainter->GetBmpSize(),pApp->m_pBitmapPainter->GetBmpOffset());
                        InvalidateRect(pApp->m_hWnd,NULL,false);
                        break;
                    }
                }
            case WM_MOUSEMOVE:
                {
                    Point2D p;
                    p.x = LOWORD(lParam);
                    p.y = HIWORD(lParam);
                    if (wParam & MK_LBUTTON) {
                        switch(pApp->m_pPath->GetTool()) {
                        case Tool_Pen:
                            {
                            pApp->m_pPath->AddPoint(p,pApp->m_pBitmapPainter->GetBmpSize(),pApp->m_pBitmapPainter->GetBmpOffset());
                            InvalidateRect(pApp->m_hWnd,NULL,false);
                            break;
                            }
                        case Tool_Ellipse:
                            {
                            if( pApp->m_pPath->GetLen() >= 1) {
                                pApp->m_pPath->SetPointAt(1,p,pApp->m_pBitmapPainter->GetBmpSize(),pApp->m_pBitmapPainter->GetBmpOffset());
                            }
                            else {
                                pApp->m_pPath->AddPoint(p,pApp->m_pBitmapPainter->GetBmpSize(),pApp->m_pBitmapPainter->GetBmpOffset());
                            }
                            InvalidateRect(pApp->m_hWnd,NULL,false);
                            break;
                            }
                        case Tool_Rectangle:
                            {
                            if( pApp->m_pPath->GetLen() >= 1) {
                                pApp->m_pPath->SetPointAt(1,p,pApp->m_pBitmapPainter->GetBmpSize(),pApp->m_pBitmapPainter->GetBmpOffset());
                            }
                            else {
                                pApp->m_pPath->AddPoint(p,pApp->m_pBitmapPainter->GetBmpSize(),pApp->m_pBitmapPainter->GetBmpOffset());
                            }
                            InvalidateRect(pApp->m_hWnd,NULL,false);
                            break;
                            }
                        }
                    }

                    return 0;
                }

            case WM_CLOSE:
                {
                    //////
                                return 0;
                            }
                        }
                    PostQuitMessage(0);
                    return 0;
                }
        }
    }
    return DefWindowProc (hWnd, iMsg, wParam, lParam) ;
}

MyApp::Paint()

void MyApp::Paint()
{
    BeginPaint(m_hWnd,&m_PaintStruct);
    if (m_bPaintToBitmap) {
        Point2D p;
        p.x = BMPXOFFSET;
        p.y = BMPYOFFSET;
        m_pPath->Offset(p);
        m_pPath->DrawTo(m_pBitmapPainter->GetMemDC());
        m_pPath->ClrPath();
        m_pBitmapPainter->Paint(); //this is where BitBlt() occurs
        m_bPaintToBitmap = false;
        if(m_pBitmapPainter->IsAdjusted() == false) {
            m_pBitmapPainter->SetbAdjusted(true);
        }
    }
    else {
        m_pBitmapPainter->Paint(); //this is where BitBlt() occurs
        m_pPath->DrawTo(m_hDC);
    }
   开发者_Python百科 EndPaint(m_hWnd,&m_PaintStruct);
}

Any help would much be appreciated.


I think what you're seeing is flicker, not tearing. To minimize flicker, your WM_PAINT should write to the window DC exactly once. Typically, this one operation is a BitBlt:

HDC hdc = BeginPaint(m_hwnd, &m_PaintStruct);
... paint to bitmap ...
BitBlt(hdc, ...); // blt from bitmap to screen
EndPaint(m_hwnd, &m_PaintStruct);

If you paint to the window DC in multiple steps, then you open the window for flicker.

Your description of the problem doesn't match the code. In your description, you say that you're blitting from the compatible DC to the window hDC. But in your code, your BitBlt is followed by a m_pPath->DrawTo(m_hDC). If a refresh occurs during the DrawTo, then the screen will refresh with a partially-drawn view, resulting in flicker.


If you are drawing the entire client area, override WM_ERASEBKGND, and simply return TRUE. That will reduce the flicker.

As others have pointed out; use the HDC given by WM_PAINT, as it may contain clipping regions, and other stuff that may optimize the screen update.

EDIT If you are not drawing the entire client area, you can perform background painting in the areas you know where your WM_PAINT handler won't paint.


BeginPaint gives you HDC you are supposed to paint to. You are ignoring it.


What OS are you running on ? If its Vista or Windows 7, are you running in some sort of "compatibility mode" disabling desktop compositing ?

Supposedly one of the advantages of the Desktop Window Manager (DWM) introduced with Vista is (source):

In Windows XP, applications update their windows directly when the OS requests them to. These requests could be executed asynchronously with respect to the refresh rate of the monitor or to any updates that may be currently running. The effect of these requests is that the user sees windows tearing and re-drawing incorrectly or slowly. The DWM style of window presentation eliminates the tearing artifacts, providing a high quality desktop experience. The benefit to the end user is that the system appears to be more responsive and the experience is cleaner.

ie with the DWM, compositing to the desktop will be synchronized with drawing to avoid ever seeing partially drawn "torn" things.

If that's not it, are you sure you're not confusing "tearing" with background erase flicker or something ?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜