5

Is there a way to double-buffer the common controls? Currently when they resize they flicker. A lot.....

EDIT: If it helps, it is a bunch of button controls and a few edit controls, all sitting on top of a tab control. The Tab control redraws itself, then the buttons redraw themselves. When the buttons redraw, they flicker.

EDIT2: Here's an example of the problem I'm having: http://billy-oneal.com/Lobfuscator.exe

Jon Seigel
  • 12,251
  • 8
  • 58
  • 92
Billy ONeal
  • 104,103
  • 58
  • 317
  • 552

8 Answers8

4

I understand the topic is quite old, but this may be relevant to someone who's running into trouble with a flicker problem.

Very much like Billy, I have run into an issue that pops up when switching tabs, where controls that are placed on the tab flicker when being shown and hidden. For reference, I'm using ShowWindow function extensively to hide and show controls.

I have been fiddling with WS_EX_COMPOSITED for a few hours and it gave me very odd results. I'm also not resizing anything, the dialog is designed to run fullscreen and it adapts to the current desktop resolution.

This is the layout of my dialog, which I have created manually, calling the CreateWindowEx function for every control:

Main window -- some controls -- tab control ---- some more controls

Indent represents the parent-child relations. The tab control has WS_CHILD and WS_CLIPCHILDREN styles set on creation, all of the controls have WS_CHILD style set.

What in the end did the trick was the following

MainProc proc hWnd:DWORD,uMsg:DWORD,wParam:DWORD,lParam:DWORD
    mov eax,uMsg
    cmp eax,WM_INITDIALOG
    je @WM_INITDIALOG
    ...

    invoke DefWindowProc,hWnd,uMsg,wParam,lParam
    ret

@WM_INITDIALOG:
    ...

    invoke GetWindowLong,hWnd,GWL_EXSTYLE
    or eax,WS_EX_COMPOSITED
    invoke SetWindowLong,hWnd,GWL_EXSTYLE,eax
    ...

MainProc endp

This bit is written in assembly (MASM32), but I'm sure you get the gist of it. Simply, get the EX_STYLE of your main window sometime during WM_INITDIALOG and add WS_EX_COMPOSITED to it.

In this singular case, this method works both on 32bit Windows XP SP3 and 64bit Windows 7 SP1. There was no need to add the WS_EX_COMPOSITED style to any of the tab's child controls (some of the static controls I use have WS_EX_TRANSPARENT set, but that's for other reasons) and at this time there is no apparent need to return non-zero on WM_ERASEBKGND message. I'm also not experiencing any performance problems on a moderately powerful C2D machine.

For reference, this is my Main

Main proc hInst:DWORD,hPrevInst:DWORD,CmdLine:DWORD,CmdShow:DWORD
    LOCAL wc:WNDCLASSEX,msg:MSG

    mov wc.cbSize,sizeof WNDCLASSEX
    mov wc.style,CS_HREDRAW or CS_VREDRAW
    mov wc.lpfnWndProc,offset MainProc
    mov wc.cbClsExtra,NULL
    mov wc.cbWndExtra,DLGWINDOWEXTRA
    push hInst
    pop wc.hInstance
    mov wc.hbrBackground,COLOR_BTNFACE+1
    mov wc.lpszClassName,offset szClassName
    invoke LoadIcon,NULL,IDI_APPLICATION
    mov wc.hIcon,eax
    mov wc.hIconSm,eax
    invoke LoadCursor,NULL,IDC_ARROW
    mov wc.hCursor,eax
    invoke RegisterClassEx,addr wc
    invoke CreateDialogParam,hInstance,IDD_MAIN,NULL,addr MainProc,NULL
    invoke ShowWindow,hWin,SW_SHOWNORMAL
    invoke UpdateWindow,hWin
    invoke LoadAccelerators,hInstance,IDD_ACC_TABLE
    mov hAcc,eax
    jmp @2
@1:
    invoke TranslateAccelerator,hWin,hAcc,addr msg
    test eax,eax
    jne @2
    invoke TranslateMessage,addr msg
    invoke DispatchMessage,addr msg
@2:
    invoke GetMessage,addr msg,NULL,0,0
    test eax,eax
    jne @1

    mov eax,msg.wParam
    ret

Main endp

There's nothing special here, neither. I'm setting the "dialog control grey" as the background color and using the CS_*REDRAW styles, those appear not to affect this situation. The "empty" dialog template I used to create the main windows is this

IDD_MAIN DIALOGEX 0,0,318,177
FONT 8,"MS Sans Serif",0,0,0
CLASS "DLGCLASS"
STYLE 0x90800000
EXSTYLE 0x00000008
BEGIN
END

Hopefully this may save some time to people looking for answers. It's a tad long, but I wanted to detail it as much as possible.

Regards.

mkey
  • 535
  • 2
  • 12
4

Look at using WS_EX_COMPOSITEDand WS_EX_TRANSPARENT styles. They provide doublebuffering, altough WM_PAINT will be called when the underlying bitmap is finished drawing, since it draws child controls from bottom to top, so you can paint only in your window procedure. I've used it in the past and work pretty well.

Set your top-level window (container) to extended style WS_EX_COMPOSITED and your child windows with WS_EX_TRANSPARENT. Also, remember to define:

#define WINVER 0x501 

See CreateWindowEx for information on the composited style. This also makes possible to do perpixel transparency on child windows.

UPDATE

What about usign WM_PRINTCLIENT to transfer the client area to a bitmap on a DC and blit all the client area as a whole?

http://blogs.msdn.com/larryosterman/archive/2008/08/27/larry-s-new-favorite-windows-message-wm-printclient.aspx

