VB.Net Tutorial/2D Graphics/Polygon — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 19:40, 26 мая 2010
Draw a Polygon
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class DrawPolygon
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 points(3) As System.Drawing.Point points(0) = New Point(120, 60) "Top Left of Trapezoid points(1) = New Point(180, 60) "Top Right of Trapezoid points(2) = New Point(240, 120) "Bottom Right of Trapezoid points(3) = New Point(60, 120) "Bottom Left of Trapezoid e.Graphics.DrawPolygon(New Pen(Color.Blue), points) 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>
Trapezoid
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class DrawPolygon
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 points(3) As System.Drawing.Point points(0) = New Point(120, 60) "Top Left of Trapezoid points(1) = New Point(180, 60) "Top Right of Trapezoid points(2) = New Point(240, 120) "Bottom Right of Trapezoid points(3) = New Point(60, 120) "Bottom Left of Trapezoid e.Graphics.DrawPolygon(New Pen(Color.Blue), points) 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>
Use mouse to draw a polygon
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms Imports System.Collections public class DrawPolygon
public Shared Sub Main Application.Run(New FrmPolygon) End Sub
End class
Public Class FrmPolygon
Inherits System.Windows.Forms.Form " polygon type options Friend WithEvents filledPolygonRadio As RadioButton Friend WithEvents lineRadio As RadioButton Friend WithEvents polygonRadio As RadioButton " command buttons Friend WithEvents cmdClear As Button Friend WithEvents cmdNewColor As Button Friend WithEvents drawWindow As Panel Friend WithEvents typeGroup As GroupBox
- 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() Me.typeGroup = New System.Windows.Forms.GroupBox() Me.filledPolygonRadio = New System.Windows.Forms.RadioButton() Me.lineRadio = New System.Windows.Forms.RadioButton() Me.polygonRadio = New System.Windows.Forms.RadioButton() Me.drawWindow = New System.Windows.Forms.Panel() Me.cmdClear = New System.Windows.Forms.Button() Me.cmdNewColor = New System.Windows.Forms.Button() Me.typeGroup.SuspendLayout() Me.SuspendLayout() " "typeGroup " Me.typeGroup.Controls.AddRange(New System.Windows.Forms.Control() {Me.filledPolygonRadio, Me.lineRadio, Me.polygonRadio}) Me.typeGroup.Location = New System.Drawing.Point(232, 8) Me.typeGroup.Name = "typeGroup" Me.typeGroup.Size = New System.Drawing.Size(128, 104) Me.typeGroup.TabIndex = 0 Me.typeGroup.TabStop = False Me.typeGroup.Text = "Select Type" " "filledPolygonRadio " Me.filledPolygonRadio.Location = New System.Drawing.Point(8, 80) Me.filledPolygonRadio.Name = "filledPolygonRadio" Me.filledPolygonRadio.Size = New System.Drawing.Size(112, 16) Me.filledPolygonRadio.TabIndex = 2 Me.filledPolygonRadio.Text = "Filled Polygon" " "lineRadio " Me.lineRadio.Location = New System.Drawing.Point(8, 24) Me.lineRadio.Name = "lineRadio" Me.lineRadio.Size = New System.Drawing.Size(112, 16) Me.lineRadio.TabIndex = 1 Me.lineRadio.Text = "Lines" " "polygonRadio " Me.polygonRadio.Location = New System.Drawing.Point(8, 48) Me.polygonRadio.Name = "polygonRadio" Me.polygonRadio.Size = New System.Drawing.Size(112, 24) Me.polygonRadio.TabIndex = 0 Me.polygonRadio.Text = "Polygon" " "drawWindow " Me.drawWindow.BackColor = System.Drawing.SystemColors.Window Me.drawWindow.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.drawWindow.Name = "drawWindow" Me.drawWindow.Size = New System.Drawing.Size(232, 264) Me.drawWindow.TabIndex = 1 " "cmdClear " Me.cmdClear.Location = New System.Drawing.Point(240, 160) Me.cmdClear.Name = "cmdClear" Me.cmdClear.Size = New System.Drawing.Size(112, 24) Me.cmdClear.TabIndex = 2 Me.cmdClear.Text = "Clear" " "cmdNewColor " Me.cmdNewColor.Location = New System.Drawing.Point(240, 192) Me.cmdNewColor.Name = "cmdNewColor" Me.cmdNewColor.Size = New System.Drawing.Size(112, 24) Me.cmdNewColor.TabIndex = 3 Me.cmdNewColor.Text = "Change Color" " "FrmPolygon " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(360, 269) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.cmdNewColor, Me.cmdClear, Me.drawWindow, Me.typeGroup}) Me.Name = "FrmPolygon" Me.Text = "Drawing Polygons" Me.typeGroup.ResumeLayout(False) Me.ResumeLayout(False) End Sub
- End Region
Private mPoints As ArrayList = New ArrayList() Dim mPen As Pen = New Pen(Color.DarkBlue) Dim mBrush As SolidBrush = New SolidBrush(Color.DarkBlue) Private Sub drawWindow_MouseDown(ByVal sender _ As Object, ByVal e As _ System.Windows.Forms.MouseEventArgs) _ Handles drawWindow.MouseDown mPoints.Add(New Point(e.X, e.Y)) drawWindow.Invalidate() " refresh panel End Sub " drawWindow_MouseDown Private Sub drawWindow_Paint(ByVal sender As Object, _ ByVal e As System.Windows.Forms.PaintEventArgs) _ Handles drawWindow.Paint Dim graphicsObject As Graphics = e.Graphics If mPoints.Count < 1 Then Return End If Dim pointArray() As Point = mPoints.ToArray(mPoints(0).GetType()) If polygonRadio.Checked Then graphicsObject.DrawPolygon(mPen, pointArray) ElseIf lineRadio.Checked Then graphicsObject.DrawLines(mPen, pointArray) ElseIf filledPolygonRadio.Checked Then graphicsObject.FillPolygon(mBrush, pointArray) End If
End Sub Private Sub cmdClear_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles cmdClear.Click mPoints.Clear() drawWindow.Invalidate() End Sub Private Sub polygonRadio_CheckedChanged(ByVal sender As _ System.Object, ByVal e As System.EventArgs) _ Handles polygonRadio.CheckedChanged drawWindow.Invalidate() End Sub Private Sub lineRadio_CheckedChanged(ByVal sender As _ System.Object, ByVal e As System.EventArgs) _ Handles lineRadio.CheckedChanged drawWindow.Invalidate() End Sub Private Sub filledPolygonRadio_CheckedChanged(ByVal sender _ As System.Object, ByVal e As System.EventArgs) _ Handles filledPolygonRadio.CheckedChanged drawWindow.Invalidate() End Sub Private Sub cmdNewColor_Click(ByVal sender As _ System.Object, ByVal e As System.EventArgs) _ Handles cmdNewColor.Click Dim colorBox As ColorDialog = New ColorDialog() Dim result As DialogResult = colorBox.ShowDialog() If result = DialogResult.Cancel Then Return End If mPen.Color = colorBox.Color mBrush.Color = colorBox.Color drawWindow.Invalidate() End Sub
End Class</source>