Computer Science 110
NAME: _________________________
NOTES: Graphics & Animation (module 16)
Adding Graphics to Your Applications
Colours and graphics are used to make applications easier to understand and more interesting. Images can be simple
graphics like circles, squares and ovals or they can be much more complex.
In this module, you will:
Use a picture box object to place images and draw graphics on your forms.
Use the timer object to include simple animation in applications.
Understanding Color
Each tiny dot on your monitor is actually composed of three smaller dots—one red, one green and one blue. Red, Green
and Blue (RGB) values are used to define the colors you see on your monitor.
To display a color, you must vary the brightness of each of the three dots. For example, the color purple is displayed when
the red and blue dots shine brightly and the green dot is dim. To display white, all the dots must be bright and to display
black, all the dots must be turned off.
The programmer can set the colour by using RGB values or by using Visual Basic color constants. Using the RGB
values allows the programmer to display more color choices.
Visual Basic has an RGB built-in function. This function can be used at run time, in an assignment statement, to change a
color property. The following is an example:
Private Sub UserForm_Initialize()
Me.BackColor = RGB(255,0,0)
End Sub
In this statement, red, green and blue are integer values. The values may range between zero (0) and 255 to represent
the brightness of the red, green and blue dots, which make up the colour. The above statement changes the background
color of a form to red.
Visual Basic also has predefined constants specifying RGB number values of some of the
more standard colors. You can use these values to set the color of an object instead of using
the RGB values. The following is a list of Visual Basic color constants:
The assignment statement below changes the background color of a form to blue when the form is loaded:
Private Sub Form_Load()
Me.BackColor = vbBlue
End Sub
The BackColor property changes the background color of an object such as a form.
Working with Color at Design Time
No doubt you will agree that color enhances an interface and makes it more interesting. Many of the objects you add to
the interface have color properties.
An object's color is changed at design time using the BackColor and ForeColor properties.
BackColor changes the background color of an object.
To use BackColor:
Double-click the BackColor property in the Properties window and select the Palette tab to display the available
ForeColor changes the color of the text displayed on an object.
To use ForeColor:
Double-click the ForeColor property in the Properties window and select the Palette tab to display the available
Note: Command buttons do not have a ForeColor property.
Working with Image Control
To place image, choose the Image control. An image object can only contain an image. The image object does not allow
you to edit the contents of the picture.
A picture box can display images in the following formats: BMP, CUR, GIF, JPG, WMF and ICO.
The following table shows the properties that can be set for a Image Control:
This identifies the object.
Good style uses the prefix img in front of the name.
Used to display a dialogue
box for selecting the image to display in the picture area
0 -fmBorderStyleNone or 1 -frmBorderStyleSingle
This can be either True or False. When it is set to True, the image
is resized to the same size as
the graphic.
Can be set to True or False to display or hide the image
Visual Basic has a built-in function called LoadPicture. This function allows the programmer to change the picture at run
time. This is done with an assignment statement using the following syntax:
The image that is used in the image control can be from a file of images that you have stored on your computer.
Moving Images
A picture box (just like a form) can serve as a container for shapes and graphics. The picture box can be positioned
within the form container and shapes can be positioned within their picture box container. The properties in the table
below are used to position both the picture box on the form and to position a shape, such as a circle, inside the picture
box itself.
To position the picture box on the form, select the picture box and adjust the properties as required. To position a shape
inside the picture box, select the shape and adjust the properties as required.
Changes the distance between the left edge of the object and the
left edge of its container
Changes the distance between the top edge of the object and the
top edge of its container
Changes the width of the object
Changes the height of the object
The distance an object is moved is expressed in units called twips.
567 twips = 1 centimetre
The Move method is used at run time to move a image object to a new location. The syntax for the Move method is as
In the example:
imgHappyFace.Move imgHappyFace.Left-20
the image object called HappyFace is moved 20 twips to the left of its current position. That means if the image is now at
100 twips from the left edge of its container, it will be moved -20 twips and will be repositioned 80 twips from the left edge
of its container.
DoEvents allows the operating system to process events while a VBA program is running. Some instances in which
DoEvents may be useful include the following:
Hardware I/O
Delay Loops
Operating System Calls
DDE Deadlocking
Yields execution so that the operating system can process other events. In this course we will only use DoEvent to allow
use to do animation.
Creating Timing Delays
Sometimes you may want sections of code to run at an interval. In Excel VBA there is not a timer object like there was in
Visual Basic 6.0. However, you can recreate the timer in the code window. For example, many word processors contain
code that executes every ten minutes to automatically save your document. In Excel VBA you can use the timer code to
simulate an animation.