Mirror Text: use Drawing2D.Matrix to transform Graphics : Matrix « 2D Graphics « VB.Net Tutorial






'Visual Basic 2005 Cookbook Solutions for VB 2005 Programmers
'by Tim Patrick (Author), John Craig (Author)
'# Publisher: O'Reilly Media, Inc. (September 21, 2006)
'# Language: English
'# ISBN-10: 0596101775
'# ISBN-13: 978-0596101770


Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms

public class MirrorText
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class

Public Class Form1
    Private Const QuoteText As String = "www.java2s.com"

    Private Sub VerticalMirror_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VerticalMirror.CheckedChanged
        MirroredText.Invalidate()
    End Sub

    Private Sub MirroredText_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MirroredText.Paint
        Dim drawingArea As Rectangle
        Dim saveState As Drawing2D.GraphicsState
        Dim mirrorMatrix As Drawing2D.Matrix

        e.Graphics.Clear(Color.White)

        If (VerticalMirror.Checked = True) Then
            drawingArea = New Rectangle(5, 5, _
                (MirroredText.ClientRectangle.Width \ 2) - 10, _
                MirroredText.ClientRectangle.Height - 10)

            e.Graphics.DrawLine(Pens.Black, MirroredText.ClientRectangle.Width \ 2, _
                5, MirroredText.ClientRectangle.Width \ 2, _
                MirroredText.ClientRectangle.Height - 10)
        Else
            drawingArea = New Rectangle(5, 5, _
                MirroredText.ClientRectangle.Width - 10, _
                (MirroredText.ClientRectangle.Height \ 2) - 10)

            e.Graphics.DrawLine(Pens.Black, 5, _
                MirroredText.ClientRectangle.Height \ 2, _
                MirroredText.ClientRectangle.Width - 10, _
                MirroredText.ClientRectangle.Height \ 2)
        End If

        e.Graphics.DrawString(QuoteText, MirroredText.Font, _
            Brushes.Black, drawingArea)

        saveState = e.Graphics.Save()
        If (VerticalMirror.Checked = True) Then
            mirrorMatrix = New Drawing2D.Matrix(-1, 0, 0, 1, MirroredText.ClientRectangle.Width, 0)
        Else
            mirrorMatrix = New Drawing2D.Matrix(1, 0, 0, -1, 0, MirroredText.ClientRectangle.Height)
        End If

        e.Graphics.Transform = mirrorMatrix

        e.Graphics.DrawString(QuoteText, MirroredText.Font, _
            Brushes.Black, drawingArea)
        e.Graphics.Restore(saveState)
    End Sub
End Class


<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        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.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.VerticalMirror = New System.Windows.Forms.RadioButton
        Me.MirroredText = New System.Windows.Forms.PictureBox
        Me.HorizontalMirror = New System.Windows.Forms.RadioButton
        CType(Me.MirroredText, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'VerticalMirror
        '
        Me.VerticalMirror.AutoSize = True
        Me.VerticalMirror.Checked = True
        Me.VerticalMirror.Location = New System.Drawing.Point(8, 8)
        Me.VerticalMirror.Name = "VerticalMirror"
        Me.VerticalMirror.Size = New System.Drawing.Size(60, 17)
        Me.VerticalMirror.TabIndex = 0
        Me.VerticalMirror.TabStop = True
        Me.VerticalMirror.Text = "&Vertical"
        Me.VerticalMirror.UseVisualStyleBackColor = True
        '
        'MirroredText
        '
        Me.MirroredText.BackColor = System.Drawing.Color.White
        Me.MirroredText.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.MirroredText.Location = New System.Drawing.Point(104, 8)
        Me.MirroredText.Name = "MirroredText"
        Me.MirroredText.Size = New System.Drawing.Size(304, 168)
        Me.MirroredText.TabIndex = 1
        Me.MirroredText.TabStop = False
        '
        'HorizontalMirror
        '
        Me.HorizontalMirror.AutoSize = True
        Me.HorizontalMirror.Location = New System.Drawing.Point(8, 32)
        Me.HorizontalMirror.Name = "HorizontalMirror"
        Me.HorizontalMirror.Size = New System.Drawing.Size(72, 17)
        Me.HorizontalMirror.TabIndex = 1
        Me.HorizontalMirror.Text = "&Horizontal"
        Me.HorizontalMirror.UseVisualStyleBackColor = True
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(419, 187)
        Me.Controls.Add(Me.HorizontalMirror)
        Me.Controls.Add(Me.MirroredText)
        Me.Controls.Add(Me.VerticalMirror)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
        Me.MaximizeBox = False
        Me.Name = "Form1"
        Me.Text = "Mirror Text"
        CType(Me.MirroredText, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub
    Friend WithEvents VerticalMirror As System.Windows.Forms.RadioButton
    Friend WithEvents MirroredText As System.Windows.Forms.PictureBox
    Friend WithEvents HorizontalMirror As System.Windows.Forms.RadioButton

End Class








17.87.Matrix
17.87.1.Matrix: TranslateMatrix: Translate
17.87.2.Matrix: RotateAtMatrix: RotateAt
17.87.3.Matrix.InvertMatrix.Invert
17.87.4.Matrix: Shear(0,0)Matrix: Shear(0,0)
17.87.5.Matrix: Shear(0.5,0.0)Matrix: Shear(0.5,0.0)
17.87.6.Matrix: Shear(1,0)Matrix: Shear(1,0)
17.87.7.Matrix Operation Invert
17.87.8.Matrix Operation Multiply
17.87.9.Mirror Text: use Drawing2D.Matrix to transform Graphics
17.87.10.Assign Matrix to Graphics.TransformAssign Matrix to Graphics.Transform