Graphics

Tips >> Visual Basic

MAKE WEB APPLICATION WITHOUT KNOWLEDGE OF CODING? CLICK HERE

 

As well as the shape control, there are three Methods that can be used for producing graphics in Visual Basic.
The three methods are 1. PSet, 2. Line and 3. Circle.
These methods can be used on a Form, PictureBox, or Printer object.

DRAWING LINES:

The Line method has the following syntax.

Object.Line Step (xStart, yStart)-[Step] (xEnd, yEnd), PenColour, BF

 

All options except "Line -(xEnd, yEnd)" are optional.

  • Step makes co-ordinates relative to CurrentX and CurrentY. If omitted, the co-ordinates refer to the underlying object with 0, 0 at the top left.

  • (xStart, yStart) is the start point for the line.

  • PenColour sets the line colour. If omitted, the ForeColor property will be used instead.

  • B will produce a rectangle.

  • F will cause a rectangle to be filled

 

The drawing Object's ScaleWidth and ScaleHeight properties are useful when drawing lines.

' Draw a line horizontally across the centre of the form
Me.Line (0, Me.ScaleHeight / 2)-(Me.ScaleWidth, Me.ScaleHeight / 2)
' Draw a line vertically down the centre of the form
Me.Line (Me.ScaleWidth / 2, 0)-(Me.ScaleWidth / 2, Me.ScaleHeight)
' Draw a line from the top left, to the bottom right
Me.Line (0, 0)-(Me.ScaleWidth, Me.ScaleHeight)
' Draw a line from the top right, to the bottom left
Me.Line (Me.ScaleWidth, 0)-(0, Me.ScaleHeight)
' Draw a small red filled box in the top left of the form
Me.Line (100, 100)-(500, 500), RGB(255, 0, 0), BF

 

 

FREEHAND DRAWING:

The MouseDown event and the MouseMove event are useful for freehand drawing. If no start position is provided for the Line method, it draws from CurrentX and CurrentY which are two system variables that maintain the x and y coordinates for each of the graphics objects.

Initially when MouseDown is raised, we draw a dot with the PSet method. This updates both CurrentX and CurrentY. When the MouseMove event is raised, we draw a line from CurrentX CurrentY to the new mouse position, which in turn updates the system variables CurrentX and CurrentY. The mouse position is passed to both the MouseDown and the MouseMove events. We check the Button status in MouseMove to ensure the button is pressed, otherwise it would draw whenever the mouse is moved.

To try this example, copy the code and paste it into a new project. Run the program, and use the mouse to draw on the form.

Option Explicit
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
     Me.PSet (X, Y)
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
     If Button = vbLeftButton Then
          ' Draw a line from the last position
          ' This gives the impression of freehand drawing
          Me.Line -(X, Y)
     End If
End Sub

 

DRAWING CIRCLES:

The Circle method has the following syntax.

Object.Circle Step (x, y), Radius, Colour, Start, End, Aspect

 

