Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class Scribble
public Shared Sub Main
Application.Run(New frmScribble)
End Sub
End class
"Sams Teach Yourself Visual Basic .NET in 21 Days
"By Lowell Mauer
"Published 2001
"Sams Publishing
"ISBN 0672322714
Public Class frmScribble
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
Private WithEvents pnlTools As System.Windows.Forms.Panel
Private WithEvents optPen As System.Windows.Forms.RadioButton
Private WithEvents picDraw As System.Windows.Forms.PictureBox
Private WithEvents optShape As System.Windows.Forms.RadioButton
Private WithEvents optText As System.Windows.Forms.RadioButton
Private WithEvents pnlOptions As System.Windows.Forms.Panel
Private WithEvents mnuMain As System.Windows.Forms.MainMenu
Private WithEvents mnuFile As System.Windows.Forms.MenuItem
Private WithEvents mnuFileNew As System.Windows.Forms.MenuItem
Private WithEvents mnuFileOpen As System.Windows.Forms.MenuItem
Private WithEvents mnuFileSave As System.Windows.Forms.MenuItem
Private WithEvents mnuFileSep As System.Windows.Forms.MenuItem
Private WithEvents mnuFileExit As System.Windows.Forms.MenuItem
Private WithEvents dlgOpen As System.Windows.Forms.OpenFileDialog
Private WithEvents dlgSave As System.Windows.Forms.SaveFileDialog
Private WithEvents pnlPenOptions As System.Windows.Forms.Panel
Private WithEvents lblColor As System.Windows.Forms.Label
Private WithEvents cboColors As System.Windows.Forms.ruboBox
Private WithEvents lblPenWidth As System.Windows.Forms.Label
Private WithEvents updPenWidth As System.Windows.Forms.NumericUpDown
Private WithEvents pnlShapeOptions As System.Windows.Forms.Panel
Private WithEvents pnlTextOptions As System.Windows.Forms.Panel
Private WithEvents lblText As System.Windows.Forms.Label
Private WithEvents txtText As System.Windows.Forms.TextBox
Private WithEvents lblTextFont As System.Windows.Forms.Label
Private WithEvents cboTextFont As System.Windows.Forms.ruboBox
Private WithEvents lblFontSize As System.Windows.Forms.Label
Private WithEvents updFontSize As System.Windows.Forms.NumericUpDown
Private WithEvents lblShapeType As System.Windows.Forms.Label
Private WithEvents cboShapeType As System.Windows.Forms.ruboBox
Private WithEvents lblShapeHeight As System.Windows.Forms.Label
Private WithEvents updShapeHeight As System.Windows.Forms.NumericUpDown
Private WithEvents updShapeWidth As System.Windows.Forms.NumericUpDown
Private WithEvents lblShapeWidth As System.Windows.Forms.Label
"Required by the Windows Form Designer
Private components As System.ruponentModel.Container
"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.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.updShapeHeight = New System.Windows.Forms.NumericUpDown()
Me.cboColors = New System.Windows.Forms.ruboBox()
Me.pnlShapeOptions = New System.Windows.Forms.Panel()
Me.lblShapeWidth = New System.Windows.Forms.Label()
Me.updShapeWidth = New System.Windows.Forms.NumericUpDown()
Me.lblShapeHeight = New System.Windows.Forms.Label()
Me.cboShapeType = New System.Windows.Forms.ruboBox()
Me.lblShapeType = New System.Windows.Forms.Label()
Me.updPenWidth = New System.Windows.Forms.NumericUpDown()
Me.pnlTools = New System.Windows.Forms.Panel()
Me.optText = New System.Windows.Forms.RadioButton()
Me.optShape = New System.Windows.Forms.RadioButton()
Me.optPen = New System.Windows.Forms.RadioButton()
Me.picDraw = New System.Windows.Forms.PictureBox()
Me.pnlPenOptions = New System.Windows.Forms.Panel()
Me.lblPenWidth = New System.Windows.Forms.Label()
Me.mnuFileExit = New System.Windows.Forms.MenuItem()
Me.updFontSize = New System.Windows.Forms.NumericUpDown()
Me.mnuMain = New System.Windows.Forms.MainMenu()
Me.mnuFile = New System.Windows.Forms.MenuItem()
Me.mnuFileNew = New System.Windows.Forms.MenuItem()
Me.mnuFileOpen = New System.Windows.Forms.MenuItem()
Me.mnuFileSave = New System.Windows.Forms.MenuItem()
Me.mnuFileSep = New System.Windows.Forms.MenuItem()
Me.cboTextFont = New System.Windows.Forms.ruboBox()
Me.pnlOptions = New System.Windows.Forms.Panel()
Me.pnlTextOptions = New System.Windows.Forms.Panel()
Me.lblFontSize = New System.Windows.Forms.Label()
Me.lblTextFont = New System.Windows.Forms.Label()
Me.txtText = New System.Windows.Forms.TextBox()
Me.lblText = New System.Windows.Forms.Label()
Me.lblColor = New System.Windows.Forms.Label()
Me.dlgSave = New System.Windows.Forms.SaveFileDialog()
Me.dlgOpen = New System.Windows.Forms.OpenFileDialog()
CType(Me.updShapeHeight, System.ruponentModel.ISupportInitialize).BeginInit()
Me.updShapeHeight.SuspendLayout()
Me.pnlShapeOptions.SuspendLayout()
CType(Me.updShapeWidth, System.ruponentModel.ISupportInitialize).BeginInit()
Me.updShapeWidth.SuspendLayout()
CType(Me.updPenWidth, System.ruponentModel.ISupportInitialize).BeginInit()
Me.updPenWidth.SuspendLayout()
Me.pnlTools.SuspendLayout()
Me.pnlPenOptions.SuspendLayout()
CType(Me.updFontSize, System.ruponentModel.ISupportInitialize).BeginInit()
Me.updFontSize.SuspendLayout()
Me.pnlOptions.SuspendLayout()
Me.pnlTextOptions.SuspendLayout()
Me.SuspendLayout()
"
"updShapeHeight
"
Me.updShapeHeight.Location = New System.Drawing.Point(64, 32)
Me.updShapeHeight.Maximum = New Decimal(New Integer() {1000, 0, 0, 0})
Me.updShapeHeight.Minimum = New Decimal(New Integer() {1, 0, 0, 0})
Me.updShapeHeight.Name = "updShapeHeight"
Me.updShapeHeight.Size = New System.Drawing.Size(48, 20)
Me.updShapeHeight.TabIndex = 3
Me.updShapeHeight.Value = New Decimal(New Integer() {20, 0, 0, 0})
"
"cboColors
"
Me.cboColors.DropDownStyle = System.Windows.Forms.ruboBoxStyle.DropDownList
Me.cboColors.DropDownWidth = 136
Me.cboColors.Location = New System.Drawing.Point(8, 24)
Me.cboColors.Name = "cboColors"
Me.cboColors.Size = New System.Drawing.Size(136, 21)
Me.cboColors.TabIndex = 2
"
"pnlShapeOptions
"
Me.pnlShapeOptions.Controls.AddRange(New System.Windows.Forms.Control() {Me.lblShapeWidth, Me.updShapeWidth, Me.updShapeHeight, Me.lblShapeHeight, Me.cboShapeType, Me.lblShapeType})
Me.pnlShapeOptions.Location = New System.Drawing.Point(160, 8)
Me.pnlShapeOptions.Name = "pnlShapeOptions"
Me.pnlShapeOptions.Size = New System.Drawing.Size(274, 56)
Me.pnlShapeOptions.TabIndex = 0
"
"lblShapeWidth
"
Me.lblShapeWidth.Location = New System.Drawing.Point(128, 32)
Me.lblShapeWidth.Name = "lblShapeWidth"
Me.lblShapeWidth.Size = New System.Drawing.Size(48, 16)
Me.lblShapeWidth.TabIndex = 2
Me.lblShapeWidth.Text = "Width:"
"
"updShapeWidth
"
Me.updShapeWidth.Location = New System.Drawing.Point(184, 32)
Me.updShapeWidth.Maximum = New Decimal(New Integer() {1000, 0, 0, 0})
Me.updShapeWidth.Minimum = New Decimal(New Integer() {1, 0, 0, 0})
Me.updShapeWidth.Name = "updShapeWidth"
Me.updShapeWidth.Size = New System.Drawing.Size(48, 20)
Me.updShapeWidth.TabIndex = 3
Me.updShapeWidth.Value = New Decimal(New Integer() {20, 0, 0, 0})
"
"lblShapeHeight
"
Me.lblShapeHeight.Location = New System.Drawing.Point(8, 32)
Me.lblShapeHeight.Name = "lblShapeHeight"
Me.lblShapeHeight.Size = New System.Drawing.Size(48, 16)
Me.lblShapeHeight.TabIndex = 2
Me.lblShapeHeight.Text = "Height:"
"
"cboShapeType
"
Me.cboShapeType.DropDownStyle = System.Windows.Forms.ruboBoxStyle.DropDownList
Me.cboShapeType.DropDownWidth = 121
Me.cboShapeType.Location = New System.Drawing.Point(64, 8)
Me.cboShapeType.Name = "cboShapeType"
Me.cboShapeType.Size = New System.Drawing.Size(121, 21)
Me.cboShapeType.TabIndex = 1
"
"lblShapeType
"
Me.lblShapeType.Location = New System.Drawing.Point(8, 8)
Me.lblShapeType.Name = "lblShapeType"
Me.lblShapeType.Size = New System.Drawing.Size(48, 16)
Me.lblShapeType.TabIndex = 0
Me.lblShapeType.Text = "Type:"
"
"updPenWidth
"
Me.updPenWidth.Location = New System.Drawing.Point(96, 8)
Me.updPenWidth.Maximum = New Decimal(New Integer() {10, 0, 0, 0})
Me.updPenWidth.Minimum = New Decimal(New Integer() {1, 0, 0, 0})
Me.updPenWidth.Name = "updPenWidth"
Me.updPenWidth.Size = New System.Drawing.Size(48, 20)
Me.updPenWidth.TabIndex = 1
Me.updPenWidth.Value = New Decimal(New Integer() {1, 0, 0, 0})
"
"pnlTools
"
Me.pnlTools.Controls.AddRange(New System.Windows.Forms.Control() {Me.optText, Me.optShape, Me.optPen})
Me.pnlTools.Dock = System.Windows.Forms.DockStyle.Left
Me.pnlTools.Name = "pnlTools"
Me.pnlTools.Size = New System.Drawing.Size(64, 332)
Me.pnlTools.TabIndex = 0
"
"optText
"
Me.optText.Appearance = System.Windows.Forms.Appearance.Button
Me.optText.Location = New System.Drawing.Point(8, 88)
Me.optText.Name = "optText"
Me.optText.Size = New System.Drawing.Size(48, 36)
Me.optText.TabIndex = 0
Me.optText.Text = "Text"
Me.optText.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
"
"optShape
"
Me.optShape.Appearance = System.Windows.Forms.Appearance.Button
Me.optShape.Location = New System.Drawing.Point(8, 48)
Me.optShape.Name = "optShape"
Me.optShape.Size = New System.Drawing.Size(48, 36)
Me.optShape.TabIndex = 0
Me.optShape.Text = "Shape"
Me.optShape.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
"
"optPen
"
Me.optPen.Appearance = System.Windows.Forms.Appearance.Button
Me.optPen.Checked = True
Me.optPen.Location = New System.Drawing.Point(8, 8)
Me.optPen.Name = "optPen"
Me.optPen.Size = New System.Drawing.Size(48, 36)
Me.optPen.TabIndex = 0
Me.optPen.TabStop = True
Me.optPen.Text = "Pen"
Me.optPen.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
"
"picDraw
"
Me.picDraw.BackColor = System.Drawing.SystemColors.Window
Me.picDraw.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
Me.picDraw.Dock = System.Windows.Forms.DockStyle.Fill
Me.picDraw.Location = New System.Drawing.Point(64, 0)
Me.picDraw.Name = "picDraw"
Me.picDraw.Size = New System.Drawing.Size(436, 260)
Me.picDraw.TabIndex = 2
Me.picDraw.TabStop = False
"
"pnlPenOptions
"
Me.pnlPenOptions.Controls.AddRange(New System.Windows.Forms.Control() {Me.updPenWidth, Me.lblPenWidth})
Me.pnlPenOptions.Location = New System.Drawing.Point(160, 8)
Me.pnlPenOptions.Name = "pnlPenOptions"
Me.pnlPenOptions.Size = New System.Drawing.Size(274, 56)
Me.pnlPenOptions.TabIndex = 0
"
"lblPenWidth
"
Me.lblPenWidth.Location = New System.Drawing.Point(8, 8)
Me.lblPenWidth.Name = "lblPenWidth"
Me.lblPenWidth.Size = New System.Drawing.Size(80, 16)
Me.lblPenWidth.TabIndex = 0
Me.lblPenWidth.Text = "Pen Width:"
"
"mnuFileExit
"
Me.mnuFileExit.Index = 4
Me.mnuFileExit.Shortcut = System.Windows.Forms.Shortcut.CtrlQ
Me.mnuFileExit.Text = "E&xit"
"
"updFontSize
"
Me.updFontSize.Location = New System.Drawing.Point(208, 32)
Me.updFontSize.Maximum = New Decimal(New Integer() {72, 0, 0, 0})
Me.updFontSize.Minimum = New Decimal(New Integer() {6, 0, 0, 0})
Me.updFontSize.Name = "updFontSize"
Me.updFontSize.Size = New System.Drawing.Size(48, 20)
Me.updFontSize.TabIndex = 5
Me.updFontSize.Value = New Decimal(New Integer() {12, 0, 0, 0})
"
"mnuMain
"
Me.mnuMain.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuFile})
"
"mnuFile
"
Me.mnuFile.Index = 0
Me.mnuFile.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuFileNew, Me.mnuFileOpen, Me.mnuFileSave, Me.mnuFileSep, Me.mnuFileExit})
Me.mnuFile.Text = "&File"
"
"mnuFileNew
"
Me.mnuFileNew.Index = 0
Me.mnuFileNew.Shortcut = System.Windows.Forms.Shortcut.CtrlN
Me.mnuFileNew.Text = "&New"
"
"mnuFileOpen
"
Me.mnuFileOpen.Index = 1
Me.mnuFileOpen.Shortcut = System.Windows.Forms.Shortcut.CtrlO
Me.mnuFileOpen.Text = "&Open..."
"
"mnuFileSave
"
Me.mnuFileSave.Index = 2
Me.mnuFileSave.Shortcut = System.Windows.Forms.Shortcut.CtrlS
Me.mnuFileSave.Text = "&Save"
"
"mnuFileSep
"
Me.mnuFileSep.Index = 3
Me.mnuFileSep.Text = "-"
"
"cboTextFont
"
Me.cboTextFont.DropDownStyle = System.Windows.Forms.ruboBoxStyle.DropDownList
Me.cboTextFont.DropDownWidth = 104
Me.cboTextFont.Location = New System.Drawing.Point(48, 32)
Me.cboTextFont.Name = "cboTextFont"
Me.cboTextFont.Size = New System.Drawing.Size(104, 21)
Me.cboTextFont.TabIndex = 3
"
"pnlOptions
"
Me.pnlOptions.Controls.AddRange(New System.Windows.Forms.Control() {Me.pnlTextOptions, Me.pnlShapeOptions, Me.cboColors, Me.lblColor, Me.pnlPenOptions})
Me.pnlOptions.Dock = System.Windows.Forms.DockStyle.Bottom
Me.pnlOptions.Location = New System.Drawing.Point(64, 260)
Me.pnlOptions.Name = "pnlOptions"
Me.pnlOptions.Size = New System.Drawing.Size(436, 72)
Me.pnlOptions.TabIndex = 1
"
"pnlTextOptions
"
Me.pnlTextOptions.Controls.AddRange(New System.Windows.Forms.Control() {Me.updFontSize, Me.lblFontSize, Me.cboTextFont, Me.lblTextFont, Me.txtText, Me.lblText})
Me.pnlTextOptions.Location = New System.Drawing.Point(160, 8)
Me.pnlTextOptions.Name = "pnlTextOptions"
Me.pnlTextOptions.Size = New System.Drawing.Size(274, 56)
Me.pnlTextOptions.TabIndex = 0
"
"lblFontSize
"
Me.lblFontSize.Location = New System.Drawing.Point(160, 32)
Me.lblFontSize.Name = "lblFontSize"
Me.lblFontSize.Size = New System.Drawing.Size(40, 16)
Me.lblFontSize.TabIndex = 4
Me.lblFontSize.Text = "Size:"
"
"lblTextFont
"
Me.lblTextFont.Location = New System.Drawing.Point(8, 32)
Me.lblTextFont.Name = "lblTextFont"
Me.lblTextFont.Size = New System.Drawing.Size(40, 16)
Me.lblTextFont.TabIndex = 2
Me.lblTextFont.Text = "Font:"
"
"txtText
"
Me.txtText.Location = New System.Drawing.Point(48, 8)
Me.txtText.Name = "txtText"
Me.txtText.Size = New System.Drawing.Size(208, 20)
Me.txtText.TabIndex = 1
Me.txtText.Text = "Scribble!"
"
"lblText
"
Me.lblText.Location = New System.Drawing.Point(8, 8)
Me.lblText.Name = "lblText"
Me.lblText.Size = New System.Drawing.Size(40, 16)
Me.lblText.TabIndex = 0
Me.lblText.Text = "Text:"
"
"lblColor
"
Me.lblColor.Location = New System.Drawing.Point(8, 8)
Me.lblColor.Name = "lblColor"
Me.lblColor.Size = New System.Drawing.Size(48, 16)
Me.lblColor.TabIndex = 1
Me.lblColor.Text = "Color:"
"
"dlgSave
"
Me.dlgSave.DefaultExt = "bmp"
Me.dlgSave.FileName = "Scribble1"
Me.dlgSave.Filter = "Bitmap files|*.bmp|PNG files|*.png"
"
"dlgOpen
"
Me.dlgOpen.DefaultExt = "bmp"
Me.dlgOpen.Filter = "Bitmap files|*.bmp|PNG files|*.png"
"
"frmScribble
"
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(500, 332)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.picDraw, Me.pnlOptions, Me.pnlTools})
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
Me.Menu = Me.mnuMain
Me.Name = "frmScribble"
Me.Text = "Scribble"
CType(Me.updShapeHeight, System.ruponentModel.ISupportInitialize).EndInit()
Me.updShapeHeight.ResumeLayout(False)
Me.pnlShapeOptions.ResumeLayout(False)
CType(Me.updShapeWidth, System.ruponentModel.ISupportInitialize).EndInit()
Me.updShapeWidth.ResumeLayout(False)
CType(Me.updPenWidth, System.ruponentModel.ISupportInitialize).EndInit()
Me.updPenWidth.ResumeLayout(False)
Me.pnlTools.ResumeLayout(False)
Me.pnlPenOptions.ResumeLayout(False)
CType(Me.updFontSize, System.ruponentModel.ISupportInitialize).EndInit()
Me.updFontSize.ResumeLayout(False)
Me.pnlOptions.ResumeLayout(False)
Me.pnlTextOptions.ResumeLayout(False)
Me.ResumeLayout(False)
End Sub
#End Region
#Region "Enumerations"
Public Enum DrawingTools
Pen
Shape
Text
End Enum
#End Region
#Region "Member variables for properties"
Private m_sFileName As String
Private m_bDrawing As Boolean
Private m_eCurrentTool As DrawingTools
#End Region
#Region "Private variables"
Private oGraphics As System.Drawing.Graphics
Private oTool As Object
Private sngX As Single
Private sngY As Single
#End Region
#Region "Properties"
Public Property FileName() As String
Get
Return m_sFileName
End Get
Set(ByVal Value As String)
m_sFileName = Value
End Set
End Property
Public Property Drawing() As Boolean
Get
Return m_bDrawing
End Get
Set(ByVal Value As Boolean)
m_bDrawing = Value
End Set
End Property
Public Property CurrentTool() As DrawingTools
Get
Return m_eCurrentTool
End Get
Set(ByVal Value As DrawingTools)
m_eCurrentTool = Value
"destroy the existing tool
If Not oTool Is Nothing Then
CType(oTool, IDisposable).Dispose()
End If
End Set
End Property
#End Region
#Region "Menu commands"
Private Sub mnuFileNew_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles mnuFileNew.Click
Me.oGraphics.Clear(Color.White)
"force the refresh
Me.picDraw.Refresh()
End Sub
Private Sub mnuFileOpen_Click()
If Me.dlgOpen.ShowDialog = DialogResult.OK Then
Me.picDraw.Image.FromFile(Me.dlgOpen.FileName)
End If
End Sub
Private Sub mnuFileSave_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles mnuFileSave.Click
Dim sFileName As String
Dim oFormat As System.Drawing.Imaging.ImageFormat
"get the filename to save to
If dlgSave.ShowDialog = DialogResult.OK Then
sFileName = dlgSave.FileName
Select Case dlgSave.FilterIndex
Case 0 "save as bitmap
oFormat = System.Drawing.Imaging.ImageFormat.Bmp
Case 1 "save as PNG
oFormat = System.Drawing.Imaging.ImageFormat.Png
Case Else
"should never happen
End Select
"possible exception on save
Try
Me.picDraw.Image.Save(sFileName, oFormat)
Catch ex As Exception
"just display for now
MessageBox.Show(ex.Message, "Error saving file", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End If
End Sub
Private Sub mnuFileExit_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles mnuFileExit.Click
Me.Close()
End Sub
#End Region
#Region "PictureBox Event Handlers"
Private Sub picDraw_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picDraw.MouseMove
If Me.Drawing Then
"this is only true if the current tool is a pen
oGraphics.DrawLine(oTool, sngX, sngY, e.X, e.Y)
sngX = e.X
sngY = e.Y
"force a redraw
Me.picDraw.Refresh()
End If
End Sub
Private Sub picDraw_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picDraw.MouseUp
"we can stop drawing now
Me.Drawing = False
End Sub
Private Sub picDraw_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picDraw.MouseDown
"start drawing
"Shape and Text are stamps, Pen works on MouseMove
"we need to create the tool and either draw it, or get ready to draw it
Select Case Me.CurrentTool
Case DrawingTools.Shape
Select Case Me.cboShapeType.Text
Case "Rectangle"
oGraphics.FillRectangle(New SolidBrush(Color.FromName(Me.cboColors.Text)), _
e.X, _
e.Y, _
Me.updShapeWidth.Value, _
Me.updShapeHeight.Value)
Case "Ellipse"
oGraphics.FillEllipse(New SolidBrush(Color.FromName(Me.cboColors.Text)), _
e.X, _
e.Y, _
Me.updShapeWidth.Value, _
Me.updShapeHeight.Value)
Case Else
End Select
"force a redraw
Me.picDraw.Refresh()
Case DrawingTools.Text
"create a font
oTool = New System.Drawing.Font(Me.cboTextFont.Text, Me.updFontSize.Value)
"draw the text at the current mouse location
oGraphics.DrawString(Me.txtText.Text, _
oTool, New SolidBrush(Color.FromName(Me.cboColors.Text)), _
e.X, e.Y)
"force a redraw
Me.picDraw.Refresh()
Case DrawingTools.Pen
"create the pen (for drawing in MouseMove)
oTool = New System.Drawing.Pen(Color.FromName(Me.cboColors.Text), Me.updPenWidth.Value)
sngX = e.X
sngY = e.Y
Me.Drawing = True
End Select
End Sub
#End Region
#Region "Other Event Handlers"
Private Sub frmScribble_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
"set up the Color ComboBox
FillLists()
"create the graphics we"ll be drawing on
Me.picDraw.Image = New Bitmap(picDraw.Width, picDraw.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb)
Me.oGraphics = Graphics.FromImage(Me.picDraw.Image)
"set the background to white
Me.oGraphics.Clear(Color.White)
"set the initial tool to the Pen
optPen_Click(Nothing, Nothing)
End Sub
Private Sub optPen_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles optPen.Click
"set the tool to a pen
Me.CurrentTool = DrawingTools.Pen
"hide all the other tool"s panels
pnlPenOptions.Visible = True
pnlShapeOptions.Visible = False
pnlTextOptions.Visible = False
End Sub
Private Sub optShape_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles optShape.Click
"set the tool to a Shape
Me.CurrentTool = DrawingTools.Shape
"hide all the other tool"s panels
pnlPenOptions.Visible = False
pnlShapeOptions.Visible = True
pnlTextOptions.Visible = False
End Sub
Private Sub optText_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles optText.Click
"set the tool to Text
Me.CurrentTool = DrawingTools.Text
"hide all the other tool"s panels
pnlPenOptions.Visible = False
pnlShapeOptions.Visible = False
pnlTextOptions.Visible = True
End Sub
#End Region
#Region "Assorted methods"
Private Sub FillLists()
With cboColors.Items
.Add("Black")
.Add("Red")
.Add("Green")
.Add("Blue")
End With
cboColors.SelectedIndex = 0
With cboShapeType.Items
.Add("Rectangle")
.Add("Ellipse")
End With
cboShapeType.SelectedIndex = 0
With cboTextFont.Items
.Add("Arial")
.Add("Times New Roman")
.Add("Courier New")
End With
cboTextFont.SelectedIndex = 0
End Sub
#End Region
End Class
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
Imports System.Math
public class ScribbleApplication
public Shared Sub Main
Application.Run(New Form1)
End Sub
End class
Public Class Form1
Private m_Bitmap As Bitmap
Private m_Graphics As Graphics
Private m_Drawing As Boolean
Private m_X As Integer
Private m_Y As Integer
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
MakeNewBitmap()
End Sub
Private Sub mnuFileClear_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles mnuFileClear.Click
MakeNewBitmap()
End Sub
Private Sub MakeNewBitmap()
Dim wid As Integer = picCanvas.ClientSize.Width
Dim hgt As Integer = picCanvas.ClientSize.Height
m_Bitmap = New Bitmap(wid, hgt)
m_Graphics = Graphics.FromImage(m_Bitmap)
m_Graphics.Clear(Me.BackColor)
picCanvas.Image = m_Bitmap
End Sub
Private Sub picCanvas_MouseDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) Handles picCanvas.MouseDown
m_Drawing = True
m_X = e.X
m_Y = e.Y
End Sub
Private Sub picCanvas_MouseMove(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) Handles picCanvas.MouseMove
If Not m_Drawing Then Exit Sub
m_Graphics.DrawLine(Pens.Black, m_X, m_Y, e.X, e.Y)
m_X = e.X
m_Y = e.Y
picCanvas.Image = m_Bitmap
End Sub
Private Sub picCanvas_MouseUp(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) Handles picCanvas.MouseUp
m_Drawing = False
End Sub
End Class
<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _
Partial Public Class Form1
Inherits System.Windows.Forms.Form
"Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
MyBase.Dispose(disposing)
End Sub
"Required by the Windows Form Designer
Private components As System.ruponentModel.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.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.picCanvas = New System.Windows.Forms.PictureBox
Me.MenuStrip1 = New System.Windows.Forms.MenuStrip
Me.mnuFile = New System.Windows.Forms.ToolStripMenuItem
Me.mnuFileClear = New System.Windows.Forms.ToolStripMenuItem
CType(Me.picCanvas, System.ruponentModel.ISupportInitialize).BeginInit()
Me.MenuStrip1.SuspendLayout()
Me.SuspendLayout()
"
"picCanvas
"
Me.picCanvas.Dock = System.Windows.Forms.DockStyle.Fill
Me.picCanvas.Location = New System.Drawing.Point(0, 24)
Me.picCanvas.Name = "picCanvas"
Me.picCanvas.Size = New System.Drawing.Size(303, 246)
Me.picCanvas.TabIndex = 0
Me.picCanvas.TabStop = False
"
"MenuStrip1
"
Me.MenuStrip1.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.mnuFile})
Me.MenuStrip1.Location = New System.Drawing.Point(0, 0)
Me.MenuStrip1.Name = "MenuStrip1"
Me.MenuStrip1.Size = New System.Drawing.Size(303, 24)
Me.MenuStrip1.TabIndex = 1
Me.MenuStrip1.Text = "MenuStrip1"
"
"mnuFile
"
Me.mnuFile.DropDownItems.AddRange(New System.Windows.Forms.ToolStripItem() {Me.mnuFileClear})
Me.mnuFile.Name = "mnuFile"
Me.mnuFile.Text = "&File"
"
"mnuFileClear
"
Me.mnuFileClear.Name = "mnuFileClear"
Me.mnuFileClear.Text = "&Clear"
"
"Form1
"
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(303, 270)
Me.Controls.Add(Me.picCanvas)
Me.Controls.Add(Me.MenuStrip1)
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
Me.MainMenuStrip = Me.MenuStrip1
Me.Name = "Form1"
Me.Text = "Scribble"
CType(Me.picCanvas, System.ruponentModel.ISupportInitialize).EndInit()
Me.MenuStrip1.ResumeLayout(False)
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents picCanvas As System.Windows.Forms.PictureBox
Friend WithEvents MenuStrip1 As System.Windows.Forms.MenuStrip
Friend WithEvents mnuFile As System.Windows.Forms.ToolStripMenuItem
Friend WithEvents mnuFileClear As System.Windows.Forms.ToolStripMenuItem
End Class
"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.ruponentModel.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