Membuat aplikasi Windows Forms baru, dan menambahkan kontrol berikut untuk Form1:
RadioButton bernama DrawLine. Atur properti Text untuk Line dan properti Checked untuk benar (true).
RadioButton bernama DrawRectangle. Atur properti Text untuk Rectangle.
RadioButton bernama DrawEllipse. Atur properti Text untuk Ellipse.
ComboBox bernama LineColor. Set properti DropDownStyle untuk DropDownList.
ComboBox bernama FillColor. Set properti DropDownStyle untuk DropDownList.
PictureBox bernama DrawingArea. Set properti BackColor ke White (atau 255, 255, 255) dan properti BorderStyle untuk Fixed3D.
Coding :
ublic Class Form1
Private FirstPoint As Point = New Point(-1, -1)
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
' ----- Fill in the list of colors.
For Each colorName As String In New String() _
{"Black", "Red", "Orange", "Yellow", "Green", _
"Blue", "Indigo", "Violet", "White"}
LineColor.Items.Add(colorName)
FillColor.Items.Add(colorName)
Next colorName
LineColor.SelectedIndex = LineColor.Items.IndexOf("Black")
FillColor.SelectedIndex = LineColor.Items.IndexOf("White")
End Sub
Private Sub DrawingArea_MouseDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles DrawingArea.MouseDown
' ----- Time to do some drawing.
Dim useLine As Pen
Dim useFill As Brush
Dim canvas As Graphics
Dim drawBounds As Rectangle
' ----- Is this the first or second click?
If (FirstPoint.Equals(New Point(-1, -1))) Then
' ----- This is the first click. Record the location.
FirstPoint = e.Location
' ----- Draw a marker at this point.
DrawMarker(FirstPoint)
Else
' ----- Get the two colors to use.
useLine = New Pen(Color.FromName(LineColor.Text))
useFill = New SolidBrush(Color.FromName(FillColor.Text))
' ----- Get the drawing surface.
canvas = DrawingArea.CreateGraphics()
' ----- Remove the first-point marker.
DrawMarker(FirstPoint)
' ----- Forrectangles and ellipses, get the
' bounding area.
drawBounds = New Rectangle( _
Math.Min(FirstPoint.X, e.Location.X), _
Math.Min(FirstPoint.Y, e.Location.Y), _
Math.Abs(FirstPoint.X - e.Location.X), _
Math.Abs(FirstPoint.Y - e.Location.Y))
' ----- Time to draw.
If (DrawLine.Checked = True) Then
' ----- Draw a line.
canvas.DrawLine(useLine, FirstPoint, e.Location)
ElseIf (DrawRectangle.Checked = True) Then
' ----- Draw a rectangle.
canvas.FillRectangle(useFill, drawBounds)
canvas.DrawRectangle(useLine, drawBounds)
Else
' ----- Draw an ellipse.
canvas.FillEllipse(useFill, drawBounds)
canvas.DrawEllipse(useLine, drawBounds)
End If
' ----- Clean up.
canvas.Dispose()
useFill.Dispose()
useLine.Dispose()
FirstPoint = New Point(-1, -1)
End If
End Sub
Private Sub DrawMarker(ByVal centerPoint As Point)
' ----- Given a point, draw a small square at
' that location.
Dim screenPoint As Point
Dim fillArea As Rectangle
' ----- Determine the fill area.
screenPoint = DrawingArea.PointToScreen(centerPoint)
fillArea = New Rectangle(screenPoint.X - 2, _
screenPoint.Y - 2, 5, 5)
' ----- Draw a red rectangle. Cyan is the RBG
' inverse of red.
ControlPaint.FillReversibleRectangle(fillArea, Color.Cyan)
End Sub
End Class
Berikut adalah hasil tampilannya :
0 Responses to 'Program Menggambar Garis, Oval , dan Persegi Panjang'
Posting Komentar