VB.Net Tutorial/2D Graphics/Pen Alignment
Версия от 16:40, 26 мая 2010; (обсуждение)
List all Pen Alignment
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class AlignmentsDemo
public Shared Sub Main
Application.Run(New AlignmentsForm)
End Sub
End class
Public Class AlignmentsForm
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 = "AlignmentsForm"
End Sub
#End Region
Private Sub AlignmentsForm_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles MyBase.Paint
Dim g As Graphics = e.Graphics
Dim alignNames As String() = System.Enum.GetNames(GetType(PenAlignment))
Array.Sort(alignNames)
Dim x As Integer = 0
Dim y As Integer = 0
Dim width As Integer = Me.ClientRectangle.Width
Dim height As Integer = Me.ClientRectangle.Height
Dim blackBrush As Brush = Brushes.Black
Dim redPen As Pen = Pens.Red
Dim alignName As String
For Each alignName In alignNames
Dim pen As Pen = New Pen(Color.White, 13)
g.DrawEllipse(pen, x + 10, y + 10, width - 20, height - 20)
g.DrawEllipse(redPen, x + 10, y + 10, width - 20, height - 20)
Dim format As StringFormat = New StringFormat()
format.Alignment = StringAlignment.Center
format.LineAlignment = StringAlignment.Center
g.DrawString(alignName, Me.Font, blackBrush, New RectangleF(x, y, width, height), format)
y = y + 15
Next
End Sub
End Class
PenAlignment Illustration
Imports System
Imports System.Drawing
Imports System.Collections
Imports System.ruponentModel
Imports System.Windows.Forms
Imports System.Data
Imports System.Drawing.Drawing2D
public class PenAlignmentComboBox
public Shared Sub Main
Application.Run(New Form1)
End Sub
End class
Public Class Form1
Inherits System.Windows.Forms.Form
Private penColor As Color = Color.Red
Dim penWidth As Integer = 5
#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.
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents ComboBox1 As System.Windows.Forms.ruboBox
Friend WithEvents numericUpDown1 As System.Windows.Forms.NumericUpDown
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents ColorBtn As System.Windows.Forms.Button
Friend WithEvents Draw As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Label1 = New System.Windows.Forms.Label
Me.Label2 = New System.Windows.Forms.Label
Me.ruboBox1 = New System.Windows.Forms.ruboBox
Me.numericUpDown1 = New System.Windows.Forms.NumericUpDown
Me.Label3 = New System.Windows.Forms.Label
Me.ColorBtn = New System.Windows.Forms.Button
Me.Draw = New System.Windows.Forms.Button
CType(Me.numericUpDown1, System.ruponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
"
"Label1
"
Me.Label1.Location = New System.Drawing.Point(8, 16)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(80, 23)
Me.Label1.TabIndex = 0
Me.Label1.Text = "Pen Alignment"
"
"Label2
"
Me.Label2.Location = New System.Drawing.Point(8, 56)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(72, 23)
Me.Label2.TabIndex = 1
Me.Label2.Text = "Pen Width"
"
"ComboBox1
"
Me.ruboBox1.Location = New System.Drawing.Point(96, 16)
Me.ruboBox1.Name = "ComboBox1"
Me.ruboBox1.Size = New System.Drawing.Size(121, 21)
Me.ruboBox1.TabIndex = 2
Me.ruboBox1.Text = "ComboBox1"
"
"numericUpDown1
"
Me.numericUpDown1.Location = New System.Drawing.Point(96, 56)
Me.numericUpDown1.Name = "numericUpDown1"
Me.numericUpDown1.Size = New System.Drawing.Size(32, 20)
Me.numericUpDown1.TabIndex = 7
"
"Label3
"
Me.Label3.Location = New System.Drawing.Point(136, 56)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(56, 23)
Me.Label3.TabIndex = 8
Me.Label3.Text = "Pen Color"
"
"ColorBtn
"
Me.ColorBtn.BackColor = System.Drawing.Color.FromArgb(CType(192, Byte), CType(0, Byte), CType(0, Byte))
Me.ColorBtn.FlatStyle = System.Windows.Forms.FlatStyle.Flat
Me.ColorBtn.Location = New System.Drawing.Point(192, 56)
Me.ColorBtn.Name = "ColorBtn"
Me.ColorBtn.Size = New System.Drawing.Size(24, 16)
Me.ColorBtn.TabIndex = 9
"
"Draw
"
Me.Draw.Location = New System.Drawing.Point(56, 104)
Me.Draw.Name = "Draw"
Me.Draw.Size = New System.Drawing.Size(120, 40)
Me.Draw.TabIndex = 10
Me.Draw.Text = "DrawGraphics"
"
"Form1
"
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(360, 349)
Me.Controls.Add(Me.Draw)
Me.Controls.Add(Me.ColorBtn)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.numericUpDown1)
Me.Controls.Add(Me.ruboBox1)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.Label1)
Me.Name = "Form1"
Me.Text = "Pen Alignment and Pen Types Sample"
CType(Me.numericUpDown1, System.ruponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AddPenAlignments()
End Sub
Private Sub AddPenAlignments()
comboBox1.Items.Add(PenAlignment.Center)
comboBox1.Text = PenAlignment.Center.ToString()
comboBox1.Items.Add(PenAlignment.Inset)
comboBox1.Items.Add(PenAlignment.Left)
comboBox1.Items.Add(PenAlignment.Outset)
comboBox1.Items.Add(PenAlignment.Right)
End Sub "AddPenAlignments
Private Sub Draw_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Draw.Click
Dim g As Graphics = Me.CreateGraphics()
g.Clear(Me.BackColor)
Dim pn1 As New Pen(Color.Blue, 3)
pn1.Width = CSng(numericUpDown1.Value)
pn1.Color = ColorBtn.BackColor
Dim str As String = ComboBox1.Text
Select Case str
Case "Center"
pn1.Alignment = PenAlignment.Center
Case "Inset"
pn1.Alignment = PenAlignment.Inset
Case "Left"
pn1.Alignment = PenAlignment.Left
Case "Outset"
pn1.Alignment = PenAlignment.Outset
Case "Right"
pn1.Alignment = PenAlignment.Right
Case Else
End Select
g.DrawRectangle(pn1, 80, 150, 150, 150)
Dim brush As New LinearGradientBrush(New Rectangle(10, 10, 20, 20), Color.Blue, Color.Green, 45.0F)
g.FillRectangle(brush, 90, 160, 130, 130)
pn1.Dispose()
g.Dispose()
End Sub
End Class