Hernán
  • 4,527
  • 2
  • 32
  • 47
  • I'll look into it. Might just get the checkmark.. I just need to ensure it works. Unless someone comes up with a solution that works on Win2k.... – Billy ONeal Dec 08 '09 at 15:35
  • Hmm... didn't work. Still flickers. It seems it's double buffering each individual child control individually, but not the child controls themselves as a whole. This, controls underneath the tab control still flicker :( – Billy ONeal Dec 09 '09 at 01:57
  • 1
    Sorry. Are you using WS_CLIP_SIBLINGS and WS_CLIPCHILDREN styles? – Hernán Dec 09 '09 at 03:15
  • I've updated my answer. Check that, I never used WM_PRINTCLIENT but may help you. – Hernán Dec 09 '09 at 03:23
  • 1
    Don't forget Larry's followup post, Herman, where he explains that WM_PRINTCLIENT was an ugly hack that was only masking the problem (while causing others), rather than actually fixing the flicker. http://blogs.msdn.com/larryosterman/archive/2009/09/16/building-a-flicker-free-volume-control.aspx – Ian Boyd Dec 10 '09 at 15:21
  • I didn't know there was a `WS_CLIPSIBLINGS` style -- I was only using `WS_CLIPCHILDREN` which was ineffective because the child controls were not children of the tab control, they were children of my window. That seems to have fixed the issue with the tab control. (So if nobody answers my question you're getting the bounty on this) However, any static controls I have still flicker when resized -- I suspect this is an issue with static controls and I'd still like to be able to double buffer them if possible... – Billy ONeal Dec 10 '09 at 18:51
  • I fixed my ugly multiline edit control flicker (it covers 90% of the window) by simply adding `WS_EX_COMPOSITED` to the container. `WS_EX_TRANSPARENT` is apparently not needed and it caused some weird background problem. – quantum Jun 01 '13 at 04:26
2

Larry Osterman recently blogged about this subject; you might find some interesting details there.

Aaron Klotz
  • 11,287
  • 1
  • 28
  • 22
  • 1
    I'd rather not have to completely re implement the win32 tab control if I can avoid it. – Billy ONeal Dec 08 '09 at 03:47
  • But the key observation there is that the underlying control (Tab control in your case) is causing its children to redraw. Double buffering the individual controls won't help. You either need to make sure they don't get invalidated, or you need a way to buffer all the drawing to the window. Osterman's original approach was to buffer with a WM_PRINTCLIENT hack, but that led to other problems. His final solution didn't involve reimplementing the entire control, just the painting, which is mostly a couple of function calls. – Adrian McCarthy Dec 12 '09 at 00:17
  • The tab control is quite a bit more complicated than the groupbox. It's not "just painting" to get it working correctly. – Billy ONeal Dec 12 '09 at 17:31
0

indeed,

someone posted an answer for this on one of my posts a while ago. Check it out: Here

and be sure to upvote him for being awesome.

The code is C#, hopefully there is a simple translation.

Community
  • 1
  • 1
Jrud
  • 820
  • 3
  • 7
  • 21
  • 2
    No. Controls in .NET have nothing to do with controls in Win32. – Billy ONeal Dec 03 '09 at 19:45
  • you didn't say anything about it being win32 when you wrote the question. and Pascal Couq added the tags after I posted. – Jrud Dec 03 '09 at 19:48
  • 3
    And if you notice the link, it goes to the Win32 common controls page. I also note that .NET doesn't call them "common controls". – Billy ONeal Dec 03 '09 at 20:30
  • Additionally (to Jruds comment) the solution provided on the linked page actually sets WS_EX_COMPOSITED... the very solution everyone else gave... Since the OS is windows, and it's not a common controls issue, but rather an issue with how windows redraws, his suggestion was valid... – Jason D Dec 12 '09 at 13:34
0

Without knowing exactly what you're doing, I assume you're using either MFC or Win32 C for this.

You probably want to do the resize on WM_SIZE message. I'm not sure where you're resizing the controls, but I think you're doing it while it's being resized, which is why it's causing the flicker.

Also, I'm thinking but I don't know, you could probably use the SetWindowPos function, and for the uFlags, have SWP_NOREDRAW. Though I'm not sure how great this would work for common controls.

Daniel
  • 75
  • 1
  • 5
  • Tab controls (my window is essentially one big tab control) need to be redrawn then resized. That's why I need the double buffer in the first place. – Billy ONeal Dec 08 '09 at 03:44
0

You can create a memory device context, MemDC at the very start. Draw everything into the MemDC, then when the window recieves a WM_PAINT message or is invalidated, copy the MemDC to the real DC by bit blitting.

I remember reading up the technique by Herbert Schildt in his book a few years back (Windows 98 Programming from the Ground Up). In that way, all the redrawing is faster as you blit the memory dc to the real dc. But the one big gotcha, was how big the memory dc you want to use! But he showed how to do it. There is a code download for all of the chapters in that book published by Osborne McGraw Hill.

Hope this helps, Best regards, Tom.

t0mm13b
  • 34,087
  • 8
  • 78
  • 110
0

You're not using WS_EX_TRANSPARENT, are you? That will cause underlying windows to be painted before the controls, and when the bottom window erases you get flicker.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
0

We use WTL::CDoubleBufferImpl mix-in for that purpose. We even draw stuff with GDI+ over. Zero flickering.

Usage is pretty simple: you just public-ly inherit from WTL::CDoubleBufferImpl<YourClass>, and chain it in the ATL message map.

Ivan Krechetov
  • 18,802
  • 8
  • 49
  • 60