ComponentOne Preview Classic for WinForms: Preview Classic for WinForms Task-Based Help > Drawing Text at an Angle

Drawing Text at an Angle

To draw text at an angle, use the Graphics object and create a subroutine to rotate text:

1.   Add the following code to the Form_Load event:

      Visual Basic

Me.C1PrintDocument1.StartDoc()

Me.C1PrintDocument1.RenderBlockGraphicsBegin()

 

' Declare the graphics object.

Dim g As System.Drawing.Graphics

g = Me.C1PrintDocument1.CurrentBlockGraphics

 

Dim fontb = New Font("Arial", 12, FontStyle.Bold)

 

' Subroutine to alter text angle.

RotateText(g, fontb, "Hello World", -45, Brushes.CadetBlue, 10, 100)

 

Me.C1PrintDocument1.RenderBlockGraphicsEnd()

Me.C1PrintDocument1.EndDoc()

      C#

this.c1PrintDocument1.StartDoc();

this.c1PrintDocument1.RenderBlockGraphicsBegin();

 

// Declare the graphics object.

System.Drawing.Graphics g;

g = this.c1PrintDocument1.CurrentBlockGraphics;

 

Font fontb = new Font("Arial", 12, FontStyle.Bold);

 

// Subroutine to alter text angle.

RotateText(g, fontb, "Hello World", -45, Brushes.CadetBlue, 10, 100);

 

this.c1PrintDocument1.RenderBlockGraphicsEnd();

this.c1PrintDocument1.EndDoc();

2.   Add the RotateText subroutine:

      Visual Basic

Public Sub RotateText(ByVal g As Graphics, ByVal f As Font, ByVal s As String, ByVal angle As Single, ByVal b As Brush, ByVal x As Single, ByVal y As Single)

    If angle > 360 Then

        While angle > 360

            angle = angle - 360

        End While

    ElseIf angle < 0 Then

        While angle < 0

            angle = angle + 360

        End While

    End If

 

    ' Create a matrix and rotate it n degrees.

    Dim myMatrix As New System.Drawing.Drawing2D.Matrix

    myMatrix.Rotate(angle, Drawing2D.MatrixOrder.Append)

 

    ' Draw the text to the screen after applying the transform.

    g.Transform = myMatrix

    g.DrawString(s, f, b, x, y)

End Sub

      C#

public void RotateText(Graphics g, Font f, string s, Single angle, Brush b, Single x, Single y)

{

    if (angle > 360)

    {

        while (angle > 360)

        {

            angle = angle - 360;

        }

    }

    else if (angle < 0)

    {

        while (angle < 0)

        {

            angle = angle + 360;

        }

    }

 

    // Create a matrix and rotate it n degrees.

    System.Drawing.Drawing2D.Matrix myMatrix = new System.Drawing.Drawing2D.Matrix();

    myMatrix.Rotate(angle, System.Drawing.Drawing2D.MatrixOrder.Append);

 

    // Draw the text to the screen after applying the transform.

    g.Transform = myMatrix;

    g.DrawString(s, f, b, x, y);

}

This topic illustrates the following:

The text appears at a 45 degree angle.

 


Send comments about this topic to ComponentOne.
Copyright © 1987-2010 ComponentOne LLC. All rights reserved.