VB.Net Tutorial/2D Graphics/Coordinate
Содержание
Draw Coordinate
<source lang="vbnet">Imports System Imports System.Drawing Imports System.Collections Imports System.ruponentModel Imports System.Windows.Forms Imports System.Data Imports System.Drawing.Drawing2D public class DrawCoordinate
public Shared Sub Main Application.Run(New Form1) End Sub
End class Public Class Form1
Inherits System.Windows.Forms.Form Private startPoint As New Point(50, 217) Private endPoint As New Point(50, 217) Public Sub New() MyBase.New() InitializeComponent() End Sub Private components As System.ruponentModel.IContainer Friend WithEvents Button1 As System.Windows.Forms.Button Friend WithEvents CheckBox1 As System.Windows.Forms.CheckBox <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.Button1 = New System.Windows.Forms.Button Me.CheckBox1 = New System.Windows.Forms.CheckBox Me.SuspendLayout() " "Button1 " Me.Button1.BackColor = System.Drawing.SystemColors.Desktop Me.Button1.Location = New System.Drawing.Point(352, 16) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size(96, 24) Me.Button1.TabIndex = 0 Me.Button1.Text = "Clear All" " "CheckBox1 " Me.CheckBox1.Location = New System.Drawing.Point(352, 56) Me.CheckBox1.Name = "CheckBox1" Me.CheckBox1.TabIndex = 1 Me.CheckBox1.Text = "Rectangle" " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.BackColor = System.Drawing.SystemColors.ActiveCaptionText Me.ClientSize = New System.Drawing.Size(480, 317) Me.Controls.Add(Me.CheckBox1) Me.Controls.Add(Me.Button1) Me.Text = "" Me.ResumeLayout(False) End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click startPoint.X = 50 startPoint.Y = 217 endPoint.X = 50 endPoint.Y = 217 Me.Invalidate(Me.ClientRectangle) End Sub Protected Overrides Sub OnMouseDown(ByVal e As MouseEventArgs) If e.Button = MouseButtons.Left Then Dim g1 As Graphics = Me.CreateGraphics() Dim linePen As New Pen(Color.Green, 1) Dim ellipsePen As New Pen(Color.Red, 1) startPoint = endPoint endPoint = New Point(e.X, e.Y) g1.DrawLine(linePen, startPoint, endPoint) If checkBox1.Checked Then g1.DrawRectangle(ellipsePen, e.X - 2, e.Y - 2, 4, 4) Else g1.DrawEllipse(ellipsePen, e.X - 2, e.Y - 2, 4, 4) End If linePen.Dispose() ellipsePen.Dispose() g1.Dispose() End If End Sub Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) Dim g As Graphics = e.Graphics Dim vertFont As New Font("Verdana", 10, FontStyle.Bold) Dim horzFont As New Font("Verdana", 10, FontStyle.Bold) Dim vertBrush As New SolidBrush(Color.Black) Dim horzBrush As New SolidBrush(Color.Blue) Dim blackPen As New Pen(Color.Black, 2) Dim bluePen As New Pen(Color.Blue, 2) " Drawing a vertical and a horizontal line g.DrawLine(blackPen, 50, 220, 50, 25) g.DrawLine(bluePen, 50, 220, 250, 220) "X axis drawing g.DrawString("0", horzFont, horzBrush, 30, 220) g.DrawString("1", horzFont, horzBrush, 50, 220) g.DrawString("2", horzFont, horzBrush, 70, 220) g.DrawString("3", horzFont, horzBrush, 90, 220) g.DrawString("4", horzFont, horzBrush, 110, 220) g.DrawString("5", horzFont, horzBrush, 130, 220) g.DrawString("6", horzFont, horzBrush, 150, 220) g.DrawString("7", horzFont, horzBrush, 170, 220) g.DrawString("8", horzFont, horzBrush, 190, 220) g.DrawString("9", horzFont, horzBrush, 210, 220) g.DrawString("10", horzFont, horzBrush, 230, 220) " Drawing vertical strings Dim vertStrFormat As New StringFormat vertStrFormat.FormatFlags = StringFormatFlags.DirectionVertical g.DrawString("-", horzFont, horzBrush, 50, 212, vertStrFormat) g.DrawString("-", horzFont, horzBrush, 70, 212, vertStrFormat) g.DrawString("-", horzFont, horzBrush, 90, 212, vertStrFormat) g.DrawString("-", horzFont, horzBrush, 110, 212, vertStrFormat) g.DrawString("-", horzFont, horzBrush, 130, 212, vertStrFormat) g.DrawString("-", horzFont, horzBrush, 150, 212, vertStrFormat) g.DrawString("-", horzFont, horzBrush, 170, 212, vertStrFormat) g.DrawString("-", horzFont, horzBrush, 190, 212, vertStrFormat) g.DrawString("-", horzFont, horzBrush, 210, 212, vertStrFormat) g.DrawString("-", horzFont, horzBrush, 230, 212, vertStrFormat) "Y axis drawing g.DrawString("100-", vertFont, vertBrush, 20, 20) g.DrawString("90 -", vertFont, vertBrush, 25, 40) g.DrawString("80 -", vertFont, vertBrush, 25, 60) g.DrawString("70 -", vertFont, vertBrush, 25, 80) g.DrawString("60 -", vertFont, vertBrush, 25, 100) g.DrawString("50 -", vertFont, vertBrush, 25, 120) g.DrawString("40 -", vertFont, vertBrush, 25, 140) g.DrawString("30 -", vertFont, vertBrush, 25, 160) g.DrawString("20 -", vertFont, vertBrush, 25, 180) g.DrawString("10 -", vertFont, vertBrush, 25, 200) " Dispose vertFont.Dispose() horzFont.Dispose() vertBrush.Dispose() horzBrush.Dispose() blackPen.Dispose() bluePen.Dispose() End Sub
End Class</source>
Draw line using float coordinates
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class DrawLineUsingFloatCoordinates
public Shared Sub Main Application.Run(New Form1) End Sub
End class public class Form1
Inherits System.Windows.Forms.Form Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) Dim blackPen As New Pen(Color.Black, 4) " Dim x1 As Single = 20.0F Dim y1 As Single = 20.0F Dim x2 As Single = 200.0F Dim y2 As Single = 20.0F e.Graphics.DrawLine(blackPen, x1, y1, x2, y2) End Sub Public Sub New() MyBase.New() Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 273) Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen End Sub
End Class</source>
Draw line using integer coordinates
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class DrawLineUsingIntegerCoordinates
public Shared Sub Main Application.Run(New Form1) End Sub
End class public class Form1
Inherits System.Windows.Forms.Form Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) Dim blackPen As New Pen(Color.Black, 4) " Dim X3 As Integer = 60 Dim Y3 As Integer = 40 Dim X4 As Integer = 250 Dim Y4 As Integer = 100 e.Graphics.DrawLine(blackPen, X3, Y3, X4, Y4) End Sub Public Sub New() MyBase.New() Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 273) Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen End Sub
End Class</source>
GDI+ Coordinate System
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class GDICoordinateSystem
public Shared Sub Main Application.Run(New Form1) End Sub
End class public class Form1
Inherits System.Windows.Forms.Form Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) Dim g As Graphics = e.Graphics g.TranslateTransform(50, 40) Dim vertFont As New Font("Verdana", 10, FontStyle.Regular) Dim horzFont As New Font("Verdana", 10, FontStyle.Regular) Dim blackBrush As New SolidBrush(Color.Black) Dim blackPen As New Pen(Color.Black, 1) Dim bluePen As New Pen(Color.Blue, 1)
" Page Coordinate ***** "X axis drawing g.DrawString("200", horzFont, blackBrush, 200, 5) g.DrawString("100", horzFont, blackBrush, 100, 5) g.DrawString("X", horzFont, blackBrush, 250, 0) " Drawing vertical strings Dim vertStrFormat As New StringFormat vertStrFormat.FormatFlags = StringFormatFlags.DirectionVertical "Y axis drawing g.DrawString("100", vertFont, blackBrush, 0, 100) g.DrawString("200", vertFont, blackBrush, 0, 200) g.DrawString("Y", vertFont, blackBrush, 0, 250) " Drawing a vertical and a horizontal line g.DrawLine(blackPen, 0, 0, 0, 250) g.DrawLine(blackPen, 0, 0, 250, 0) Dim A As New Point(0, 0) Dim B As New Point(120, 80) g.DrawLine(Pens.Black, A, B) End Sub Public Sub New() MyBase.New() Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 273) Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen End Sub
End Class</source>