Tips and Tricks
Contents
Flicker free drawing in controls
Embedding bitmaps in your manifest
Creating type safe collections the quick way
Flicker free drawing in controls
You have just spent several days writing a beautiful looking custom control but are faced with one last problem that is spoiling the whole effect. whenever the control is redrawn it flickers. this is most obvious when the control is being resized and so redrawn when times in succession.
Solving this problem is very easy with. NET Framework. if you come from a C ++ and GDI background then the solution was to create a memory based bitmap, draw the control onto the bitmap and then bits this to the screen (otherwise known as double buffering ). this is such a common requirement that the UserControl class actually implements this functionality for you. all you need to do is include the following two lines of code into your class constructor.
SetStyle (ControlStyles. DoubleBuffer, true );
SetStyle (ControlStyles. AllPaintingInWmPaint, true );
The first line will request that double buffering be used whenever the OnBackground or OnPaint methods are called. this will reduce then amount of flicker but may not remove it completely as painting the whole control still results in two separate blitting operations.
The second line of code above is used to ensure that only a single blitting operation occurs when drawing. this occurs when the underlying windows WM_PAINT message is processed. when this happens it will create the memory bitmap, call the OnBackground method, call the OnPaint method and then finally blit then result to the screen.