375
Chapter 15
Adding Graphics
and Animation Effects
After completing this chapter, you will be able to:
n Use the System.Drawing namespace to add graphics to your forms.
n Create animation effects on your forms.
n Expand or shrink objects on a form at run time.
n 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 2010 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 artwork on a form using the System.Drawing
namespace, 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 and change
a form’s background image and color. 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 electrocardiogram. If this isn’t your idea of fun, you might decide on a more
modest goal: 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
Microsoft 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!
Table of Contents
Adding Graphics
and Animation Effects............................... 375
Adding Artwork by Using
the System.Drawing Namespace.................................... 376
Using a Form’s Coordinate System............................... 376
The System .Drawing .Graphics Class............................. 376
Using the Form’s Paint Event.................................... 378
Adding Animation to Your Programs.................................. 380
Moving Objects on the Form.................................... 380
The Location Property......................................... 381
Creating Animation by Using a Timer Object...................... 382
Expanding and Shrinking Objects While a Program Is Running........... 386
One Step Further: Changing Form Transparency........................ 388
Chapter 15 Quick Reference.......................................... 390