日期:2009-03-16  浏览次数:20393 次

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 many times in succession.

Solving this problem is very easy with the .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 blit 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.

The only drawback to this technique is the greater use of resources. However, most controls are relatively small in screen size and so this is unlikely to be an issue.

Embedding bitmaps in your manifest
Usually a bitmap will be embedded into your assembly automatically, just by being associated with a property of a Form using the designer. But there are many circumstances where you may want to embed a bitmap (or other resource) without associating it with any particular Form .

You can do this in Visual Studio .NET by right-clicking the project of interest and selecting the 'Add Existing Item' option. Navigate to your bitmap and selecting it will make it appear in your project details. Now select the bitmap and modify the 'Build' property to become 'Embed as resource'. Rebuild your project and then use the ILDAsm.exe utility to examine the manifest for your built assembly. You will now see that the bitmap has been added as a resource.

To extract this resource at runtime is not obvious with only involves three steps.


    // Get the assembly we are built into
    Assembly myAssembly = Assembly.GetAssembly(Type.GetType("MyNamespace.ThisClass"));

    // Get the resource stream containing the embedded resource
    Stream imageStream = myAssembly.GetManifestResourceStream("Example.bmp");

    // Load the bitmap from the stream
    Bitmap pics = new Bitmap(imageStream);


The first line of code is used to get a reference to the assembly this code is built into. In your own code you should substitute the 'MyNamespace.ThisClass' string for the fully qualified name of the class the code is inside.

The second line requests from the assembly a stream that contains the contents of the named resource. This name will need to match the name that appears when using the ILDAsm.exe utility. Visual Studio