CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2003
    Location
    Ellesmere Port, UK
    Posts
    26

    Drawing a triangles and squares?

    Is it possible in VB .NET to draw shapes in a picture box and then fil lthe shapes with a colour?

    For example, could I get the basic outline of a house (square with a triangle on top) and get the square filled brick red and the triangle filled grey?

  2. #2
    Join Date
    Jan 2003
    Location
    Amsterdam, Netherlands
    Posts
    97
    Yes, that is possible.
    To try the following example, add the code to the codesection of a form.

    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
    Dim gr As System.Drawing.Graphics = e.Graphics
    Dim x, y As Integer

    x = 100
    y = 100

    ' Define shape of the house
    Dim p(2) As System.Drawing.Point
    p(0) = New System.Drawing.Point(x - 10, y)
    p(1) = New System.Drawing.Point(x + 110, y)
    p(2) = New System.Drawing.Point(x + 50, y - 50)
    Dim r As New System.Drawing.Rectangle(x, y, 100, 100)

    ' Draw house
    Dim bGray As New System.Drawing.SolidBrush(System.Drawing.Color.Gray)
    Dim bBrick As New System.Drawing.Drawing2D.HatchBrush(System.Drawing.Drawing2D.HatchStyle.HorizontalBrick, System.Drawing.Color.Orange, System.Drawing.Color.Red)
    gr.FillPolygon(bGray, p)
    gr.FillRectangle(bBrick, r)

    ' Draw house outline
    Dim pBlack As New System.Drawing.Pen(System.Drawing.Color.Black, 2)
    gr.DrawPolygon(pBlack, p)
    gr.DrawRectangle(pBlack, r)
    End Sub


    Danny

  3. #3
    Join Date
    Jan 2003
    Location
    Ellesmere Port, UK
    Posts
    26

    Thumbs up

    wow that is really cool

    .NET \o/

    Thanks alot

  4. #4
    Join Date
    Jan 2003
    Location
    Ellesmere Port, UK
    Posts
    26
    Ok, Ive expanded the code.

    I now have it, that when a user clicks within a certain area (ie, a part of the drawing) the color dialog shows and they select a new color. Ive used a system.drawing.color variable to store the color, but i just cant repaint the form

    Code:
        Dim cSleave1 As System.Drawing.Color
        Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
            Dim gr As System.Drawing.Graphics = e.Graphics
            ' Define shape of the shirt
            Dim s1(3) As System.Drawing.Point
            s1(0) = New System.Drawing.Point(100, 100)
            s1(1) = New System.Drawing.Point(55, 123)
            s1(2) = New System.Drawing.Point(55, 158)
            s1(3) = New System.Drawing.Point(100, 135)
    
            Dim s2(3) As System.Drawing.Point
            s2(0) = New System.Drawing.Point(200, 100)
            s2(1) = New System.Drawing.Point(245, 123)
            s2(2) = New System.Drawing.Point(245, 158)
            s2(3) = New System.Drawing.Point(200, 135)
            Dim r As New System.Drawing.Rectangle(100, 100, 100, 125)
    
            ' Draw house
            cSleave1 = System.Drawing.Color.Blue
            Dim bS1 As New System.Drawing.SolidBrush(cSleave1)
            Dim bS2 As New System.Drawing.SolidBrush(System.Drawing.Color.Blue)
            Dim bSH As New System.Drawing.SolidBrush(System.Drawing.Color.WhiteSmoke)
            gr.FillPolygon(bS1, s1)
            gr.FillPolygon(bS2, s2)
            gr.FillRectangle(bSH, r)
            ' Draw house outline
            Dim pBlack As New System.Drawing.Pen(System.Drawing.Color.Black, 2)
            gr.DrawPolygon(pBlack, s1)
            gr.DrawPolygon(pBlack, s2)
            gr.DrawRectangle(pBlack, r)
        End Sub
    
        Protected Overrides Sub OnClick(ByVal e As System.EventArgs)
            If Me.MousePosition.X > 54 & Me.MousePosition.X < 101 & Me.MousePosition.Y > 99 & Me.MousePosition.Y < 158 Then
                dColour.ShowDialog()
                cSleave1 = dColour.Color
    
                ' REPAINT THE GOD **** FORM! :D
            End If
        End Sub
    Any ideas how to update the color?

  5. #5
    Join Date
    Jan 2003
    Location
    Amsterdam, Netherlands
    Posts
    97
    You can try something like this.
    I made 4 changes to your code.


    Code:
        ' 1: set default color for sleave
        Dim cSleave1 As System.Drawing.Color = System.Drawing.Color.Blue
        Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
            Dim gr As System.Drawing.Graphics = e.Graphics
            ' Define shape of the shirt
            Dim s1(3) As System.Drawing.Point
            s1(0) = New System.Drawing.Point(100, 100)
            s1(1) = New System.Drawing.Point(55, 123)
            s1(2) = New System.Drawing.Point(55, 158)
            s1(3) = New System.Drawing.Point(100, 135)
    
            Dim s2(3) As System.Drawing.Point
            s2(0) = New System.Drawing.Point(200, 100)
            s2(1) = New System.Drawing.Point(245, 123)
            s2(2) = New System.Drawing.Point(245, 158)
            s2(3) = New System.Drawing.Point(200, 135)
            Dim r As New System.Drawing.Rectangle(100, 100, 100, 125)
    
            ' Draw house
            ' 2: Don't need to set color here anymore
            'cSleave1 = System.Drawing.Color.Blue
            Dim bS1 As New System.Drawing.SolidBrush(cSleave1)
            Dim bS2 As New System.Drawing.SolidBrush(System.Drawing.Color.Blue)
            Dim bSH As New System.Drawing.SolidBrush(System.Drawing.Color.WhiteSmoke)
            gr.FillPolygon(bS1, s1)
            gr.FillPolygon(bS2, s2)
            gr.FillRectangle(bSH, r)
            ' Draw house outline
            Dim pBlack As New System.Drawing.Pen(System.Drawing.Color.Black, 2)
            gr.DrawPolygon(pBlack, s1)
            gr.DrawPolygon(pBlack, s2)
            gr.DrawRectangle(pBlack, r)
        End Sub
    
        ' 3: Use OnMouseDown instate of OnClick
        Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
            If e.X > 54 And e.X < 101 And e.Y > 99 And e.Y < 158 Then
                dColour.ShowDialog()
                cSleave1 = dColour.Color
    
                ' 4: Redraw your Form
                Me.Refresh()
            End If
        End Sub
    Danny

  6. #6
    Join Date
    Jan 2003
    Location
    Ellesmere Port, UK
    Posts
    26

    Thumbs up

    again, thanks alot

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured