Solved

Creating a Drawing Panel in VB.NET

Posted on 2006-07-18
7
3,906 Views
Last Modified: 2008-01-09
Hello All,

I am working on creating a mini-paint program written in VB.NET.  The program is relatively simple in nature, my issue is this...

While I can get the program to paint fine, I now want to add functionality to select one of four different paint colors (Red, Blue, Green, or Black) and one of three bursh sizes (Small, Medium, or Large).

I am completely versed in the stance of homework in Experts-Exchange and am not seeking someone to give me the correct code, instead some clear explanations and guidance would be most appreciated.  My code will be posted next...

==================================================================================================

Public Class FrmPainter
    Inherits System.Windows.Forms.Form

    Dim shouldPaint As Boolean = False

#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 GroupBox1 As System.Windows.Forms.GroupBox
    Friend WithEvents GroupBox2 As System.Windows.Forms.GroupBox
    Friend WithEvents btnBlack As System.Windows.Forms.RadioButton
    Friend WithEvents btnGreen As System.Windows.Forms.RadioButton
    Friend WithEvents btnBlue As System.Windows.Forms.RadioButton
    Friend WithEvents btnRed As System.Windows.Forms.RadioButton
    Friend WithEvents btnLarge As System.Windows.Forms.RadioButton
    Friend WithEvents btnMedium As System.Windows.Forms.RadioButton
    Friend WithEvents btnSmall As System.Windows.Forms.RadioButton
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.GroupBox1 = New System.Windows.Forms.GroupBox
        Me.btnBlack = New System.Windows.Forms.RadioButton
        Me.btnGreen = New System.Windows.Forms.RadioButton
        Me.btnBlue = New System.Windows.Forms.RadioButton
        Me.btnRed = New System.Windows.Forms.RadioButton
        Me.GroupBox2 = New System.Windows.Forms.GroupBox
        Me.btnLarge = New System.Windows.Forms.RadioButton
        Me.btnMedium = New System.Windows.Forms.RadioButton
        Me.btnSmall = New System.Windows.Forms.RadioButton
        Me.GroupBox1.SuspendLayout()
        Me.GroupBox2.SuspendLayout()
        Me.SuspendLayout()
        '
        'GroupBox1
        '
        Me.GroupBox1.BackColor = System.Drawing.SystemColors.ScrollBar
        Me.GroupBox1.Controls.Add(Me.btnBlack)
        Me.GroupBox1.Controls.Add(Me.btnGreen)
        Me.GroupBox1.Controls.Add(Me.btnBlue)
        Me.GroupBox1.Controls.Add(Me.btnRed)
        Me.GroupBox1.Location = New System.Drawing.Point(0, 0)
        Me.GroupBox1.Name = "GroupBox1"
        Me.GroupBox1.Size = New System.Drawing.Size(112, 168)
        Me.GroupBox1.TabIndex = 1
        Me.GroupBox1.TabStop = False
        Me.GroupBox1.Text = "Color"
        '
        'btnBlack
        '
        Me.btnBlack.Location = New System.Drawing.Point(24, 120)
        Me.btnBlack.Name = "btnBlack"
        Me.btnBlack.Size = New System.Drawing.Size(64, 24)
        Me.btnBlack.TabIndex = 3
        Me.btnBlack.Text = "Black"
        '
        'btnGreen
        '
        Me.btnGreen.Location = New System.Drawing.Point(24, 88)
        Me.btnGreen.Name = "btnGreen"
        Me.btnGreen.Size = New System.Drawing.Size(64, 24)
        Me.btnGreen.TabIndex = 2
        Me.btnGreen.Text = "Green"
        '
        'btnBlue
        '
        Me.btnBlue.Location = New System.Drawing.Point(24, 56)
        Me.btnBlue.Name = "btnBlue"
        Me.btnBlue.Size = New System.Drawing.Size(64, 24)
        Me.btnBlue.TabIndex = 1
        Me.btnBlue.Text = "Blue"
        '
        'btnRed
        '
        Me.btnRed.Location = New System.Drawing.Point(24, 24)
        Me.btnRed.Name = "btnRed"
        Me.btnRed.Size = New System.Drawing.Size(64, 24)
        Me.btnRed.TabIndex = 0
        Me.btnRed.Text = "Red"
        '
        'GroupBox2
        '
        Me.GroupBox2.BackColor = System.Drawing.SystemColors.ScrollBar
        Me.GroupBox2.Controls.Add(Me.btnLarge)
        Me.GroupBox2.Controls.Add(Me.btnMedium)
        Me.GroupBox2.Controls.Add(Me.btnSmall)
        Me.GroupBox2.Location = New System.Drawing.Point(0, 168)
        Me.GroupBox2.Name = "GroupBox2"
        Me.GroupBox2.Size = New System.Drawing.Size(112, 144)
        Me.GroupBox2.TabIndex = 2
        Me.GroupBox2.TabStop = False
        Me.GroupBox2.Text = "Size"
        '
        'btnLarge
        '
        Me.btnLarge.Location = New System.Drawing.Point(24, 88)
        Me.btnLarge.Name = "btnLarge"
        Me.btnLarge.Size = New System.Drawing.Size(64, 24)
        Me.btnLarge.TabIndex = 6
        Me.btnLarge.Text = "Large"
        '
        'btnMedium
        '
        Me.btnMedium.Location = New System.Drawing.Point(24, 56)
        Me.btnMedium.Name = "btnMedium"
        Me.btnMedium.Size = New System.Drawing.Size(64, 24)
        Me.btnMedium.TabIndex = 5
        Me.btnMedium.Text = "Medium"
        '
        'btnSmall
        '
        Me.btnSmall.Location = New System.Drawing.Point(24, 24)
        Me.btnSmall.Name = "btnSmall"
        Me.btnSmall.Size = New System.Drawing.Size(64, 24)
        Me.btnSmall.TabIndex = 4
        Me.btnSmall.Text = "Small"
        '
        'FrmPainter
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.BackColor = System.Drawing.SystemColors.Window
        Me.ClientSize = New System.Drawing.Size(536, 310)
        Me.Controls.Add(Me.GroupBox2)
        Me.Controls.Add(Me.GroupBox1)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D
        Me.MaximizeBox = False
        Me.Name = "FrmPainter"
        Me.Text = "Drawing Panel"
        Me.GroupBox1.ResumeLayout(False)
        Me.GroupBox2.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region

    ' draw circle if shouldPaint is True
    Private Sub FrmPainter_MouseMove( _
                ByVal sender As System.Object, _
                ByVal e As System.Windows.Forms.MouseEventArgs) _
                Handles MyBase.MouseMove

        ' paint circle if mouse pressed
        If shouldPaint Then
            Dim graphic As Graphics = CreateGraphics()

            graphic.FillEllipse _
                (New SolidBrush(Color.Red), e.X, e.Y, 4, 4)
        End If

    End Sub 'FrmPainter_MouseMove

    ' set shouldPaint to True
    Private Sub FrmPainter_MouseDown(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.MouseEventArgs) _
        Handles MyBase.MouseDown

        shouldPaint = True
    End Sub ' FrmPainter_MouseDown

    ' set shouldPaint to False
    Private Sub FrmPainter_MouseUp(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.MouseEventArgs) _
        Handles MyBase.MouseUp

        shouldPaint = False
    End Sub ' FrmPainter_MouseUp
