Shape Painter : Painter « GUI Applications « VB.Net Tutorial

Home
VB.Net Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statements
5.Date Time
6.Class Module
7.Development
8.Collections
9.Generics
10.Attributes
11.Event
12.LINQ
13.Stream File
14.GUI
15.GUI Applications
16.Windows Presentation Foundation
17.2D Graphics
18.I18N Internationlization
19.Reflection
20.Regular Expressions
21.Security
22.Socket Network
23.Thread
24.Windows
25.XML
26.Database ADO.net
27.Design Patterns
VB.Net
VB.Net by API
VB.Net Tutorial » GUI Applications » Painter 
15.13.3.Shape Painter
Shape Painter
'Sams Teach Yourself Microsoft Visual Basic .NET 2003 in 21 Days, Second Edition (Paperback)
'by Steve Holzner (Author)
'# Publisher: Sams; edition (April 212003)
'# Language: English
'# ISBN-100672325314
'# ISBN-13978-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 NothingThen
                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(0248)
        Me.Button1.Name = "Button1"
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Rectangle"
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(80248)
        Me.Button2.Name = "Button2"
        Me.Button2.TabIndex = 1
        Me.Button2.Text = "Ellipse"
        '
        'Button3
        '
        Me.Button3.Location = New System.Drawing.Point(160248)
        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(00)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(15248)
        Me.Label1.TabIndex = 3
        Me.Label1.Text = "Graphics"
        '
        'Button4
        '
        Me.Button4.Location = New System.Drawing.Point(240248)
        Me.Button4.Name = "Button4"
        Me.Button4.TabIndex = 4
        Me.Button4.Text = "Freehand"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(513)
        Me.ClientSize = New System.Drawing.Size(320273)
        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.EventArgsHandles MyBase.Load
        gphFormGraphics = Me.CreateGraphics()
    End Sub

    Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgsHandles 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.MouseEventArgsHandles 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.EventArgsHandles Button1.Click
        btnCurrentButton = Buttons.Rectangle
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgsHandles Button2.Click
        btnCurrentButton = Buttons.Ellipse
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgsHandles Button3.Click
        btnCurrentButton = Buttons.Line
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgsHandles Button4.Click
        btnCurrentButton = Buttons.Freehand
    End Sub

    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgsHandles 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 >= 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.PaintEventArgsHandles 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 >= Then
                    gphFormGraphics.DrawLines(Pens.Navy, ptPointsArray)
                End If
        End Select
    End Sub
End Class
15.13.Painter
15.13.1.Scribble 1Scribble 1
15.13.2.Scribble application buffered by BitmapScribble application buffered by Bitmap
15.13.3.Shape PainterShape Painter
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.