Wyo VB - Ch. 7 Notes
Objective #1: Understand and use the
typesetting and computer display concepts associated with the monitor and hardcopy
displays.
- Graphics on a computer screen are formed by arranging pixels (picture elements).
A pixel is the smallest graphic element that can be displayed on the screen,
however different types of monitors have different capabilities in terms of
displaying graphics. Some monitors can display 1024 pixels horizontally and
768 pixels vertically (SVGA resolution). Others can only display a screen
that is 640 by 480 pixels (VGA resolution).
- Since images may look different and distorted on monitors of different resolutions,
Visual Basic uses a standard measure called a twip. The twip is the
default standard measure of graphic elements. A twip is 1/20 of a point, where
a point is 1/72 of an inch. Points were widely used by printer's as a unit
of measure long before computer graphics or even computers. Font sizes on
the computer screen are measured in points. For example, a 12 font size means
12 points. Since you know that a twip is 1/20 of a point and a point is 1/72
of an inch, it can be calculated that there are 1440 twips in an inch.
- Because of the nature of the technology that displays graphics on a computer
screen, a inch on the screen (called a logical inch) is actually slightly
longer than a printed inch depending on the monitor's screen solution. This
is partly due to the fact that the resolution of a printed page on any type
of printer is MUCH better than the resolution on a screen. Did you ever notice
that a picture in a magazine is always more crisp and detailed than that on
modern computer screens? You see laser printers can often print at over 1000
dots per inch, dpi whereas a computer can usually only display about 96 pixels
per logical inch.
Objective #2: Initialize and use user-defined coordinate
systems appropriate to application programs.
- Since a form's default Height and Width properties are probably oddball
values such as 3600 twips for height and 4800 for width. It is convenient
to relabel the imaginary horizontal and vertical axes with numbers that are
easier to work with. You can use the form's ScaleMode, ScaleHeight, and ScaleWidth
properties to do this.
- You may set the ScaleMode property of a form to vbUser (or 0) rather
than the default vbTwips (or 1). Then you can set the ScaleHeight and
ScaleWidth properties to numbers that are easier to work with such
as 100 or 1000.
- The default window coordinates do not work similarly as the typical mathematics
graph. In Visual Basic's coordinate system (and most programming graphics
environments) the y-values range from 0 to 100 as you follow down the
screen or form from the upper-left corner.
Objective #3: Use the Circle and Line form methods.
- The Circle
method is a built-in procedure used to draw circles on certain objects such
as Picture Boxes, forms, or the Printer objects.
- The syntax to be used for this method is
object.Circle (x, y), radius
where object is a form, a shape control, a picture box or other objects that
support the Circle method. The center of the circle is the point (x, y) and
the radius is the desired radius of the circle.
For example,
frmMain.Circle(300, 400), 200
would draw a circle on form frmMain with the center 300, 400 and radius of
200.
- The Line
method is a built-in procedure that draws a line on certain objects and forms.
The syntax to be used for this method is:
frmMain.Line (x, y) - (x2,
y2) OR
frmMain.Line - (x, y)
where a line is drawn on the form between the points (x, y) and (x2, y2) in
the first example. A line is drawn from the last point drawn to the point
(x, y) in the second example.
- For example,
frmMain.Line(300, 400) - (1000, 1200)
would draw a line on frmMain from the point 300, 400 to the point 1000, 1200.
Objective #4: Use the RGB color function.
- The RGB function can be used to assign any customized color
to certain properties of various objects. This allows you to use a multitude
of colors besides the VB color constants such as vbRed, vbBlue, etc.
- For example, you can set the background of a form to a unique color with
the statement
frmMain.BackColor = RGB(50, 23, 99)
- The RGB function takes 3 arguments. All three arguments must be integers
in the range 0 to 255. The first argument refers to the amount of red, the
second refers to the amount of green and the fourth refers to the amount of
blue.
- To create the color black use the function call RGB(0, 0, 0). To create
the color white, use the function call RGB(255, 255, 255).
Objective #5: Use the Shape control.
- A shape control can be used to place a graphic on a form.
- Use the prefix shp for shape objects.
- Shape controls do not respond to events such as Click.
Objective #6: Use Picture Box and Image controls to add graphics to
a VB program.
- Almost any kind of graphic format including .bmp, .gif, .jpg can be displayed
within a Picture Box control. After placing the Picture Box control
on a form, set its Picture property to the any graphic file on your hard drive.
The prefix pic is used for Picture Box objects.
- An Image control can also display a bitmap graphic through its Picture
property. The prefix img is used for Image objects. The purpose of an Image
control is only to display an image. It does not have nearly as many useful
properties and methods and therefore cannot be used as flexibly as a Picture
Box. However, an Image object is more resource-efficient (i.e. memory, etc.)
for this reason and should not be overlooked.
- To load a new image in an Image control at runtime, use the LoadPicture
function like this:
imgGraphic.Picture = LoadPicture("Z:\Basic\Ch7\Ch7Proj1\images\spaceinvader.jpg")
where the path of the actual graphic is the parameter of the VB LoadPicture
function or simply
imgGraphic.Picture = LoadPicture("spaceinvader.jpg")
if the graphic file is located in the same folder as your project.
To clear an image from an Image control, use the statement
imgGraphic.Picture = ("")
with the null string as the parameter.
Objective #7: Animate graphics on a form and use collision detection.
- You can achieve animation by simplying changing a Shape, Picture Box, or
Image control's Top or Left property within a loop.
- For example, the following loop would animate the Shape object shpObject
horizontally across a form (assuming that it is initially positioned along
the left edge of the form):
For J = 1 To (frmMain.ScaleWidth - shpObject.Width)
shpObject.Left = shpObject.Left + 1
Next J
- However, Shape objects have a Move method as well that can be used
to animate the object across the form.
- Collision detection is code that responds to graphics when they touch
each other or a boundary of the form. For example, the code
Private Sub Form_Activate()
Form1.ScaleMode = 0
Form1.ScaleWidth = 100
Form1.ScaleHeight = 100
Form1.Line (0, 20)-(100,20)
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single,
Y As Single)
If (shpCircle.Top <= 20) Then
MsgBox "Collision Occurred"
End If
End Sub
could be used to detect a collision between the shpCircle object and the line
drawn on the form.
- The following code could also be used to detect a collision by checking
to see if the shpCircle object "straddles" the line
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single,
Y As Single)
If (shpCircle.Top > 20 And shpCircle.Top + shpCircle.Height
> 20) Then
MsgBox "Collision
Occurred"
End If
End Sub
Objective #8: Create menus with the menu editor.
- Professional-looking menus can be added to a VB project with the
menu editor. You can access the menu editor by choosing the Tools/Menu
Editor... menu command.
- If a menu command object's Checked property is set to True, then
a checkmark will appear to the left of the menu item. If it is set to False
(which is the default), then a checkmark does not appear.
- If a menu command object's Enabled property is set to True (the default),
then the menu command can be chosen by the user. If it is set to False, then
the menu command will be "grayed out" so tha the user knows that
he cannot choose that option.
- The Checked and Enabled properties of menu commands can be used to give
feedback to the user and enhance the interface of your program. You should
always use these properties with menu items when appropriate. Users appreciate
it when menu commands are enabled only when they are appropriate choices.
If you notice, most commercial Windows applications follow this convention.
- Often, limiting the user's to choosing appropriate commands prevents the
programmer from having to test for illegal user inputs. Leaving clues and
even error messages for the user explaining why certain commands are indeed
inappropriate provides a better experience for the user. Things such as checkmarks
placed next to previously selected menu items enhances the user interface
of a program as well.
- A shortcut (popup) menu can be created by simply setting the top-level
menu item's Visible property to False. Also, use the following code with the
Form_Mouseup event:
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single,
Y As Single)
If (Button = vbRightButton) Then
frmMain.PopupMenu
mnuShortcut
End If
End Sub
The Form's PopupMenu method is being used to display the menu command
object mnuShortcut. The mnuShortcut's Visible property is set to False, however
its menu command items are visible. The Button = vbRightButton is being used
to tell whether the user's right-mouse button was clicked or not.
Objective #9: Use a scrollbar as a means of input to a program.
- There are two different scroll bar controls, the horizontal scrollbar
and the vertical scrollbar. They work similarly except the one is horizontal
and the other is vertical. Their prefixes are hsb and vsb.
- Scrollbars respond to their Change event. As the user moves the scrollbar,
the Value property is updated for the scrollbar. By placing code in the scrollbar's
Change event, you can make your program respond by the user's movement of
the scrollbar.
- It is sometimes useful to use the value of the scrollbar as the subscript
for an array.
- See Ch. 7 Demo #3 for an example of scrollbars.
Objective #10: Be able to use the three form events MouseDown, MouseUp,
and MouseMove as well as a form's KeyPress & KeyDown events.
- A MouseDown
event occurs when the user presses the mouse button somewhere on the form.
- A MouseUp
event occurs when the user releases the mouse button. Note that the MouseUp
event may occur in the very next instant after its corresponding MouseDown
event if the user clicked the mouse quickly. Or, if the user decides to hold
down the mouse button for a long time or he/she drags the mouse across the
screen, the MouseUp event will occur much later than its corresponding MouseDown
event.
- A MouseMove
event occurs when the mouse's position changes.
- See Ch 7 Demo #5 for an illustration of
the MouseDown, MouseUp, and MouseMove events.
- A form's KeyPress and KeyDown events allow your program to respond to presses
of various keys as long as the Form has the focus.
- See Ch. 7 Demo #6 for an illustration of
a program that uses the form'w KeyPress event.
- See Ch. 7 Demo #7 for an illustration of a program that uses the form's
KeyDown event along with the arrow keys on the keyboard.
Objective #11: Use the Rnd function to generate random numbers within
desired ranges.
- You can use the built-in VB function Rnd to generate random numbers. The
Rnd function returns a decimal number between (but not including) 0 and 1.
- Using the statement
intRandomNumber = Int (Rnd * (Hi - Lo + 1)) + Lo
you can generate a random whole number between and including the values substituted
for Lo and Hi and store that random number in the variable intRandomNumber.
- Use the Randomize statement first though in the Form_Load event.
This "seeds" the VB random number generator so that your program
doesn't generate the same random numbers every time it executes.