All options except "Circle (x, y), Radius" are optional.

  • Step makes co-ordinates relative to CurrentX and CurrentY. If omitted, the co-ordinates refer to the underlying object with 0, 0 at the top left.

  • (x, y) the centre of the circle.

  • Radius the radius of the circle.

  • Colour sets the arc colour. If omitted, the ForeColor property will be used instead.

  • Start the start point of the arc (in radians, with 0 starting at the 3 O'Clock position - default = 0).

  • End the end point of the arc (in radians, with 0 starting at the 3 O'Clock position - default = 2 * Pi radians).

  • Aspect ratio relative to the height and width of the bounding rectangle of the circle. The default value is 1, a prefect circle. A value less than 1 results in a short fat circle, and a value greater than 1 results in a tall thin circle.

This example draws a short fat circle in the centre of the form with a radius of 1000 twips.

Me.Circle (Me.ScaleWidth / 2, Me.ScaleHeight / 2), 1000, , , , 0.5

Notice that when optional values are omitted, the comma is still used as a placeholder.

 

The next example draws a series of circles and arcs to further illustrate the use of the Circle method. The resulting image is shown below the example.

Const pi As Double = 3.142
Dim r As Integer
' Draw a circle in the centre of the form
Me.Circle (Me.ScaleWidth / 2, Me.ScaleHeight / 2), 500
' Draw the top half of a semi circle,
' 500 twips above centre
Me.Circle (Me.ScaleWidth / 2, Me.ScaleHeight / 2 - 500), 500, , 0, pi
' Draw the bottom half of a semi circle,
' 500 twips below centre
Me.Circle (Me.ScaleWidth / 2, Me.ScaleHeight / 2 + 500), 500, , pi, 0
' Draw the left half of a semi circle,
' 500 twips to the left of centre
Me.Circle (Me.ScaleWidth / 2 - 500, Me.ScaleHeight / 2), 500, , pi / 2, 1.5 * pi
' Draw the right half of a semi circle,
' 500 twips to the right of centre
Me.Circle (Me.ScaleWidth / 2 + 500, Me.ScaleHeight / 2), 500, , 1.5 * pi, pi / 2
' Draw a short fat oval in the centre of the form
Me.Circle (Me.ScaleWidth / 2, Me.ScaleHeight / 2), 500, , , , 0.5
' Draw a tall thin oval in the centre of the form
Me.Circle (Me.ScaleWidth / 2, Me.ScaleHeight / 2), 500, , , , 2
' Set the radius to whichever is the larger,
' the width of the form, or the height
If Me.ScaleWidth > Me.ScaleHeight Then
     r = Me.ScaleWidth / 2
Else
     r = Me.ScaleHeight / 2
End If
' The shape of the form will determine the aspect ratio
Me.Circle (Me.ScaleWidth / 2, Me.ScaleHeight / 2), r, , , , Me.ScaleHeight / Me.ScaleWidth

RUBBERBAND EFFECT

This example illustrates how to use a "rubberband" effect using the Circle method and the DrawMode property. To try it, copy and paste it into a new project.

Option Explicit
' Declare variables that are required in more than one event
Dim centreX As Integer, centreY As Integer
Dim Drawing As Boolean
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    ' Determine centre of circle
    centreX = X
    centreY = Y
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Static oldX As Integer, oldY As Integer
    If Button = vbLeftButton Then
        ' Change the drawing mode to invert whatever is on
        ' the canvas in order to preserve existing drawing
        Me.DrawMode = vbInvert
        ' If we've started drawing a circle, we need to
            ' delete the old circle
            If Drawing = True Then
                ' Calculate the radius of the circle using Pythagoras' theory
                ' In a right angled triangle, the square on the hypotenuse
                ' is equal to the sum of the areas of the squares on the
                ' other two sides. This ensures that the circumference of the
                ' circle passes through the current mouse cursor position
                Me.Circle (centreX, centreY), Sqr((centreX - oldX) ^ 2 + (centreY - oldY) ^ 2)
            End If
        ' Draw new circle
        ' Use Pythagoras to calculate radius
        Me.Circle (centreX, centreY), Sqr((centreX - X) ^ 2 + (centreY - Y) ^ 2)
        ' Store the current mouse coordinates
        oldX = X
        oldY = Y
        Drawing = True
    End If
End Sub
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    ' Change the drawing mode to overwrite whatever is on the canvas
    Me.DrawMode = vbCopyPen
    Me.Circle (centreX, centreY), Sqr((centreX - X) ^ 2 + (centreY - Y) ^ 2)
    Drawing = False
End Sub

PLOTTING POINTS:

The PSet method has the following syntax.

 

Object.PSet Step (x, y), Colour

 

 

All options except "PSet (x, y)" are optional.

  • Step makes co-ordinates relative to CurrentX and CurrentY. If omitted, the co-ordinates refer to the underlying object with 0, 0 at the top left.

  • (x, y) the co-ordinates for the point to be set.

  • Colour sets the arc colour. If omitted, the ForeColor property will be used instead.

 

This example draws a point 5 pixels wide in the centre of the Form.

 

Me.DrawWidth = 5
Me.PSet (Me.ScaleWidth / 2, Me.ScaleHeight / 2)

 

GRAPHICS PROPERTIES:
The following is a list of properties for Visual Basic's Graphics objects.

The ScaleMode Graphics Property

The ScaleMode Property is used to determine the units used for drawing.

ScaleMode

Mode Constant Description
0 vbUser Used when ScaleHeight, ScaleWidth, ScaleLeft, and ScaleTop properties have custom values
1 vbTwips Twentieth of a printer's point (default)
2 vbPoints A printer's point (72 points to the inch)
3 vbPixels The smallest unit of monitor or printer resolution (machine dependent)
4 vbCharacters 120 x 240 twips per unit
5 vbInches Measured in inches
6 vbMillimeters Measured in millimeters
7 vbCentimeters Measured in centimeters

 

The following sets the ScaleMode of the form to inches.

 

Me.ScaleMode = vbInches

 

DRAW MODE

The DrawMode Property is used to determine how the graphical methods will be drawn over the background of the Graphic Object.

DRAWMODE

Mode Constant Description
1 vbBlackness Black Pen
2 vbNotMergePen Inverse of Merge Pen, mode 15
3 vbMaskNotPen Combination of background and inverse pen
4 vbNotCopyPen Inverse of Copy Pen, mode 13
5 vbMaskPenNot Combination of pen and inverse of background
6 vbInvert Inverse the display
7 vbXorPen Combination of the pen and the display, but not both
8 vbNotMaskPen Inverse setting of Mask Pen, mode 9
9 vbMaskPen Combination of the pen and the background
10 vbNotXorPen Inverse of Xor Pen, mode 7
11 vbNop Output unchanged
12 vbMergeNotPen Combination of background and the inverse of the pen colour
13 vbCopyPen Colour specified by the ForeColor Property (default)
14 vbMergePenNot Combination of pen colour and the inverse of the background
15 vbMergePen Combination of the pen colour and the display
16 vbWhiteness White Pen

 

The following changes the DrawMode of the form to the inverse of the background.

 

Me.DrawMode = vbInvert

 

 

 

DRAWSTYLE

The DrawStyle Property is used to determine the line style of the Graphics Object.

DrawStyle

Style Constant Description
0 vbSolid Default - solid
1 vbDash Dash
2 vbDot Dot
3 vbDashDot Dash-Dot
4 vbDashDotDot Dash-Dot-Dot
5 vbInvisible Transparent
6 vbInsideSolid Inside solid

 

The following sets the DrawStyle of the form to Dotted.

 

Me.DrawStyle = vbDot

 

 

DrawWidth

The DrawWidth property determines the width for output with the graphic methods. The width setting is in pixels regardless of the ScaleMode unit.

The following example sets the drawing mode to Invert, the style to dotted, and the width to 5 pixels on a form.

Me.ScaleMode = vbInches
Me.DrawMode = vbInvert
Me.DrawStyle = vbDot
Me.DrawWidth = 5
Me.Line (0, 0)-(1, 1)

FILLSTYLE


The FillStyle Property is used to determine how to fill Graphics Objects.

FillStyle

Style Constant Description
0 vbFSSolid Default - solid
1 vbFSTransparent Transparent (Default)
2 vbHorizontalLine Filled with horizontal lines
3 vbVerticalLine Filled with vertical lines
4 vbUpwardDiagonal Filled with upward diagonal lines
5 vbDownwardDiagonal Filled with downward diagonal lines
6 vbCross Filled with horizontal and vertical lines
7 vbDiagonalCross Filled with diagonal crossed lines

 

The following sets the FillStyle of the form to crossed.

 

Me.FillStyle = vbCross

 

SPECIFYING COLOURS:

The RGB function allows colours to be defined using ranges between 0 and 255 for red, green and blue. The values are passed to the function in the order red, green, blue - hence the function name RGB. A value of 0 means none of the colour, and a value of 255 means full intensity of that colour.
For example, red can be set using RGB(255, 0, 0). Intermediate colours can be defined using intermediate values. For example, grey can be set using RGB(128, 128, 128).RGB(0, 0, 0) is used to specify Black, and RGB(255, 255, 255) is used to specify White.

The following example sets for Form's ForeGround property to Magenta. Any graphics methods used will be drawn using this colour, unless overridden within the function.

 

Me.ForeColor = RGB(255, 0, 255)

 

 

The following colour constants may also be used to specify colours in Visual Basic.

COLOUR CONSTANTS:

Constant Hexadecimal Value Colour
vbBlack &H0 Black
vbRed &HFF Red
vbGreen &HFF00 Green
vbYellow &HFFFF Yellow
vbBlue &HFF0000 Blue
vbMagenta &HFF00FF Magenta
vbCyan &HFFFF00 Cyan
vbWhite &HFFFFFF White

 

The following sets the form's ForeColor property to red.

 

Me.ForeColor = vbRed

 

 

SMALL DRAWING PACKAGE

This example uses the graphics features mentioned above to produce a drawing package.

 

 

If you don't find what you are looking for. Please click here to submit your query, our experts will reply soon.

 

E-mail : sales@virtualsplat.com
Phone : +91-9892413501

Whatsapp Icon +91-9967648641

Whatsapp Icon +91-9967648641