End Class

==================================================================================================

Any guidance is most appreciated.  What I think the next step would be is to create some type of case control repitition, but I am having difficulty determining how this would operate.

Kind Regards,

Dan Malek
danielmalek@hotmail.com
0
Comment
Question by:danielmalek
  • 2
7 Comments
 
LVL 14

Expert Comment

by:jjardine
ID: 17135201
I think you would start by adding in either a listbox, dropdownlist, or radio buttons that would allow the user to select which color and brush size they want to use.   Then Once they select that value, store that value in some variables (ie.   _selectedColor as Color and _selectedBrush as Brush)   So when they set their change their settings, do a case statement to map your strings in the list to the actual color to store it.  Then in your code change where you hard code the Color.red or any other reference to color to the new _selectedColor and the same with the Brush.
0
 

Author Comment

by:danielmalek
ID: 17135219
If you look at the above code, I have used radio buttons but am having difficulties creating the logic...
0
 
LVL 14

Accepted Solution

by:
jjardine earned 250 total points
ID: 17135311
How about if you added a Click event for the radio buttons.  Then when that event fires you can update your local variables.  For Example

Public Sub rdoRed_Click(sender as Object, e as eventargs) handles rdoRed.Click
  If rdoRed.checked Then
    _selectedColor = Color.Red
  End If
End Sub
0
 
LVL 21

Assisted Solution

