Animating Pictures

Windows doesn’t provide much direct support for animation or transparent images. But it does provide everything you need to create your own, including a model: icons. From the outside, an icon looks like an image on a transparent background. Internally, an icon is simply a bitmap with a corresponding mask. Once you understand how the GDI module uses masks to make icons look transparent, you can make your own transparent images, of any size.

When you create an icon with an editor such as IconWorks, you create only the pixel data. The tool automatically creates an AND mask and an XOR mask and puts both (with a header) into an ICO file. Eventually the icon masks are loaded into memory and become accessible through an icon handle (HICON). Programs can then call DrawIcon, which carves a hole in the background surface in the shape of the mask and puts the image into that hole. You must do essentially the same thing to turn any bitmap into an icon. From there, it takes only a few more steps to make your “icon” into a sprite—that is, a moveable image suitable for animation.

I wrap this functionality up in two ways. First, the CPictureGlass class does it the hard way. We’re going to take a long look at the details. Second, XPic­tureGlass wraps it up the easy way. It takes advantage of the MaskPicture and MaskColor features of the UserControl object to create a control that works a lot like CPictureGlass with just a few lines of code. CPictureGlass and XPic­tureGlass each have their advantages. Either way, you get transparent pictures.

I’m using the term animate somewhat loosely. Real animation involves displaying a sequence of transparent images to simu­-late movement. This chapter does only the simplest form of animation—moving a single image across a background. It’s your job to turn the CPictureGlass class into the CMovingPictures collection.