The Last Word
In this chapter you've traveled from the basics of Windows forms-creating them, displaying them, and handling their interactions, to advanced techniques using shaped forms and visual inheritance. In many respects, these new developments hint at two of the themes I take up in the rest of the book. Shaped forms give just a taste of what you encounter in 13, which dive headfirst into GDI+. Visual inheritance provides a preview into some of the custom control designs you explore with control inheritance throughout this book. Both techniques represent the step from ordinary
|
|||||||||||||||||||||||||||||||||
Chapter 6: Modern Controls
Many of the controls you've
In this chapter, you learn about all these controls. More important, you learn the tricks and techniques you need to master them. Custom control classes, one of my favorite themes, returns in this chapter with a few
The ImageList
The ImageList is a special type of collection that holds images of a preset
To create an ImageList at design time, drag it onto your form (it will appear in the component tray). The basic properties for the ImageList are described in Table 6-1.
You can add, remove, and rearrange images using the ListView designer. Just click the ellipsis (
…
)
Dealing with the ImageList in CodeIf you look at the automatically generated code, you'll see that the image files you add are stored in a resource file in your project (as is any binary data added at design time). When the form is loaded, the images are deserialized into Image objects and placed in the collection. A special class, the ImageListStreamer, makes this process a simple one-line affair, regardless of how many images are in your ImageList. This code is inserted automatically by VS .NET, and doesn't need to be modified manually
this.imagesLarge.ImageStream = ((System.Windows.Forms.ImageListStreamer) (resources.GetObject("imagesLarge.ImageStream")));
If you want to have an ImageList object around for a longer period (for example, to use in different forms), you should create it directly in code. You might also want to create Image objects out of graphic files rather than use a project resource. First, you need a variable to reference the ImageList. private ImageList iconImages = new ImageList(); Then, you can create a method that fills the ImageList.
// Configure the ImageList. iconImages.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit; iconImages.ImageSize = new System.Drawing.Size(16, 16); // Get all the icon files in the current directory. string[] iconFiles = Directory.GetFiles(Application.StartupPath, "*.ico"); // Create an Image object for each file and add it to the ImageList. // You can also use an Image subclass (like Icon). foreach (string iconFile in iconFiles) { Icon newIcon = new Icon(iconFile); iconImages.Images.Add(newIcon); }
Once you have images in an ImageList control, you can use them to provide pictures to another control. Many modern controls provide an ImageList property, which stores a reference to an ImageList control. Individual items in the control (like tree nodes or list rows) then use an ImageIndex or similar property, which identifies a single picture in the ImageList by index number (starting at 0). You look at examples that use this technique later in this chapter.
In the meantime, you should also note that the ImageList can be a useful way to store images that you need to use in any scenario. The example that
// Get the graphics device context for the form. Graphics g = this.CreateGraphics(); // Draw each image using the ImageList.Draw() method. for (int i = 0; i < iconImages.Images.Count; i++) { iconImages.Draw(g, 30 + i * 30, 30, i); } // Release the graphics device context. g.Dispose();
As with all manual drawing, these icons are erased as soon as the form is repainted (for example if you minimize and then maximize it). I tackle this issue in Chapter 12. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||