by:oleggold
oleggold earned 250 total points
ID: 17135520
Here's the wonderfull step by step example by SAMS:
http://www.samspublishing.com/articles/article.asp?p=29477&seqNum=10&rl=1
I'm sure You'll find all Your answers down there,e,g:
Draw module:
 Public Sub draw( _
    ByVal shape As String, _
    ByVal width As Integer, _
    ByVal height As Integer, _
    ByVal x As Integer, _
    ByVal y As Integer, _
    ByVal fillType As String, _
    ByVal outlineWeight As Single, _
    ByVal outlineColor As String, _
    ByVal solidColor As String, _
    ByVal blendFrom As String, _
    ByVal blendTo As String, _
    ByVal pattern As String, _
    ByVal patternForeColor As String, _
    ByVal patternBackColor As String, _
    ByVal blendStyle As String, _
    ByVal rotateAngle As Single)

    'purpose: draw a shape to the drawing surface (frmChild)

    'local scope
    Dim myPen As Pen
    Dim myRectangle As Rectangle
    Dim myBrush As Brush
    Dim myHatchStyle As Drawing2D.HatchStyle
    Dim myType As System.Type
    Dim myBlendStyle As Drawing2D.LinearGradientMode
    Dim myState As Drawing2D.GraphicsState

    'get the current state of the graphics object (pre-tranform)
    myState = m_myGraphics.Save()

    'set the rotation transformation value
    m_myGraphics.RotateTransform(angle:=rotateAngle)

    'check what shape to draw
    Select Case shape
      Case "Line"

        'create a new pen instance
        myPen = New Pen(color.FromName(name:=outlineColor), _
          width:=outlineWeight)

        'draw the line to the surface
        m_myGraphics.DrawLine(pen:=myPen, x1:=x, y1:=y, _
          x2:=x + width, y2:=y + height)

      Case "Rectangle", "Ellipse"

        'note: the rectangle and ellipse are very similar
        '   they can use the same code

        'create a new pen myect for the outline of the shape
        myPen = New Pen(color.FromName(name:=outlineColor), _
          width:=outlineWeight)

        'create the rectangle object
        myRectangle = New Rectangle(x:=x, y:=y, width:=width, _
          height:=height)

        'draw the rectangle to the surface
        If shape = "Ellipse" Then

          'draw the ellipse
          m_myGraphics.DrawEllipse(pen:=myPen, _
            rect:=myRectangle)

        Else

          'draw a rectangle
          m_myGraphics.DrawRectangle(pen:=myPen, _
            rect:=myRectangle)

        End If

        'fill the rectangle/ellipse
        If fillType <> "NONE" Then

          'determine brush type to create
          Select Case fillType
            Case "SOLID"

              'create a new solid brush
              myBrush = New SolidBrush( _
                color:=color.FromName(name:=solidColor))

            Case "PATTERN"

              'create the hatch brush
              'note: we use the type object and the parse
              '    method of the enum to return the
              '    value of the enum's member from its
              '    name (string)
              myType = myHatchStyle.GetType()
              myBrush = New System.Drawing.Drawing2D.HatchBrush _
              (myHatchStyle.Parse(enumType:=myType, _
                value:=pattern),
_foreColor:=Color.FromName(name:=patternForeColor),
_backColor:=color.FromName(name:=patternBackColor))

            Case "BLEND"

              'create a blend brush
              'note: we use the type object and the parse
              '    method of the enum to return the
              '    value of the enum's member from its
              '    name (string)
              myType = myBlendStyle.GetType
              myBrush = New _
                System.Drawing.Drawing2D.LinearGradientBrush( _
                rect:=myRectangle, _
                color1:=color.FromName(name:=blendFrom), _
                color2:=color.FromName(name:=blendTo), _
                LinearGradientMode:=myBlendStyle.Parse( _
                enumType:=myType, value:=blendStyle))

          End Select

          'fill the shape
          If shape = "Ellipse" Then

            'draw the ellipse with the correct brush
            m_myGraphics.FillEllipse(brush:=myBrush, _
              rect:=myRectangle)

          Else

            'fill the rectangle with the correct brush
            m_myGraphics.FillRectangle(brush:=myBrush, _
              rect:=myRectangle)

          End If

        End If

    End Select

    'reset the state of the graphics object
    m_myGraphics.Restore(myState)

  End Sub

End Module
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.Net - MemoryMappedFiles - Confirm receipt 2 35
No Data for DropDown List 2 32
how to hit button in form and close application from main() 4 33
vb.net sendkey 4 17
This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question