I cannot draw rectangle

jimmylost

Member
Joined
Sep 9, 2005
Messages
6
Programming Experience
Beginner
I drag in the panel , but no rectangle appear :eek:
VB.NET:
  Dim MyPoint As ArrayList = New ArrayList
    Dim DragFlag As Boolean = False
    Dim StartPt, EndPt, FalseStartPt, FalseEndPt As PointF
    Dim myshapetype As ArrayList = New ArrayList
    Dim centreX As Integer = 448 / 2
    Dim centreY As Integer = 228 / 2
    Dim curstatus As String = "draw"
    Dim TrueX, TrueY
    Private Sub panel1_Mousedown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown
        'If curstatus = "Move" Or curstatus = "ZoomRegion" Then
        '    DragFlag = True
        'End If
        TrueX = (e.X - Panel1.Width / 2) * Width / Panel1.Width + centreX
        TrueY = (e.Y - Panel1.Height / 2) * Height / Panel1.Height + centreY
        StartPt = New PointF(TrueX, TrueY)
        EndPt = New PointF(TrueX, TrueY)
        FalseStartPt = New PointF(e.X, e.Y) 'coordinate after zoomming in or out
        FalseEndPt = New PointF(e.X, e.Y)
        If curstatus = "Draw" Then
            DragFlag = True
            MyPoint.Add(New PointF(TrueX, TrueY))
        ElseIf curstatus = "ZoomIn" Then
            TrueX = centreX
            TrueY = centreY
            Width = Width * Width / Panel1.Width
            Height = Height * Height / Panel1.Height
            Panel1.Invalidate()
        End If
    End Sub
    Private Sub panel1_Mousemove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseMove
        'If curstatus = "Move" Or curstatus = "ZoomRegion" Then
        '    Panel1.Invalidate()
        'End If
        Label1.Text = "X:" & e.X & "Y:" & e.Y
        TrueX = (e.X - Panel1.Width / 2) * Width / Panel1.Width + centreX
        TrueY = (e.Y - Panel1.Height / 2) * Height / Panel1.Height + centreY
        If DragFlag Then
            EndPt = New PointF(TrueX, TrueY)
            FalseEndPt = New PointF(e.X, e.Y)
            If curstatus = "Draw" Then
                Panel1.Invalidate()
            End If
        End If
    End Sub
    Private Sub panel1_Mouseup(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseUp
        'If curstatus = "Move" Then
        '    DragFlag = False
        '    centreX = -TrueX * Width / Panel1.Width + centreX
        '    centreY = -TrueY * Height / Panel1.Height + centreY
        '    Panel1.Invalidate()
        'End If
        'If curstatus = "ZoomRegion" Then
        '    DragFlag = False
        '    Dim w, h
        '    w = Math.Abs(FalseEndPt.X - FalseStartPt.X)
        '    h = Math.Abs(FalseEndPt.Y - FalseStartPt.Y)
        '    If w > 0 And h > 0 Then
        '        Dim TempX, TempY, Ratio
        '        TempX = (FalseStartPt.X + FalseEndPt.X) / 2
        '        TempY = (FalseStartPt.Y + FalseEndPt.Y) / 2
        '        centreX = (TempX - Panel1.Width / 2) * Width / Panel1.Width + centreX
        '        centreY = (TempY - Panel1.Height / 2) * Height / Panel1.Height + centreY
        '        If w > h Then
        '            Ratio = Width / Panel1.Width
        '        Else
        '            Ratio = Height / Panel1.Height
        '        End If
        '        Width = w * Ratio : Height = h * Ratio
        '        Panel1.Invalidate()
        '    End If
        'End If
        Label1.Text = "X:" & e.X & "Y:" & e.Y
        TrueX = (e.X - Panel1.Width / 2) * Width / Panel1.Width + centreX
        TrueY = (e.Y - Panel1.Height / 2) * Height / Panel1.Height + centreY
        EndPt = New PointF(TrueX, TrueY)
        FalseEndPt = New PointF(e.X, e.Y)
        If curstatus = "Draw" Then
            DragFlag = False
            MyPoint.Add(New PointF(TrueX, TrueY))
            myshapetype.Add("Rectangle")
        End If
        Panel1.Invalidate()
    End Sub
    Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
        Dim g As Graphics = e.Graphics
        Dim i As Integer
        Dim mypen As New Pen(Color.DarkBlue)
        'If curstatus = "ZoomRegion" And DragFlag Then
        '    DrawRectangle(g, FalseStartPt, FalseEndPt, mypen)
        'End If
        g.TranslateTransform(-centreX, -centreY, System.Drawing.Drawing2D.MatrixOrder.Prepend)
        g.ScaleTransform(Panel1.Width / Width, Panel1.Width / Width, System.Drawing.Drawing2D.MatrixOrder.Append)
        g.TranslateTransform(Panel1.Width / 2, Panel1.Height / 2, System.Drawing.Drawing2D.MatrixOrder.Append)
        For i = 0 To MyPoint.Count Step 2
            If i + 1 < MyPoint.Count Then
                Dim TempStartPt As PointF = MyPoint(i)
                Dim TempEndPt As PointF = MyPoint(i + 1)
                DrawRectangle(g, TempStartPt, TempEndPt, mypen)
            End If
        Next
        If curstatus = "Draw" And DragFlag = True Then
            DrawRectangle(g, StartPt, EndPt, mypen)
        End If
    End Sub
    Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click
        shape.Text = "Rectangle"
        shape.ImageIndex = 4
    End Sub
    Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
        shape.Text = "Ellipse"
        shape.ImageIndex = 0
    End Sub
    Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click
        shape.Text = "Line"
        shape.ImageIndex = 1
    End Sub
    Sub DrawRectangle(ByVal g As Graphics, ByVal sPt As PointF, ByVal ePt As PointF, ByVal pen As Pen)
        Dim minX, minY, maxX, maxY
        If sPt.X > ePt.X Then
            If maxX - minX > 0 And maxY - minY > 0 Then
                g.DrawRectangle(pen, minX, minY, maxX - minX, maxY - minY)
            End If
        End If
    End Sub
End Class
 
You have a lot going on there. I didn't go through all your code. My recommendation would be to temporarily comment out anything that is not essential for the drawing of the rectangle. Take it down to the basics. Then step through line by line to see where it is going wrong. Here is the bare basics that you need to draw a rectangle on a Panel:
VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] point1, point2 [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Point
[/SIZE][SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Panel1_MouseDown([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.MouseEventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] Panel1.MouseDown
point1 = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Point(e.X, e.Y)
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Panel1_MouseUp([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.MouseEventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] Panel1.MouseUp
point2 = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Point(e.X, e.Y)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] g [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Graphics = Panel1.CreateGraphics
g.DrawRectangle([/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Pen(Color.Black), point1.X, point1.Y, point2.X - point1.X, point2.Y - point1.Y)
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]

 
Back
Top