VB.Net/2D/Pen
Содержание
Draw Curve with Different pens
Imports System
Imports System.Windows.Forms
Imports System.Drawing.Text
Imports System.Drawing
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
" Define the curve"s control points.
Dim pts() As Point = { _
New Point(200, 30), _
New Point(200, 150), _
New Point(10, 50), _
New Point(250, 200), _
New Point(20, 200), _
New Point(250, 50), _
New Point(100, 250) _
}
" Draw the closed curve.
e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
For tension As Single = 0 To 2 Step 0.25
Dim curve_pen As New Pen(Color.Black, tension * 4 + 1)
e.Graphics.DrawClosedCurve(curve_pen, pts, tension, _
Drawing2D.FillMode.Alternate)
Next tension
" Draw rectangles on the control points.
For i As Integer = 0 To pts.Length - 1
e.Graphics.FillRectangle(Brushes.White, pts(i).X - 2, pts(i).Y - 2, 5, 5)
e.Graphics.DrawRectangle(Pens.Black, pts(i).X - 2, pts(i).Y - 2, 5, 5)
Next i
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(364, 309)
Me.Name = "Form1"
Me.Text = "Title"
Me.ResumeLayout(False)
End Sub
End Class
Draw thick rectangle outline in red
Imports System
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
Imports System.Drawing.Text
Public Class MainClass
Shared Sub Main()
Dim myform As Form = New FrmDrawShapes()
Application.Run(myform)
End Sub " Main
End Class
Public Class FrmDrawShapes
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.Container
"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()
"
"frmDrawShapes
"
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(520, 197)
Me.Name = "frmDrawShapes"
Me.Text = "Drawing Shapes"
End Sub
#End Region
" draw various shapes on form
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
" references to object we will use
Dim graphicsObject As Graphics = e.Graphics
" pen and location for red outline rectangle
Dim thickRedPen As Pen = New Pen(Color.Red, 10)
Dim drawArea2 As Rectangle = New Rectangle(80, 30, 65, 100)
" draw thick rectangle outline in red
graphicsObject.DrawRectangle(thickRedPen, drawArea2)
End Sub " OnPaint
End Class
Draw with Pen Compound Array
Imports System.Drawing.Drawing2D
Imports System
Imports System.Drawing.Text
Imports System.Drawing
Imports System.Windows.Forms
Public Class MainClass
Shared Sub Main()
Dim form1 As Form = New Form1()
Application.Run(form1)
End Sub " ValidationError
End Class
Public Class Form1
Private Sub Form1_Paint(ByVal sender As Object, _
ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim the_pen As New Pen(Color.Black, 10)
the_pen.rupoundArray = New Single() {0.0, 0.2, 0.4, 0.6, 0.8, 1.0}
e.Graphics.DrawRectangle(the_pen, 20, 50, 100, 100)
e.Graphics.DrawEllipse(the_pen, 150, 50, 100, 100)
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(292, 273)
Me.Name = "Form1"
Me.Text = "TransformArrow"
Me.ResumeLayout(False)
End Sub
End Class
Pen Compound Array Demo 1
Imports System.Drawing.Drawing2D
Imports System
Imports System.Drawing.Text
Imports System.Drawing
Imports System.Windows.Forms
Public Class MainClass
Shared Sub Main()
Dim form1 As Form = New Form1()
Application.Run(form1)
End Sub " ValidationError
End Class
Public Class Form1
Private Sub Form1_Paint(ByVal sender As Object, _
ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim the_pen As New Pen(Color.Black, 10)
the_pen.rupoundArray = New Single() {0.0, 0.45, 0.55, 1.0}
e.Graphics.DrawLine(the_pen, 10, 20, 400, 20)
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(292, 273)
Me.Name = "Form1"
Me.Text = "TransformArrow"
Me.ResumeLayout(False)
End Sub
End Class
Pen"s Compound Array
Imports System
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Text
Public Class MainClass
Shared Sub Main()
Dim form1 As Form1 = new Form1
Application.Run(form1)
End Sub
End Class
Public Class Form1
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()
Me.SuspendLayout()
"
"Form1
"
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(392, 213)
Me.Controls.AddRange(New System.Windows.Forms.Control() {})
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
Dim G As Graphics = e.Graphics
Dim P1 As Pen = New Pen(Color.Blue, 10)
"compoundarray
Dim P3 As Pen = New Pen(Color.Blue, 20)
Dim lines() As Single = {0.0, 0.1, 0.9, 1.0}
P3.rupoundArray = lines
G.DrawLine(P3, 20, CInt(Me.Height / 2), Me.Width - 20, CInt(Me.Height / 2))
End Sub
End Class
Pen width Demo
Imports System.Drawing.Drawing2D
Imports System
Imports System.Drawing.Text
Imports System.Drawing
Imports System.Windows.Forms
Public Class MainClass
Shared Sub Main()
Dim form1 As Form = New Form1()
Application.Run(form1)
End Sub " ValidationError
End Class
Public Class Form1
Private Sub Form1_Paint(ByVal sender As Object, _
ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim the_pen As New Pen(Color.Black, 1)
e.Graphics.DrawLine(the_pen, 50, 10, 200, 10)
the_pen.Width = 5
e.Graphics.DrawLine(the_pen, 50, 40, 200, 40)
the_pen.Width = 10
e.Graphics.DrawLine(the_pen, 50, 100, 200, 100)
the_pen.Width = 20
e.Graphics.DrawLine(the_pen, 50, 200, 200, 200)
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(292, 273)
Me.Name = "Form1"
Me.Text = "TransformArrow"
Me.ResumeLayout(False)
End Sub
End Class
Transformed Pen
Imports System.Drawing.Drawing2D
Imports System
Imports System.Drawing.Text
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Math
Public Class MainClass
Shared Sub Main()
Dim form1 As Form = New Form1()
Application.Run(form1)
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
Const Cx As Integer = 120
Const Cy As Integer = 120
Const R As Integer = 100
Const PEN_WID As Integer = 10
Dim the_pen As New Pen(Color.Black, PEN_WID)
the_pen.ScaleTransform(1, 4, MatrixOrder.Append)
the_pen.RotateTransform(45, MatrixOrder.Append)
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias
e.Graphics.ScaleTransform(1, 4, MatrixOrder.Append)
e.Graphics.RotateTransform(45, MatrixOrder.Append)
For angle As Single = 0 To 2 * PI Step PI / 8
Dim graphics_state As GraphicsState = e.Graphics.Save
e.Graphics.TranslateTransform( _
CSng(Cx + R * Cos(angle)), _
CSng(Cy + R * Sin(angle)), _
MatrixOrder.Append)
e.Graphics.DrawEllipse(Pens.White, -PEN_WID , -PEN_WID , PEN_WID, PEN_WID)
e.Graphics.Restore(graphics_state)
Next angle
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(200, 200)
Me.Name = "Form1"
Me.Text = ""
Me.ResumeLayout(False)
End Sub
End Class