Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6639203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:31:35+00:00 2026-05-25T23:31:35+00:00

I’m sort of new to rendering graphics with GDI… I made a paint program,

  • 0

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);
    }
    EndPaint(m_hWnd,&m_PaintStruct);
}

Any help would much be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:31:36+00:00Added an answer on May 25, 2026 at 11:31 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I am doing a simple coin flipping experiment for class that involves flipping a
I know there's a lot of other questions out there that deal with this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.