The ProblemIf you ever use default WinForms TreeView or ListView controls you already notice massive flickering when control size changes (for example when you change form size with mouse). In this article I will try find workarounds for this behavior and eliminate flickering by using double buffering. When I first notice this problem, I was made some googling and didn't find any solutions, so I have invented several solutions by myself. First Attempt with TreeViewLet me first say, why this problem occurs. In general control painting cycle consist from two phases: erasing and painting. First Windows send message to erase background, then to paint. When control receives erase message if fill control surface with solid color, then control draw content on the surface. When user changes form size, Windows invokes many painting cycles, and in every cycle erase and painting occurs, but because painting occurs with delay after erasing, user will see noticeable flickering. So my first idea was to eat erasing messages and exclude tree view labels from painting cycle by using clipping region. To do this I override default WndProc method, catch WM_ERASEBKGND and WM_PAINT messages. This solution works well for me for about a year, but some parts still flicker (tree lines, buttons and icons), and sometimes (rarely) selected node painted with glitches. protected override void WndProc(ref Message m) Second AttemptWhen I start moving my project to Windows Vista, I have found that Microsoft was implemented native double-buffering, and my task is only to enable such functionality. To do this I will send TVM_SETEXTENDEDSTYLE message with TVS_EX_DOUBLEBUFFER style after control windows handle creation. This works very well but only under Windows Vista. public DbTreeView() Third AttemptAfter implementing native double-buffering under Vista, I start thinking about older systems, especially Windows XP, because it has large user base. So I have returned to my first attempt and start investigation. First I was try to substitute owner draw hdc, by overriding NM_CUSTOMDRAW message, but without luck (it seems than Windows do not detect handle substitution and ignores it). Then I remembered about WM_PRINT message. This message used to draw control on the user supplied surface and usually used for printing. But why not use this message to draw control on bitmap, and then draw this bitmap in the paint cycle? First version uses background bitmap for offscreen painting, and it works! After proving concept I started optimization process, and bitmap was replaced with .NET BufferedGraphics class. This class is specially created for implementing double-buffering in controls. protected override void OnSizeChanged(EventArgs e) Final SolutionNow I have working solution, but I want to make it more elegant and with less native interoperability. After additional investigation I have decided to use internal Control logic to do this. Control class has UserPaint style and this style means that all painting occurs in user code, and underlying control painting is not invoked at all. So I have set UserPaint style, as well as OptimizedDoubleBuffer and AllPaintInWmPaint (these styles enable internal control double buffering support). After setting this styles all I need is to override OnPaint control and simply paint control using WM_PRINT message. Sounds good, but not works, because control with UserPaint style intercept not only default painting but also default printing. So instead of sending WM_PRINT message, I have created dummy WM_PRINTCLIENT message and dispatch it directly to default window procedure. public DbTreeView() ListViewThe same approach is valid for ListView, with one exception, native double buffering is available for ListView starting from Windows XP. And even more support for native double buffering are exists in .net 2 ListView control, all I need is to enable double-buffering. public DbListView() For older systems I have used exactly the same solution as for tree view. Known Issues
Downloadsdb-tree-list-view.zipDbTreeView and DbListView sources, as well as small demo. |
Articles >