RotateTransform won't draw anything my paint event

red_ghost

Member
Joined
Mar 13, 2007
Messages
13
Location
Green Bay WI
Programming Experience
3-5
I'm writing a screen for a program to design labels. When you click on the panel where you wish to place text, I open a form I created displaying Text, Font, and Direction. Direction will be in degrees for rotation. It works fine when I put in 0, but otherwise nothing happens. Nothing will be redrawn, not even any of the other graphics. How I am doing this is I have a class for each object type and a collection. When I draw them on screen in the click event I add them to the collection and let the paint method draw it. But when I have something rotated it doesn't draw anything. If I enter 180, it will alternate drawing the rectangle I use to display the limits of the label and the text. Here's the code:

Mouse Down:
VB.NET:
    Private Sub pnlprv_Mousedown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles pnlprv.MouseDown
        If e.Button = Windows.Forms.MouseButtons.Left Then
            Select Case mode
                Case ""
                Case "drawtext"
                    Dim ftext As New frmText
                    ftext.ShowDialog()
                    If ftext.cncl = False Then
                        Dim f As Font
                        Select Case ftext.cboFont.Text
                            Case "D"
                                f = New Font("Courier New", 19 / num, FontStyle.Regular, GraphicsUnit.Pixel)

                        End Select
                        Dim sfmat As New StringFormat
                        sfmat.Trimming = StringTrimming.Character
                        myGraphics.RotateTransform(ftext.rotate)
                        myGraphics.DrawString(ftext.txtText.Text, f, Brushes.Black, e.X, e.Y, sfmat)
                        Dim dt As New drawntext(e.X, e.Y, f, ftext.txtText.Text, Brushes.Black, sfmat, ftext.Rotate)
                        textcol.Add(dt)
                        myGraphics.RotateTransform(0)

                        Dim dir As String
                        If ftext.Rotate = 0 Then
                            dir = "N"
                        ElseIf ftext.Rotate = 90 Then
                            dir = "R"
                        ElseIf ftext.Rotate = 180 Then
                            dir = "I"
                        Else
                            dir = "B"
                        End If
                        Dim fontstring As String = vbCrLf & "^FO" & e.X * num & "," & e.Y * num & "^A0" & dir & ",0,0^FD " & dt.Text & "^FS" & vbCrLf & vbCrLf
                        txtCode.Text = txtCode.Text.Replace(vbCrLf & vbCrLf, fontstring)
                    End If
                    mode = ""
                    Me.Cursor = Cursors.Arrow
            End Select
        ElseIf e.Button = Windows.Forms.MouseButtons.Right Then
            mode = ""
            Me.Cursor = Cursors.Arrow
        End If
    End Sub

Paint event:
VB.NET:
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        Dim x As Integer = 0
        RECTANGLEFORM = e.ClipRectangle
        For x = 1 To rectcol.Count
            Dim recta As drawnrect = rectcol.Item(x)
            Dim rec As New Rectangle(recta.x, recta.y, recta.width, recta.height)
            myGraphics.DrawRectangle(recta.pen, rec)
            Dim recs() As Rectangle = {rec}
            myGraphics.FillRectangles(recta.fill, recs)
        Next
        For x = 1 To textcol.Count
            Dim dt As drawntext = textcol.Item(x)
            myGraphics.RotateTransform(dt.Rotate)
            myGraphics.DrawString(dt.Text, dt.Font, dt.Brush, dt.x, dt.y, dt.SFmat)
            myGraphics.RotateTransform(0)
        Next
    End Sub
 
figured it out. I guess it rotates around 0,0 and it was probably rotating everything. I took out the code in the click to draw to the screen and simply add to the collection and use an invalidate in the click. I am using the following to rotate around a specified point (x and y of where you click).

VB.NET:
Private Sub RotateAround(ByVal gr As Graphics, _
        ByVal X As Single, ByVal Y As Single, ByVal degrees As Single)
        ' Translate to center the rectangle at the origin.
        gr.TranslateTransform(-X, -Y, Drawing2D.MatrixOrder.Append)

        ' Rotate.
        gr.RotateTransform(degrees, Drawing2D.MatrixOrder.Append)

        ' Translate the result back to its original position.
        gr.TranslateTransform(X, Y, Drawing2D.MatrixOrder.Append)
    End Sub

I grabbed that off of DevX.

SO when I draw it I do the following:

VB.NET:
            Dim dt As drawntext = textcol.Item(x)
            RotateAround(myGraphics, dt.x, dt.y, dt.Rotate)
            myGraphics.DrawString(dt.Text, dt.Font, dt.Brush, dt.x, dt.y, dt.SFmat)
            RotateAround(myGraphics, dt.x, dt.y, -dt.Rotate)
 
Back
Top