VB.Net/2D/Transform — различия между версиями

Материал из VB Эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 12:41, 26 мая 2010

Combine TranslateTransform and ScaleTransform

Imports System
Imports System.Windows.Forms
Imports System.Drawing.Text
Imports System.Drawing
Imports System.Drawing.Drawing2D
Public Class MainClass
    
    Shared Sub Main(ByVal args As String())
        Dim myform As Form = New Form1()
        Application.Run(myform)
    End Sub
End Class

Public Class Form1
    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        " Draw the original diamond.
        DrawDiamond(e.Graphics)
        " Translate 
        e.Graphics.TranslateTransform(-50, -50, MatrixOrder.Append)
        " Scale by a factor of 2.
        e.Graphics.ScaleTransform(2, 2, MatrixOrder.Append)
        " Translate the center back 
        e.Graphics.TranslateTransform(50, 50, MatrixOrder.Append)
        " Draw the diamond.
        DrawDiamond(e.Graphics)
    End Sub
    Private Sub DrawDiamond(ByVal gr As Graphics)
        Dim pts() As Point = { _
            New Point(75, 125), _
            New Point(125, 75), _
            New Point(175, 125), _
            New Point(125, 175) _
        }
        gr.DrawPolygon(Pens.Black, pts)
    End Sub
End Class

<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _
Partial Public Class Form1
    Inherits System.Windows.Forms.Form
    "Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overloads 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.ruponentModel.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.SuspendLayout()
        "
        "Form1
        "
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(500, 500)
        Me.Name = "Form1"
        Me.Text = "Title"
        Me.ResumeLayout(False)
    End Sub
End Class


Path Transform Matrix Demo

Imports System
Imports System.ruponentModel
Imports System.Windows.Forms
Imports System.Data
Imports System.Configuration
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Text
Imports System.Globalization
Imports System.Text
Imports System.Collections
Public Class MainClass
    
    Shared Sub Main()
        
        Dim myform As Form = New PathTranslationForm()
        Application.Run(myform)
    End Sub
End Class




Public Class PathTranslationForm
    Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
    Public Sub New()
        MyBase.New()
        "This call is required by the Windows Form Designer.
        InitializeComponent()
        "Add any initialization after the InitializeComponent() call
    End Sub
    "Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub
    "Required by the Windows Form Designer
    Private components As System.ruponentModel.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()
        components = New System.ruponentModel.Container()
        Me.Text = "PathTranslationForm"
    End Sub
#End Region

    Sub PathTranslationForm_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles MyBase.Paint
        Dim g As Graphics = e.Graphics
        Dim path As GraphicsPath = New GraphicsPath()
        Dim rect As RectangleF = New RectangleF(0, 0, 50, 50)
        Dim format As StringFormat = New StringFormat()
        format.Alignment = StringAlignment.Center
        format.LineAlignment = StringAlignment.Center
        path.AddRectangle(rect)
        path.AddString("P", Me.Font.FontFamily, CInt(Me.Font.Style), Me.Font.Height+30, rect, format)
        
        g.DrawPath(Pens.Black, path)
        Dim matrix As Matrix = New Matrix()
        matrix.Translate(50, 50)
        path.Transform(matrix)
        g.DrawPath(Pens.Black, path)
    End Sub
End Class


Transform Demo

Imports System
Imports System.ruponentModel
Imports System.Windows.Forms
Imports System.Data
Imports System.Configuration
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Text
Imports System.Globalization
Imports System.Text
Imports System.Collections
Public Class MainClass
    
    Shared Sub Main()
        
        Dim myform As Form = New TranslationForm()
        Application.Run(myform)
    End Sub
End Class



Public Class TranslationForm
    Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
    Public Sub New()
        MyBase.New()
        "This call is required by the Windows Form Designer.
        InitializeComponent()
        "Add any initialization after the InitializeComponent() call
    End Sub
    "Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub
    "Required by the Windows Form Designer
    Private components As System.ruponentModel.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()
        components = New System.ruponentModel.Container()
        Me.Text = "TranslationForm"
    End Sub
#End Region

    Sub DrawLabeledRect(ByVal g As Graphics, ByVal label As String)
    End Sub
    Sub TranlationForm_Pain(ByVal sender As Object, ByVal e As PaintEventArgs) Handles MyBase.Paint
        Dim g As Graphics = e.Graphics
        Dim rect As RectangleF = New RectangleF(0, 0, 125, 125)
        Dim format As StringFormat = New StringFormat()
        format.Alignment = StringAlignment.Center
        format.LineAlignment = StringAlignment.Center
        g.FillRectangle(Brushes.White, rect)
        g.DrawRectangle(Pens.Black, rect.X, rect.Y, rect.Width, rect.Height)
        g.DrawString("Translate(0,0)", Me.Font, Brushes.Black, rect, format)

        Dim matrix As Matrix = New Matrix()
        matrix.Translate(150, 150)
        g.Transform = matrix
        DrawLabeledRect(g, "Translate(150, 150)")
    End Sub
End Class


TranslateTransform Demo

Imports System
Imports System.Windows.Forms
Imports System.Drawing.Text
Imports System.Drawing
Imports System.Drawing.Drawing2D
Public Class MainClass
    
    Shared Sub Main(ByVal args As String())
        Dim myform As Form = New Form1()
        Application.Run(myform)
    End Sub
End Class

Public Class Form1
    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        Dim device_rect As New Rectangle(50, 50, 150, 150)
        e.Graphics.DrawRectangle(Pens.Black, device_rect)
        Dim world_rect As New Rectangle(0, 0, 1, 1)
        MapRectangles(e.Graphics, world_rect, device_rect)
        DrawSmiley(e.Graphics)
    End Sub
    Private Sub DrawSmiley(ByVal gr As Graphics)
        Dim the_pen As New Pen(Color.Black, 0)
        gr.FillEllipse(Brushes.Yellow, 0, 0, 1, 1)              " Face.
        gr.DrawEllipse(the_pen, 0, 0, 10, 10)
    End Sub
    " Map a world coordinate rectangle to a device coordinate rectangle.
    Private Sub MapRectangles(ByVal gr As Graphics, ByVal world_rect As Rectangle, ByVal device_rect As Rectangle)
        gr.ResetTransform()
        gr.TranslateTransform( _
            CSng(-(world_rect.X + world_rect.Width / 2)), _
            CSng(-(world_rect.Y + world_rect.Height / 2)), _
            MatrixOrder.Append)
    End Sub
End Class

<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _
Partial Public Class Form1
    Inherits System.Windows.Forms.Form
    "Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overloads 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.ruponentModel.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.SuspendLayout()
        "
        "Form1
        "
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(564, 209)
        Me.Name = "Form1"
        Me.Text = "Title"
        Me.ResumeLayout(False)
    End Sub
End Class


TranslateTransform: Draw Ellipse Path

Imports System
Imports System.ruponentModel
Imports System.Windows.Forms
Imports System.Data
Imports System.Configuration
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Text
Imports System.Globalization
Imports System.Text
Imports System.Collections
Public Class MainClass
    
    Shared Sub Main()
        
        Dim myform As Form = New PathFlattenForm()
        Application.Run(myform)
    End Sub
End Class



Public Class PathFlattenForm
    Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
    Public Sub New()
        MyBase.New()
        "This call is required by the Windows Form Designer.
        InitializeComponent()
        "Add any initialization after the InitializeComponent() call
    End Sub
    "Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub
    "Required by the Windows Form Designer
    Private components As System.ruponentModel.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()
        "
        "PathFlattenForm
        "
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(416, 126)
        Me.Name = "PathFlattenForm"
        Me.Text = "PathFlattenForm"
    End Sub
#End Region

    Sub PathFlattenForm_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles MyBase.Paint
        Dim g As Graphics = e.Graphics
        Dim width As Single = Me.ClientSize.Width
        Dim height As Single = Me.ClientSize.Height
        Dim rect As RectangleF = New RectangleF(0, 0, width, height)
        Dim format As StringFormat = New StringFormat()
        format.Alignment = StringAlignment.Center
        format.LineAlignment = StringAlignment.Center
        Dim path As GraphicsPath = New GraphicsPath()
        path.Reset()
        path.AddEllipse(rect)
        g.DrawPath(Pens.Black, path)
        g.DrawString("Ellipse", Me.Font, Brushes.Black, rect, format)
        g.TranslateTransform(width, 0)
    End Sub
End Class