Website Design United States, Website Design California, Website Designing United States, Website Designing California

Use the Graphics object to draw shapes on a Form - Visual Basic .NET

Draw simple 2D shapes on a form using the Graphics object

'' Form contains:
' Button - btnRectangles
' Button - btnLines

Imports System.Drawing ' place outside of the class (form)

' put inside the form
Private Sub btnRectangles_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnRectangles.Click
Dim objGraphics As Graphics
objGraphics = Me.CreateGraphics() ' create graphics object of form
objGraphics.Clear(Color.White) ' clear the form
' Draw a series of rectangles showing the overloaded methods
' Rectangle Outlines (no fill)
' DrawRectangle(Pen, Rectangle)
' Pen - an object that contains the colour, width and style of the outline
' Rectangle - an object the contains the X and Y coordinates
' as well as the Width and Height of the shape
objGraphics.DrawRectangle(Pens.Chartreuse, New Rectangle(10, 10, 20, 30))
' DrawRectangle(Pen, X As Integer, Y As Integer, Width As Integer, Height As Integer)
' DrawRectangle(Pen, X As Single, Y As Single, Width As Single, Height As Single)
' Pen - an object that contains the colour, width and style of the outline
' X and Y - specifies the location on the object where the rectangle starts
' Width and Height - the width and height of the Rectangle that is drawn
' Integer - a whole number (no decimal)
' Single - a number that can have a decimal
objGraphics.DrawRectangle(Pens.RoyalBlue, 20, 20, 40, 60)
objGraphics.DrawRectangle(Pens.Green, 22.5!, 22.5!, 35.5!, 55.5!) ' the ! is to tell vb that it is a single value
' Filled Rectangles
' FillRectangle(Brush, Rectangle) and
' FillRectangle(Brush, RectangleF)
' Brush - an object that contains the type and colour of the fill
' Rectangle - an object that contains Integer values for X and Y
' coordinates as well as Width and Height
' RectangleF - an object that contains Single values for X and Y
' coordinates as well as Width and Height
objGraphics.FillRectangle(Brushes.Magenta, New Rectangle(30, 100, 50, 20))
objGraphics.FillRectangle(Brushes.Black, _
New RectangleF(82.5!, 122.9!, 42.6!, 17.1!))
' FillRectangle(Brush, X As Integer, Y As Integer, Width As Integer, Height As Integer) and
' FillRectangle(Brush, X As Single, Y As Single, Width As Single, Height As Single)
' Brush - an object that specifies the type and colour of the fill for the drawing
' X and Y - specifies the location on the object where the rectangle starts
' Width and Height - the width and height of the Rectangle that is drawn
' Integer - a whole number (no decimal)
' Single - a number that can have a decimal
objGraphics.FillRectangle(Brushes.CornflowerBlue, 30, 160, 200, 30)
objGraphics.FillRectangle(Brushes.Bisque, 232.9!, 193.5!, 192.7!, 24.8!)
End Sub

Private Sub btnEllipses_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnLines.Click
Dim objGraphics As Graphics
objGraphics = Me.CreateGraphics() ' create graphics object from form
objGraphics.Clear(Color.White) ' clear the form
' Draw a series of lines showing the overloaded methods
' DrawLine(Pen, X1 As Integer, Y1 As Integer, X2 As Integer, Y2 As Integer) and
' DrawLine(Pen, X1 As Single, Y1 As Single, X2 As Single, Y2 As Single)
' Pen - an object that contains the type, width and colour of the line
' X1 and Y1 - the coordinates of the starting point of the line
' X2 and Y2 - the coordinates of the ending point of the line
' Integer - a whole number (no decimal)
' Single - a number that can have a decimal
objGraphics.DrawLine(Pens.Chartreuse, 0, 0, Me.Width, Me.Height)
objGraphics.DrawLine(New Pen(Color.SkyBlue, 5.5), 2.5!, 20.2!, 245.9!, 120.1!)
' DrawLine(Pen, Point, Point) and
' DrawLine(Pen, PointF, PointF)
' Pen - an object that contains the type, width and colour of the line
' Point - an object that contains an Integer X and Y coordinate
' PointF - an object that contains a Single data type X and Y coordinate
objGraphics.DrawLine(Pens.Gainsboro, New Point(Me.Width \ 2, 0), _
New Point(Me.Width \ 2, Me.Height))
objGraphics.DrawLine(New Pen(Color.Firebrick, 10.1), New PointF(88.7, 0.5), _
New PointF(50.3, 300.8))
End Sub


WEB DESIGN INDIA
42 B Malviya Nagar , New Delhi-110017

Skype: manmeetsi
Email: support.webdesignindia@gmail.com
Tel: 91-011-40502005, 9810067295

 















 


© 2008-2009 dotnet4all.com