Thread: getting problems in double buffering code :(

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    That is because each paint message you are creating, using and destroying the memory DC.

    You should;

    Create the memory DC once, when the form is created.

    Destroy the memory DC (and/or its selected GDI objects) as few times as possible, when the form closes and resizes.

    Most importantly your paint should only be a BitBlt(), of the memory DC to the DC from the paintstruct using the rect in the paintstruct.

    When some event (timer, user input, etc) requires a screen change you redraw the memory DC and generate a paint message.

    The paint message should be the minimum size / rect possible and should be sent directly to the forms callback (using UpdateWindow()).

    I have previously posted full code.

    EDIT: Not sure if it still applies to latest .NET IDEs...

    When you create a DC it has default GDI objects (ie BITMAP)

    When you SelectObject() your created BITMAP it pushes out the default one (as the return from SelectObject()).

    You should catch this return from SelectObject() because the created BITMAP (and other created GDI objects) may not delete while selected into a DC .

    It is good practice to return the created DC to its default state by using SelectObject() to return the default GDI objects you pushed out (with created ones).
    Last edited by novacain; 10-11-2014 at 02:01 AM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  2. #2
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    Quote Originally Posted by novacain View Post
    That is because each paint message you are creating, using and destroying the memory DC.

    You should;

    Create the memory DC once, when the form is created.

    Destroy the memory DC (and/or its selected GDI objects) as few times as possible, when the form closes and resizes.

    Most importantly your paint should only be a BitBlt(), of the memory DC to the DC from the paintstruct using the rect in the paintstruct.

    When some event (timer, user input, etc) requires a screen change you redraw the memory DC and generate a paint message.

    The paint message should be the minimum size / rect possible and should be sent directly to the forms callback (using UpdateWindow()).

    I have previously posted full code.

    EDIT: Not sure if it still applies to latest .NET IDEs...

    When you create a DC it has default GDI objects (ie BITMAP)

    When you SelectObject() your created BITMAP it pushes out the default one (as the return from SelectObject()).

    You should catch this return from SelectObject() because the created BITMAP (and other created GDI objects) may not delete while selected into a DC .

    It is good practice to return the created DC to its default state by using SelectObject() to return the default GDI objects you pushed out (with created ones).
    i did what you said:
    Code:
    static LRESULT CALLBACK WndProcForm(HWND HandleWindow, UINT msg, WPARAM wParam, LPARAM lParam)
            {
                static POINT PreviousLocation, Location;
                static bool Tracking = false;
                static MouseButtons MBButtons;
                static bool blControl = false;
                static bool blShift = false;
                static bool blResize = false;
                static int xPos = 0;
                static int yPos = 0;
                static UINT_PTR timerid=0;
                static UINT_PTR JoystickTimer=1;
                static bool blnDrag = false;
                static bool KeyPressed=false;
                static int KeyDownCount=0;
                HDC hdcimage = CreateCompatibleDC(NULL);
                HBITMAP hbitmap=NULL;
                //UINT JoystickCount =0;
    //..............
    //Working with messages
                switch(msg)
                {
                    case WM_ERASEBKGND:
                        return (LRESULT)1;
    case WM_PAINT:
                    {
                        if (inst->Paint==NULL) break;
                        PAINTSTRUCT  ps;
                        HDC hdc = BeginPaint(inst->hwnd, &ps);
    
                        int width=ps.rcPaint.right-ps.rcPaint.left;
                        int height =ps.rcPaint.bottom-ps.rcPaint.top;
                        hbitmap=CreateBitmap(width,height,1,32,NULL);//create the bitmap with icon size
    
                        SelectObject(hdcimage, hbitmap);
    
                        FillRect(hdcimage,&ps.rcPaint,CreateSolidBrush(inst->clrBackColor));
                        inst->Paint(inst->hwnd,hdcimage);
    
                        BitBlt(ps.hdc,0,0,ps.rcPaint.right-ps.rcPaint.left,ps.rcPaint.bottom-ps.rcPaint.top,hdcimage,0,0,SRCCOPY);
    
                        EndPaint(inst->hwnd, &ps);
    
    
                    }
    break;
    case WM_DESTROY:
                    {
                        DeleteObject(hbitmap);
                        DeleteDC(hdcimage);
                        joyReleaseCapture(JOYSTICKID1);
                        --FormCount;
                        if(FormCount == 0)
                            End();
                    }
                    break;
    }}
    the flicker continues with child controls

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GDI+ and double buffering
    By george7378 in forum Windows Programming
    Replies: 3
    Last Post: 04-28-2012, 10:45 PM
  2. Double buffering problem
    By george7378 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2012, 12:22 PM
  3. WS_EX_COMPOSITED style (double buffering) problems
    By JasonD in forum Windows Programming
    Replies: 2
    Last Post: 10-12-2004, 11:21 AM
  4. double buffering for allegro
    By Leeman_s in forum C++ Programming
    Replies: 6
    Last Post: 09-12-2002, 02:45 PM
  5. double buffering in MFC
    By Unregistered in forum Windows Programming
    Replies: 0
    Last Post: 03-07-2002, 12:29 PM