CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    Join Date
    Jan 2010
    Posts
    76

    Question Controls are flicker madly on resize. I'm trying double buffering but I still ...

    ok so for the past half year I've been trying to double buffer my app as it flickers madly when I resize it. Well I was able to Set WM_ERASEBGRND to true to stop it from erasing the Background and under my WM_PAINT my windows procedure I was able to blitblt a gray background on my form so its just not all black.

    The thing is is that all of the controls flicker and I even replaced all of my SetWindowPos with DeferWindowPos.

    Before this I used WS_EX_COMPOSITED and eve though it does a pretty good job with it, it doesn't update any of my scroll bars. For example if I clicked on a scroll thumb and moved it to the top you have to let go of it for it to update the scroll bar. And when you click down on the buttons for the scroll bars they no longer sink in like they use to which causes problems.

    Anyways here is the code I'm using which I found off line and trying to get it to work so my controls stop flickering:

    Code:
    case WM_ERASEBKGND:
        return 1;
        break;
    
    HDC          hdcMem;
    HBITMAP      hbmMem;
    HANDLE       hOld;
     
    PAINTSTRUCT  ps;
    HDC          hdc;
    RECT Temp_Rect; 
    
    case WM_PAINT:
     Temp_Rect=SSMAIN_RECT;
        // Get DC for window
        hdc = BeginPaint(hwnd, &ps);
     
        // Create an off-screen DC for double-buffering
        hdcMem = CreateCompatibleDC(hdc);
        hbmMem = CreateCompatibleBitmap(hdc, SSMAIN_WIDTH,SSMAIN_HEIGHT);
     
        hOld   = (HBITMAP)SelectObject(hdcMem, hbmMem);
     
        // Draw into hdcMem here
     
        // Transfer the off-screen DC to the screen
        Temp_Rect.left=-200;
          Temp_Rect.top=-200;
        FillRect(hdcMem, &Temp_Rect, (HBRUSH) (COLOR_WINDOW+7));
        BitBlt(hdc, 0, 0,  SSMAIN_WIDTH, SSMAIN_HEIGHT, hdcMem, 0, 0, SRCCOPY);
     
        // Free-up the off-screen DC
        SelectObject(hdcMem, hOld);
        DeleteObject(hbmMem);
        DeleteDC    (hdcMem);
     
        EndPaint(hwnd, &ps);
       
        break;

  2. #2
    Join Date
    Jul 2006
    Posts
    75

    Re: Controls are flicker madly on resize. I'm trying double buffering but I still ...

    Hi,

    I had a problem with flickering using tab control. I solved it with the help of sublass procedure
    Here how it looks like:
    Code:
     WinTabBase* object = (WinTabBase*)GetWindowLong(hWnd,GWL_USERDATA);
    
    	/*if(uMsg == WM_LBUTTONDOWN)
    	{
    		//return 0;
    	}*/
    
    	if(uMsg == WM_RBUTTONDOWN)
    	{
    		SendMessage(hWnd,WM_LBUTTONDOWN,wParam,lParam);
    		POINT cpos;
    		GetCursorPos(&cpos);
    		TrackPopupMenu(object->tabmenu,TPM_LEFTALIGN,cpos.x,cpos.y,0,object->handle,0);
    	}
    
    	if (uMsg == WM_ERASEBKGND)
            return 1;
        if (uMsg == WM_PAINT) 
    	{
    
            ValidateRect (hWnd, NULL);
    
            HDC hControlDC;
            HDC hMemoryDC;
            RECT rc;       
            HBITMAP hBitmap;    
            HGDIOBJ hSave;       
    
            GetClientRect (hWnd, & rc); 
    
            hControlDC = GetDC (hWnd);
            hMemoryDC = CreateCompatibleDC (hControlDC);
    
            hBitmap = CreateCompatibleBitmap (hControlDC, rc.right, rc.bottom);
            hSave = SelectObject (hMemoryDC, hBitmap);
    
           
            CallWindowProc (object->prevwndproc, hWnd, WM_ERASEBKGND, (WPARAM)hMemoryDC, 0);
    
            
            CallWindowProc (object->prevwndproc, hWnd, WM_PRINTCLIENT, (WPARAM)hMemoryDC, PRF_CLIENT);
    
            
            BitBlt (hControlDC, 0, 0, rc.right, rc.bottom, hMemoryDC, 0, 0, SRCCOPY);
    
            DeleteObject (SelectObject (hMemoryDC, hSave));
            DeleteDC (hMemoryDC);
            ReleaseDC (hWnd, hControlDC);
    
            return 0;
        }
    
    	if(uMsg == WM_COMMAND)
    	{
    		object->OnCommand(hWnd,uMsg,wParam,lParam);
    	}
    
    
        return CallWindowProc (object->prevwndproc, hWnd, uMsg, wParam, lParam);
    Basically i'm doing the same as you but in subclass procedure. Hope it will help.

  3. #3
    Join Date
    Jan 2010
    Posts
    76

    Re: Controls are flicker madly on resize. I'm trying double buffering but I still ...

    Quote Originally Posted by Sh@dow View Post
    Hi,

    I had a problem with flickering using tab control. I solved it with the help of sublass procedure
    Here how it looks like:
    Code:
     WinTabBase* object = (WinTabBase*)GetWindowLong(hWnd,GWL_USERDATA);
    
    	/*if(uMsg == WM_LBUTTONDOWN)
    	{
    		//return 0;
    	}*/
    
    	if(uMsg == WM_RBUTTONDOWN)
    	{
    		SendMessage(hWnd,WM_LBUTTONDOWN,wParam,lParam);
    		POINT cpos;
    		GetCursorPos(&cpos);
    		TrackPopupMenu(object->tabmenu,TPM_LEFTALIGN,cpos.x,cpos.y,0,object->handle,0);
    	}
    
    	if (uMsg == WM_ERASEBKGND)
            return 1;
        if (uMsg == WM_PAINT) 
    	{
    
            ValidateRect (hWnd, NULL);
    
            HDC hControlDC;
            HDC hMemoryDC;
            RECT rc;       
            HBITMAP hBitmap;    
            HGDIOBJ hSave;       
    
            GetClientRect (hWnd, & rc); 
    
            hControlDC = GetDC (hWnd);
            hMemoryDC = CreateCompatibleDC (hControlDC);
    
            hBitmap = CreateCompatibleBitmap (hControlDC, rc.right, rc.bottom);
            hSave = SelectObject (hMemoryDC, hBitmap);
    
           
            CallWindowProc (object->prevwndproc, hWnd, WM_ERASEBKGND, (WPARAM)hMemoryDC, 0);
    
            
            CallWindowProc (object->prevwndproc, hWnd, WM_PRINTCLIENT, (WPARAM)hMemoryDC, PRF_CLIENT);
    
            
            BitBlt (hControlDC, 0, 0, rc.right, rc.bottom, hMemoryDC, 0, 0, SRCCOPY);
    
            DeleteObject (SelectObject (hMemoryDC, hSave));
            DeleteDC (hMemoryDC);
            ReleaseDC (hWnd, hControlDC);
    
            return 0;
        }
    
    	if(uMsg == WM_COMMAND)
    	{
    		object->OnCommand(hWnd,uMsg,wParam,lParam);
    	}
    
    
        return CallWindowProc (object->prevwndproc, hWnd, uMsg, wParam, lParam);
    Basically i'm doing the same as you but in subclass procedure. Hope it will help.
    Thanks Shadow for the code and all.

    Does this mean I have to subclass each control that I have on my parent form also?

    Also wouldn't that cause problems with certain controls that needs to do its own specific painting as well?

    Thanks

  4. #4
    Join Date
    Jul 2006
    Posts
    75

    Re: Controls are flicker madly on resize. I'm trying double buffering but I still ...

    I don't know what controls do you use. I had a tab control. I did subclassing only for it, not for controls that is have. I worked for me.
    As i understood you have flickering when you resize your parent form. Try to subclass only this parent form and see if it helps.

  5. #5
    Join Date
    Jan 2010
    Posts
    76

    Re: Controls are flicker madly on resize. I'm trying double buffering but I still ...

    Quote Originally Posted by Sh@dow View Post
    I don't know what controls do you use. I had a tab control. I did subclassing only for it, not for controls that is have. I worked for me.
    As i understood you have flickering when you resize your parent form. Try to subclass only this parent form and see if it helps.



    How would I go about subclassing the parent form?

    Wouldn't that be the same as editing the WM_PAINT message of your windows procedure?

    Also I changed the size of bitblt for a certain area.

    Now heres the thing, the area where the bitblt is flickers while the rest of my windows doesn't as it leaves a trail of the other controls of the form when I resize it.

    Also I have a toolbar,rebar,buttons,static, and a rtf control on my form.

    I'm extremely lost with this ><

  6. #6
    Join Date
    Jan 2010
    Posts
    76

    Re: Controls are flicker madly on resize. I'm trying double buffering but I still ...

    ok guys since this is not working for me I guess I'm going have to clip the areas where the controls are after I figure out how to do that

  7. #7
    Join Date
    Jul 2006
    Posts
    75

    Re: Controls are flicker madly on resize. I'm trying double buffering but I still ...

    >>How would I go about subclassing the parent form?

    After creating the form (CreateWindow / CreateWindowEx) do the following:
    Code:
    prevwndproc = (WNDPROC) SetWindowLong (
                handle, GWL_WNDPROC, (LONG) TabCtrlSubclassProc);
    This code sets a new window procedure TabCtrlSubclassProc which is defined by you.
    In the procedure you do your custom job after that do a
    Code:
    CallWindowProc (object->prevwndproc, hWnd, uMsg, wParam, lParam);
    to call a normal window procedure.

    Don't pay attention to the "object->". Since TabCtrlSubclassProc must be defined as static i use object as an additional parameter to the procedure to get handles and other window specific data. I store it using"
    Code:
    SetWindowLong(handle,GWL_USERDATA,(LONG)this);
    And getting using:
    Code:
    WinTabBase* object = (WinTabBase*)GetWindowLong(hWnd,GWL_USERDATA);
    This might help:
    http://msdn.microsoft.com/en-us/libr...lassing_window

  8. #8
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Controls are flicker madly on resize. I'm trying double buffering but I still ...

    Quote Originally Posted by Senith View Post
    ok guys since this is not working for me I guess I'm going have to clip the areas where the controls are after I figure out how to do that
    In fact, what you really have to do is to prepare a simple minimalistic compilable project that reproduces your problem, and upload the one here in the thread. usually this is most effective way to investigate your problem and provide a solution to it.
    Best regards,
    Igor

  9. #9
    Join Date
    Jan 2010
    Posts
    76

    Re: Controls are flicker madly on resize. I'm trying double buffering but I still ...

    Quote Originally Posted by Igor Vartanov View Post
    In fact, what you really have to do is to prepare a simple minimalistic compilable project that reproduces your problem, and upload the one here in the thread. usually this is most effective way to investigate your problem and provide a solution to it.
    Ok thanks, I really appreciate that .

    I have to get some work done though so when I get back I'll upload my program for you all

    Thanks again ^^

  10. #10
    Join Date
    Jan 2010
    Posts
    76

    Re: Controls are flicker madly on resize. I'm trying double buffering but I still ...

    Also I have some images here as well

    1. [img] http://phantomworksstudios.com/cpp/flickeringarea.png [/img]
    2. [img] http://phantomworksstudios.com/cpp/flickeringpng.png [/img]

    Due to my servers restriction I have to have my application within a zip file.
    http://phantomworksstudios.com/cpp/ssgdiproblem.zip
    There are two.

    One is where I only bitblt part of the window which is the top left while the other one I bitblt the whole thing


    I'll be back on later all

    Thanks again ^^
    Last edited by Senith; May 17th, 2012 at 04:43 PM.

  11. #11
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917

    Re: Controls are flicker madly on resize. I'm trying double buffering but I still ...

    I am not sure why did you deliver executable. Did misunderstood Igor’s post?
    Quote Originally Posted by Igor Vartanov View Post
    ...prepare a simple minimalistic compilable project that reproduces your problem, and upload the one here in the thread.
    He meant to create project that reproduces a problem, so we will have a chance to see the cause not the effect.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  12. #12
    Join Date
    Jan 2010
    Posts
    76

    Re: Controls are flicker madly on resize. I'm trying double buffering but I still ...

    Quote Originally Posted by JohnCz View Post
    I am not sure why did you deliver executable. Did misunderstood Igor’s post?
    He meant to create project that reproduces a problem, so we will have a chance to see the cause not the effect.
    I'm sorry I did misunderstood that.

    If you mean create another project just with the double buffer and some controls then yes I've done that as well and I still get the same results.

    Also I don't know why but the only control in my app that doesn't flicker is everything inside of one static control which has it own windows proc and is the parent of a rtf and a list box which doesn't flicker.

    Everything else though does flicker so i don't know whats going on

    Also my OS is windows 2008 Server Standard and I'm using wxDevC++ as my IDE.

  13. #13
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917

    Re: Controls are flicker madly on resize. I'm trying double buffering but I still ...

    Quote Originally Posted by Senith View Post
    If you mean create another project just with the double buffer and some controls then yes I've done that as well and I still get the same results.
    OK, you have created project that does the same.
    The thing we are asking for is to compress (zip) entire project (solution, project, source and resource files) and attach it to your post.
    Before you do that remove Debug and Release folder as well as .ncb file.

    This way we can build and debug your code to find what is causing flicker problem.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  14. #14
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: Controls are flicker madly on resize. I'm trying double buffering but I still ...

    It seems like your code for double-buffering is in the WndProc for the main window (please confirm).

    From your screenshots, it also seems like you do not actually have any controls per se, but rather you are brute-force-drawing the _shapes_ of what look like controls (but aren't actually "real" controls) into the the double buffer.

    If so, then you are setting about this in the wrong way. In addition, it explains why a few of your "real" controls (you mentioned a static control and a list box) are behaving properly.

    The correct way is to let the controls draw themselves from their own WndProcs (which may be the windows-supplied default WndProc for the control). Then in your main window, do only one thing: set the WS_CLIPCHILDREN style. No need to override the main window's WM_ERASEBKGRD.

    Anyway, we look forward to your post of the source code for a minimal program that exhibits the problems you are experiencing.

    Mike

  15. #15
    Join Date
    Jan 2010
    Posts
    76

    Re: Controls are flicker madly on resize. I'm trying double buffering but I still ...

    Quote Originally Posted by MikeAThon View Post
    It seems like your code for double-buffering is in the WndProc for the main window (please confirm).

    From your screenshots, it also seems like you do not actually have any controls per se, but rather you are brute-force-drawing the _shapes_ of what look like controls (but aren't actually "real" controls) into the the double buffer.

    If so, then you are setting about this in the wrong way. In addition, it explains why a few of your "real" controls (you mentioned a static control and a list box) are behaving properly.

    The correct way is to let the controls draw themselves from their own WndProcs (which may be the windows-supplied default WndProc for the control). Then in your main window, do only one thing: set the WS_CLIPCHILDREN style. No need to override the main window's WM_ERASEBKGRD.

    Anyway, we look forward to your post of the source code for a minimal program that exhibits the problems you are experiencing.

    Mike
    (I haven't been online for a while due to work and the internet not working correctly)

    And that is correct as the double buffer is within the WndProc of my application.

    Also I have the double buffering working now. Some styles of the control such as "WS_CLIPCHILDREN, WS_CLIPSIBLINGS AND WS_EX_TRANSPARENT" were causing some problems with my double buffering which I have working now


    (Thanks for your info WS_CLIPCHILDREN really appreciate it. I've tried that though with my first application, Everything withing it flickered. )

    Also if I misunderstood your post please let me know .

    Also all the controls on my form are real controls and not custom made (Yet)

    Also I ran into the problem with my frame control. For some reason every other control within my app has its background clipped besides my frame(button). So of course everytime I resize the frame will flicker. Now if I add WS_EX_TRANSPARENT to the frame control the background of it is not being up dated as leaves a trail of windows behind. Almost as if its being clipped after I set it to WS_EX_TRANSPARENT.

    So I know how to get around that too(an idea that popped into my mind. Explained below) as well, but i'm stuck atm.

    I need to be able to draw onto the background of the frame control without erasing the frames border(ie I want to preserve the frames edges and text)

    Thanks everyone for your help. It really helped me out.

    Also if you guys will still like to see my code let me know and I'll post it. (Still need to clean it up as its a mess now *Working on it*)

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured