'Sams Teach Yourself Microsoft Visual Basic .NET 2003 in 21 Days, Second Edition (Paperback)
'by Steve Holzner (Author)
'# Publisher: Sams; 2 edition (April 21, 2003)
'# Language: English
'# ISBN-10: 0672325314
'# ISBN-13: 978-0672325311
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class PainterShape
public Shared Sub Main
Application.Run(New Form1)
End Sub
End class
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents Button3 As System.Windows.Forms.Button
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Button4 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
Me.Button3 = New System.Windows.Forms.Button
Me.Label1 = New System.Windows.Forms.Label
Me.Button4 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(0, 248)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 0
Me.Button1.Text = "Rectangle"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(80, 248)
Me.Button2.Name = "Button2"
Me.Button2.TabIndex = 1
Me.Button2.Text = "Ellipse"
'
'Button3
'
Me.Button3.Location = New System.Drawing.Point(160, 248)
Me.Button3.Name = "Button3"
Me.Button3.TabIndex = 2
Me.Button3.Text = "Line"
'
'Label1
'
Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 24.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label1.Location = New System.Drawing.Point(0, 0)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(152, 48)
Me.Label1.TabIndex = 3
Me.Label1.Text = "Graphics"
'
'Button4
'
Me.Button4.Location = New System.Drawing.Point(240, 248)
Me.Button4.Name = "Button4"
Me.Button4.TabIndex = 4
Me.Button4.Text = "Freehand"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(320, 273)
Me.Controls.Add(Me.Button4)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.Button3)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Dim gphFormGraphics As Graphics
Dim pt1, pt2 As Point
Dim ptPointsArray() As Point
Dim intNumberOfPoints As Integer = 0
Dim recDrawingRectangle As Rectangle
Dim btnCurrentButton As Buttons
Enum Buttons
Rectangle
Ellipse
Line
Freehand
End Enum
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
gphFormGraphics = Me.CreateGraphics()
End Sub
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
pt1 = New Point(e.X, e.Y)
End Sub
Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
pt2 = New Point(e.X, e.Y)
recDrawingRectangle = New Rectangle(Math.Min(pt2.X, pt1.X), Math.Min(pt2.Y, pt1.Y), _
Math.Abs(pt2.X - pt1.X), Math.Abs(pt2.Y - pt1.Y))
Select Case btnCurrentButton
Case Buttons.Rectangle
gphFormGraphics.DrawRectangle(Pens.Navy, recDrawingRectangle)
Case Buttons.Ellipse
gphFormGraphics.DrawEllipse(Pens.Navy, recDrawingRectangle)
Case Buttons.Line
gphFormGraphics.DrawLine(Pens.Navy, pt2, pt1)
End Select
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
btnCurrentButton = Buttons.Rectangle
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
btnCurrentButton = Buttons.Ellipse
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
btnCurrentButton = Buttons.Line
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
btnCurrentButton = Buttons.Freehand
End Sub
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
If btnCurrentButton = Buttons.Freehand And e.Button = MouseButtons.Left Then
Dim ptNew As New Point(e.X, e.Y)
ReDim Preserve ptPointsArray(intNumberOfPoints)
ptPointsArray(intNumberOfPoints) = ptNew
intNumberOfPoints += 1
If intNumberOfPoints >= 2 Then
gphFormGraphics.DrawLines(Pens.Navy, ptPointsArray)
End If
End If
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Select Case btnCurrentButton
Case Buttons.Rectangle
gphFormGraphics.DrawRectangle(Pens.Navy, recDrawingRectangle)
Case Buttons.Ellipse
gphFormGraphics.DrawEllipse(Pens.Navy, recDrawingRectangle)
Case Buttons.Line
gphFormGraphics.DrawLine(Pens.Navy, pt2, pt1)
Case Buttons.Freehand
If intNumberOfPoints >= 2 Then
gphFormGraphics.DrawLines(Pens.Navy, ptPointsArray)
End If
End Select
End Sub
End Class