Professional Documents
Culture Documents
Assessments
Exercise 8
Multiple-Choice Quiz 9
4.1.1 Co-Ordinate Systems And Object Placement
Left….top property
Twips
Inch 将英寸指定为度量单位。
Millimeter 将毫米指定为度量单位。
Pixel 将设备像素指定为度量单位。
Point 将打印机点( 1/72 英寸)指定为度量单位。
World 将世界坐标系单位指定为度量单位。
Hierarchical Co-ordinate system
The Top and Left properties of a control
are expressed relative to the container
E.g., (0,0) set control will be at the top-left corner of the
container
container object uses its own coordinate
system.
the positions of objects placed within the
container are expressed relative to that (local)
origin not the global origin at the top-left of the
screen
Also be called as nested coordinate system.
Hierarchical Co-ordinate system is convenient
when the container moves, need no
recalculating all their internal coordinate
values
whenever reported and used ,x,y positions
are expressed in the coordinate system
(local) of the object involved.
How get x,y position ?
when mouse button was pressed
x,y position of the mouse when its button was pressed:
Private Sub name_MouseDown(ByVal sender As Object,
ByVal e As System.Windows.Forms.MouseEventArgs)
the MouseEventArgs parameter e to the event handler
provide the x,y position of the mouse when its button
was pressed
e.X
e.Y
The x,y values are expressed relative to the origin (top-
left corner) of the object handling this event.
It is convenient to
use the coordinates to find internal parts of the control
use the user input as the basis for drawing.
How to convert coordinate systems
It is necessary to convert an object’s
coordinate system
from the local to global
from the global to local
Local
localX = Button2.Width/2
localY = Button2.Height/2
global
globalX = localX + Button2.Left + FrameB.Left
+ FrameA.Left
globalY = localY + Button2.Top + FrameB.Top
+ FrameA.Top
To convert coordinate systems
to convert this global coordinate into the
coordinate system of one object
newLocalX = globalX - FrameA.Left -
Backdrop.Left
newLocalY = globalY - FrameA.Top -
Backdrop.Top
Code of Conversion function
the local coordinate system the global
coordinate system.
first parameter : the
control object,
second parameter : and the x or y coordinate
Public Function globalX(insideObj As Object, localX As Single)_
As Single
Dim result As Single
Dim walkObj As Object
' start with the local position
result = localX
' starting with the control whose coordinate system
' this value is expressed in
Set walkObj = insideObj
' walk up control/container tree until we get to the
' form at the top
While Not (TypeOf walkObj Is Form)
' add offset for that coordinate system
result = result + walkObj.Left
' move to the next container up
Set walkObj = walkObj.Container
Wend
globalX = result
Public Function globalY(insideObj As Object, _
localY As Single) As Single
Dim result As Single
Dim walkObj As Object
result = localY
Set walkObj = insideObj
While Not (TypeOf walkObj Is Form)
result = result + walkObj.Top
Set walkObj = walkObj.Container
Wend
globalY = result
End Function
Conversion function
the global coordinate system the local
coordinate system.
' Convert a global x coordinate value to the equivalent
' x position in the local coordinate system of the given
' control object.
Public Function localX(insideObj As Object, _
globalX As Single) As Single
Dim result As Single
Dim walkObj As Object
result = globalX
Set walkObj = insideObj
While Not (TypeOf walkObj Is Form)
result = result - walkObj.Left
Set walkObj = walkObj.Container
Wend
Return result
End Function
' Convert a global y coordinate value to the equivalent
' y position in the local coordinate system of the given
' control object.
Public Function localY(insideObj As Object, _
globalY As Single) As Single
Dim result As Single
Dim walkObj As Object
result = globalY
Set walkObj = insideObj
While Not (TypeOf walkObj Is Form)
result = result - walkObj.Top
Set walkObj = walkObj.Container
Wend
Return result
End Function
4.1.2 Drawing Primitives (VB6)
FillStyle
Picture Box-description (VB6)
Simple graph : using Line and Shape controls
complex drawing : it is often easier to write specific code
to create the drawing within a PictureBox control.
The PictureBox control is created using the toolbox icon
shown
Note : the difference between the Image control and the
PictureBox control
Picture Box-Properties (VB6)
CurrentX, CurrentY
determine the current drawing position.
BackColor, ForeColor, FillColor: determine the color of
the next drawing output.
BackColor indicates the background color of a drawing primitive.
ForeColor determines the foreground color for drawing.
FillColor determines the color of the interior of filled primitives
such as filled rectangles and circles.
Colors can be specified at design time by interactively picking
them from a palette or named list of colors available in the
property editor
To specify colors at run time, use
one of a set of predefined constants
special hexadecimal color codes
Color Const (VB6)
Constant Name Color Value
vbBlack &h000000
vbRed &h0000FF
vbGreen &h00FF00
vbBlue &hFF0000
vbYellow &h00FFFF
vbMagenta &hFF00FF
vbCyan &hFFFF00
vbWhite &hFFFFFF
Picture Box-Properties (VB6)
FillStyle
determines whether drawing primitives are created with
interior area filled in (1-Solid),
left blank (0-Transparent),
some other filling effect (such as 4-Upward Diagonal which
produces diagonal fill lines oriented with their left side higher
than their right).
If FillStyle is not set to 0-Transparent, then the current FillColor
value determines the color of the lines or area used for filling.
If FillStyle is not 1-Solid, then some of the background color (set
by BackColor) will show through.
Picture Box-Properties (VB6)
DrawWidth
determines the width of lines and borders drawn on objects.
This value is always expressed in pixels (not twips).
DrawStyle
determines the line style used for drawing lines and edges.
0-Solid,
1-Dash,
2-Dot,
3-Dash Dot,
4-Dash Dot Dot,
5-Transparent.
Note that all nontransparent lines which have DrawWidth > 1 will
be drawn solid
Picture Box-Properties (VB6)
Font
determines the font used for the next text drawing
(print) operation.
It holds a StdFont object having a series of
properties including:
Name
Size
Bold
Italic
Underline
Picture Box-Properties (VB6)
E.g., to create and use a new font object,
code as following is used:
Dim myFont As New StdFont
myFont.Name = "Arial"
myFont.Size = 14
myFont.Bold = True
Set myPictureBox.Font = myFont
Note : the Font property is normally
assigned using the Set command rather
than the conventional assignment
Why? because it is an object reference
PictureBox Methods (VB6)
drawnObject.Cls
clears the PictureBox to its background color
sets CurrentX and CurrentY to 0,0
Notice: no parameters
PictureBox Methods (VB6)
DrawLine
DrawRectangle / FillRectangle
DrawEllipse / FillEllipse
DrawPolygon / FillPolygon
DrawImage
DrawString
Graphics Description
Visual Basic provides a sophisticated
Graphics tool kit that offers the ability to
create custom graphical displays.
Though drawings can occur anywhere in an
interface, in most cases you'll want to
display custom graphics within a
PictureBox control.
Doing so can keep your code clearer,
clearly delimits the location of your drawing
when using the design view.
Graphics Description
The Graphics class offers a variety of methods to
draw primitive shapes:
lines, arcs, rectangles, ellipses and polygons.
these primitives can be drawn as simple line shapes or as
filled solids.
also offers the ability to draw images from existing
image files
all of the methods are overloaded
offer the programmer a variety of ways to express the
coordinates necessary to draw the shape.
Personal preference and the needs of the specific
application that is being developed will determine which
specific representation you'll use.
demonstrate
DrawingExample application.
The application has 6 relevant controls:
a PictureBox to provide a drawing surface,
Private Sub
drawPolygonExample(ByVal e As
PaintEventArgs)
Dim bluePen As New
Pen(Color.Blue)
Dim polyPoints(4) As
Point
polyPoints(0).X = 1
polyPoints(0).Y = 1
Methods
DrawImage
displays an Image or part of an Image at a
given location.
DrawString
displays a text string
the String, Font and Location are passed to
the method as parameters.
Sample code
creates a String, a Font and a Brush and then
draws the string at coordinate 25, 30.
Private Sub drawStringExample(ByVal e _
As PaintEventArgs)
Step 3 : drawing the hands of the watch.
Drawing a watch with stationary hands is very simple
use some DrawLine calls and provide the necessary absolute coordinates.
The watch needs to track the time
these hands need to be drawn over and over again.
As such, it would be useful to have access to some methods that
provide the appropriate coordinates for the watch hands.
These coordinates should be based upon the current time.
To that end, we will next create two methods that will provide the
X and Y coordinates for an individual hand.
These values will be based upon the hands current value and the
intended length of the hand.
It's not necessary to have a perfect understanding of the
calculations that are used to obtain the coordinates;
you should recognize the role of the totalIncrements variable.
Step 4 : drawing the actual hand
The drawHand subroutine is the longest
component of the code, but despite the
length, it's not particularly complex.
Each hand will be a polygon with four
points
these coordinates will be held in a Point array
and passed to the FillPolygon routine;
this polygon will then be outlined in black,
Undo Capability
A Simplified Undo System
Exercise 8
Exam 3
Exam 3 Multiple-Choice
Exam 3 Practical
The final exam
Take the certification exam
Certification Exam Multiple-Choice