' Code from Begin VB.net Programming
Imports System
Imports System.Collections
Imports System.Data
Imports System.IO
Imports System.Xml.Serialization
Imports System.Xml
Imports System.Windows.Forms
Imports System.Data.SqlClient
Imports System.Drawing
Imports System.ComponentModel
Public Class MainClass
Shared Sub Main()
Dim form1 As Form = New Form1
Application.Run(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 canvas As PaintCanvas
Friend WithEvents paletteColor As ColorPalette
Friend WithEvents mnuMain As System.Windows.Forms.MainMenu
Friend WithEvents mnuTools As System.Windows.Forms.MenuItem
Friend WithEvents mnuToolsCircle As System.Windows.Forms.MenuItem
Friend WithEvents mnuToolsHollowCircle As System.Windows.Forms.MenuItem
Friend WithEvents dlgOpenBackground As System.Windows.Forms.OpenFileDialog
Friend WithEvents mnuFile As System.Windows.Forms.MenuItem
Friend WithEvents mnuFileOpenBackground As System.Windows.Forms.MenuItem
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.canvas = New PaintCanvas()
Me.paletteColor = New ColorPalette()
Me.mnuMain = New System.Windows.Forms.MainMenu()
Me.mnuFile = New System.Windows.Forms.MenuItem()
Me.mnuFileOpenBackground = New System.Windows.Forms.MenuItem()
Me.mnuTools = New System.Windows.Forms.MenuItem()
Me.mnuToolsCircle = New System.Windows.Forms.MenuItem()
Me.mnuToolsHollowCircle = New System.Windows.Forms.MenuItem()
Me.dlgOpenBackground = New System.Windows.Forms.OpenFileDialog()
Me.SuspendLayout()
'
'canvas
'
Me.canvas.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right)
Me.canvas.BackColor = System.Drawing.Color.White
Me.canvas.Location = New System.Drawing.Point(4, -4)
Me.canvas.Name = "canvas"
Me.canvas.Size = New System.Drawing.Size(684, 356)
Me.canvas.TabIndex = 0
'
'paletteColor
'
Me.paletteColor.Anchor = ((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right)
Me.paletteColor.AutoScroll = True
Me.paletteColor.Location = New System.Drawing.Point(0, 356)
Me.paletteColor.Name = "paletteColor"
Me.paletteColor.Size = New System.Drawing.Size(688, 24)
Me.paletteColor.TabIndex = 1
'
'mnuMain
'
Me.mnuMain.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuFile, Me.mnuTools})
'
'mnuFile
'
Me.mnuFile.Index = 0
Me.mnuFile.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuFileOpenBackground})
Me.mnuFile.Text = "&File"
'
'mnuFileOpenBackground
'
Me.mnuFileOpenBackground.Index = 0
Me.mnuFileOpenBackground.Text = "Open &Background Image"
'
'mnuTools
'
Me.mnuTools.Index = 1
Me.mnuTools.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuToolsCircle, Me.mnuToolsHollowCircle})
Me.mnuTools.Text = "&Tools"
'
'mnuToolsCircle
'
Me.mnuToolsCircle.Checked = True
Me.mnuToolsCircle.Index = 0
Me.mnuToolsCircle.Text = "&Circle"
'
'mnuToolsHollowCircle
'
Me.mnuToolsHollowCircle.Index = 1
Me.mnuToolsHollowCircle.Text = "&Hollow Circle"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(688, 377)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.paletteColor, Me.canvas})
Me.Menu = Me.mnuMain
Me.Name = "Form1"
Me.Text = "WroxPaint"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub paletteColor_LeftClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles paletteColor.LeftClick
canvas.GraphicLeftColor = paletteColor.LeftColor
End Sub
Private Sub paletteColor_RightClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles paletteColor.RightClick
canvas.GraphicRightColor = paletteColor.RightColor
End Sub
Private Sub mnuToolsCircle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuToolsCircle.Click
canvas.GraphicTool = PaintCanvas.GraphicTools.CirclePen
UpdateMenu()
End Sub
Private Sub mnuToolsHollowCircle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuToolsHollowCircle.Click
canvas.GraphicTool = PaintCanvas.GraphicTools.HollowCirclePen
UpdateMenu()
End Sub
Private Function UpdateMenu()
If canvas.GraphicTool = PaintCanvas.GraphicTools.CirclePen Then
mnuToolsCircle.Checked = True
Else
mnuToolsCircle.Checked = False
End If
If canvas.GraphicTool = PaintCanvas.GraphicTools.HollowCirclePen Then
mnuToolsHollowCircle.Checked = True
Else
mnuToolsHollowCircle.Checked = False
End If
End Function
Private Sub mnuFileOpenBackground_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileOpenBackground.Click
OpenBackgroundImage()
End Sub
Public Sub OpenBackgroundImage()
If dlgOpenBackground.ShowDialog() = DialogResult.OK Then
Dim backgroundImage As Image = _
Image.FromFile(dlgOpenBackground.FileName)
canvas.BackgroundImage = backgroundImage
End If
End Sub
End Class
Public Class PaintCanvas
Inherits System.Windows.Forms.UserControl
Public Enum GraphicTools As Integer
CirclePen = 0
HollowCirclePen = 1
End Enum
Public Enum GraphicSizes As Integer
Small = 4
Medium = 10
Large = 20
End Enum
Public GraphicsItems As New ArrayList()
Public GraphicTool As GraphicTools = GraphicTools.CirclePen
Public GraphicSize As GraphicSizes = GraphicSizes.Medium
Public GraphicLeftColor As Color = Color.Black
Public GraphicRightColor As Color = Color.White
Private Sub DoMousePaint(ByVal e As MouseEventArgs)
Dim newItem As GraphicsItem
Dim useColor As Color = GraphicLeftColor
If e.Button = MouseButtons.Right Then useColor = GraphicRightColor
Select Case GraphicTool
Case GraphicTools.CirclePen, GraphicTools.HollowCirclePen
Dim filled As Boolean = True
If GraphicTool = GraphicTools.HollowCirclePen Then _
filled = False
Dim circle As New GraphicsCircle()
circle.SetPoint(e.X, e.Y, GraphicSize, useColor, filled)
newItem = circle
End Select
If Not newItem Is Nothing Then
GraphicsItems.Add(newItem)
Invalidate(newItem.Rectangle)
End If
End Sub
#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
'UserControl 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.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
'
'PaintCanvas
'
Me.BackColor = System.Drawing.Color.WhiteSmoke
Me.Name = "PaintCanvas"
End Sub
#End Region
Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
If e.Button = MouseButtons.Left Or e.Button = MouseButtons.Right Then
DoMousePaint(e)
End If
End Sub
Protected Overrides Sub OnMouseMove(ByVal e As System.Windows.Forms.MouseEventArgs)
If e.Button = MouseButtons.Left Or e.Button = MouseButtons.Right Then
DoMousePaint(e)
End If
End Sub
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
Dim item As GraphicsItem
For Each item In GraphicsItems
If e.ClipRectangle.IntersectsWith(item.Rectangle) = True Then
item.Draw(e.Graphics)
End If
Next
End Sub
Protected Overrides Sub OnPaintBackground(ByVal pevent As System.Windows.Forms.PaintEventArgs)
Dim backgroundBrush As New SolidBrush(BackColor)
pevent.Graphics.FillRectangle(backgroundBrush, pevent.ClipRectangle)
If Not BackgroundImage Is Nothing Then
Dim clientRectangle As New Rectangle(0, 0, Width, Height)
Dim imageWidth As Integer = BackgroundImage.Width
Dim imageHeight As Integer = BackgroundImage.Height
Dim ratio As Double = _
CType(imageHeight, Double) / CType(imageWidth, Double)
If imageWidth > clientRectangle.Width Then
imageWidth = clientRectangle.Width
imageHeight = CType(CType(imageWidth, Double) * ratio, Integer)
Else
imageHeight = clientRectangle.Height
imageWidth = CType(CType(imageHeight, Double) / ratio, Integer)
End If
Dim imageLocation As New Point( _
(clientRectangle.Width / 2) - (imageWidth / 2), _
(clientRectangle.Height / 2) - (imageHeight / 2))
Dim imageSize As New Size(imageWidth, imageHeight)
Dim imageRectangle As New Rectangle(imageLocation, imageSize)
pevent.Graphics.DrawImage(BackgroundImage, imageRectangle)
End If
End Sub
Protected Overrides Sub OnResize(ByVal e As System.EventArgs)
Invalidate()
End Sub
End Class
Public MustInherit Class GraphicsItem
Public Color As Color
Public IsFilled As Boolean
Public Rectangle As Rectangle
Public MustOverride Sub Draw(ByVal graphics As Graphics)
Public Sub SetPoint(ByVal x As Integer, ByVal y As Integer, _
ByVal graphicSize As Integer, _
ByVal graphicColor As Color, ByVal graphicIsFilled As Boolean)
Rectangle = New _
Rectangle(x - (graphicSize / 2), y - (graphicSize / 2), _
graphicSize, graphicSize)
Color = graphicColor
IsFilled = graphicIsFilled
End Sub
End Class
Public Class GraphicsCircle
Inherits GraphicsItem
Public Overrides Sub Draw(ByVal graphics As _
System.Drawing.Graphics)
If IsFilled = True Then
Dim brush As New SolidBrush(Me.Color)
graphics.FillEllipse(brush, Me.Rectangle)
Else
Dim pen As New Pen(Me.Color)
Dim drawRectangle As Rectangle = Me.Rectangle
drawRectangle.Inflate(-1, -1)
graphics.DrawEllipse(pen, drawRectangle)
End If
End Sub
End Class
Public Class ColorPaletteButton
Public Color As Color = Color.Black
Public Rectangle As Rectangle
Public ButtonAssignment As ButtonAssignments = ButtonAssignments.None
Public Enum ButtonAssignments As Integer
None = 0
LeftButton = 1
RightButton = 2
End Enum
Public Sub New(ByVal newColor As Color)
Color = newColor
End Sub
Public Sub SetPosition(ByVal x As Integer, ByVal y As Integer, _
ByVal buttonSize As Integer)
Rectangle = New Rectangle(x, y, buttonSize, buttonSize)
End Sub
Public Sub Draw(ByVal graphics As Graphics)
Dim brush As New SolidBrush(Color)
graphics.FillRectangle(brush, Rectangle)
Dim pen As New Pen(Color.Black)
graphics.DrawRectangle(pen, Rectangle)
If ButtonAssignment <> ButtonAssignments.None Then
Dim font As New Font("verdana", 8, FontStyle.Bold)
Dim buttonText As String = "L"
If ButtonAssignment = ButtonAssignments.RightButton Then _
buttonText = "R"
Dim fontBrush As SolidBrush
If Color.R < 100 Or Color.B < 100 Or Color.G < 100 Then
fontBrush = New SolidBrush(Color.White)
Else
fontBrush = New SolidBrush(Color.Black)
End If
graphics.DrawString(buttonText, font, fontBrush, _
Rectangle.Left, Rectangle.Top)
End If
End Sub
End Class
Public Class ColorPalette
Inherits System.Windows.Forms.UserControl
Public Buttons As New ArrayList()
Public ButtonSize As Integer = 15
Public ButtonSpacing As Integer = 5
Public LeftColor As Color = Color.Black
Public RightColor As Color = Color.White
Private leftButton As ColorPaletteButton
Private rightButton As ColorPaletteButton
Event LeftClick(ByVal sender As Object, ByVal e As EventArgs)
Event RightClick(ByVal sender As Object, ByVal e As EventArgs)
Public Function GetButtonAt(ByVal x As Integer, ByVal y As Integer) _
As ColorPaletteButton
Dim button As ColorPaletteButton
For Each button In Buttons
If button.Rectangle.Contains(x, y) = True Then Return button
Next
End Function
Public Function AddColor(ByVal newColor As Color) As _
ColorPaletteButton
Dim button As New ColorPaletteButton(newColor)
Buttons.Add(button)
End Function
#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
' add the colors...
AddColor(Color.Black)
AddColor(Color.White)
AddColor(Color.Red)
AddColor(Color.Blue)
AddColor(Color.Green)
AddColor(Color.Gray)
AddColor(Color.DarkRed)
AddColor(Color.DarkBlue)
AddColor(Color.DarkGreen)
AddColor(Color.DarkGray)
AddColor(Color.FromArgb(208, 112, 222))
AddColor(CType(SystemBrushes.ActiveCaption, SolidBrush).Color)
End Sub
'UserControl 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 dlgColor As System.Windows.Forms.ColorDialog
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.dlgColor = New System.Windows.Forms.ColorDialog()
'
'ColorPalette
'
Me.Name = "ColorPalette"
End Sub
#End Region
Protected Overrides Sub OnResize(ByVal e As System.EventArgs)
Dim x As Integer, y As Integer
Dim button As ColorPaletteButton
For Each button In Buttons
button.SetPosition(x, y, ButtonSize)
x += (ButtonSize + ButtonSpacing)
If x + ButtonSize > Width Then
y += (ButtonSize + ButtonSpacing)
x = 0
End If
Next
Invalidate()
End Sub
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
Dim button As ColorPaletteButton
For Each button In Buttons
If e.ClipRectangle.IntersectsWith(button.Rectangle) Then
button.Draw(e.Graphics)
End If
Next
End Sub
Protected Overrides Sub OnMouseUp(ByVal e As System.Windows.Forms.MouseEventArgs)
Dim button As ColorPaletteButton = GetButtonAt(e.X, e.Y)
If Not button Is Nothing Then
If e.Button = MouseButtons.Left Then
If Not button Is rightButton Then
LeftColor = button.Color
If Not leftButton Is Nothing Then
leftButton.ButtonAssignment = _
ColorPaletteButton.ButtonAssignments.None
Invalidate(leftButton.Rectangle)
End If
button.ButtonAssignment = _
ColorPaletteButton.ButtonAssignments.LeftButton
Invalidate(button.Rectangle)
leftButton = button
RaiseEvent LeftClick(Me, New EventArgs())
End If
End If
If e.Button = MouseButtons.Right Then
If Not button Is leftButton Then
RightColor = button.Color
If Not rightButton Is Nothing Then
rightButton.ButtonAssignment = _
ColorPaletteButton.ButtonAssignments.None
Invalidate(rightButton.Rectangle)
End If
button.ButtonAssignment = _
ColorPaletteButton.ButtonAssignments.RightButton
Invalidate(button.Rectangle)
rightButton = button
RaiseEvent RightClick(Me, New EventArgs())
End If
End If
Else
If dlgColor.ShowDialog = DialogResult.OK Then
AddColor(dlgColor.Color)
OnResize(New EventArgs())
End If
End If
End Sub
End Class
|