Chapter 15
Adding Graphics and Animation Effects
After completing this chapter, you will be able to:
Use the System.Drawing namespace to add graphics to your forms.
Create animation effects on your forms.
Expand or shrink objects on a form at run time.
Change the transparency of a form.
For many developers, adding artwork and special effects to an application is the most exciting—and addictive—part of programming. Fortunately, creating impressive and useful graphical effects with Microsoft Visual Basic 2005 is both satisfying and easy.
In this chapter, you'll learn how to add a number of visually interesting features to your programs. You'll learn how to create compelling artwork on a form, how to create simple animation effects by using PictureBox and Timer controls, and how to expand or shrink objects at run time by using the Height and Width properties. You'll also learn how to change the transparency of the form. When you've finished, you'll have many of the skills you need to create a visually exciting user interface.
What will you be able to do on your own? This is the point when your imagination takes over. One of my favorite results is from a reader of a previous version of this book who used what he had learned about Visual Basic and graphics to build his own electrocardiograph machine, complete with analog circuitry and a Windows form displaying digital data from the homemade EKG. If this isn't your idea of fun, you might more modestly decide to enhance your application's start page so that it contains custom artwork and visual effects—perhaps in combination with one or more digital photographs loaded into picture box objects on a form.
Even game programmers can have some serious fun using graphics in Visual Basic and Visual Studio. However, if you're planning on creating the next version of Microsoft Zoo Tycoon or Microsoft Halo, you had better plan for much more than visual output. Modern video games contain huge libraries of objects and complex formulas for rendering graphical images that go well beyond the scope of this book. But that still leaves a lot of room for experimentation and fun!
Upgrade Notes: Migrating Visual Basic 6 Code to Visual Basic 2005
If you're experienced with Microsoft Visual Basic 6, you'll notice some new features in Visual Basic 2005, including the following:
In Visual Basic 6, you use the Line and Shape controls to create simple lines, rectangles, and circles on your forms. In Visual Basic 2005, no drawing controls are provided in the Toolbox. Instead, you're encouraged to use the GDI+ graphics services directly through the System.Drawing namespace.
The Visual Basic 6 keywords Circle, Line, and PSet have been replaced, respectively, by the DrawEllipse method, the DrawLine method, and the Point structure in the System.Drawing.Graphics class.
The coordinate system in Visual Basic is now pixels rather than twips.
In Visual Basic 6, many controls can be relocated, or animated, on the form by rapidly calling the control's Move method. Visual Basic 2005 controls don't have a Move method, but they can still be relocated quickly by updating the control's Left, Top, or Location property or by using the SetBounds method.
Visual Basic 2005 controls continue to support drag-and-drop effects, but they handle them in a different way. For example, although Visual Basic 2005 continues to support the DragDrop event, the DragIcon and DragMode properties are no longer available.
Microsoft Visual Studio 2005 can work with more image formats than Visual Basic 6 can. In particular, the System.Drawing.Imaging namespace contains functions to work with the following image formats: BMP, EMF, EXIF, GIF, Icon, JPEG, MemoryBMP, PNG, TIFF, and WMF.