VB.Net Tutorial/2D Graphics/Rectangle — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 15:54, 26 мая 2010
Содержание
- 1 Create Rectangle From Size And Point
- 2 Create Rectangle From X, Y, Width, Height
- 3 Draw Rectangle
- 4 Draw Rectangle in different size
- 5 Draw Rectangle with rectangle X, Y, Width and Height
- 6 Fill Rectangle
- 7 Get Rectangle properties
- 8 Is inside a Rectangle
- 9 Rectangle.Inflate
- 10 Rectangle.Intersect
- 11 Rectangle.Union
- 12 Rectangle.Union and Intersect
- 13 Use RectangleF
Create Rectangle From Size And Point
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class CreateRectangleFromSizeAndPoint
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 = Me.CreateGraphics() Dim x As Integer = 40 Dim y As Integer = 40 Dim height As Integer = 120 Dim width As Integer = 120 Dim pt As New Point(80, 80) Dim sz As New Size(100, 100) Dim rect1 As New Rectangle(pt, sz) Dim redPen As New Pen(Color.Red, 2) g.DrawRectangle(redPen, rect1) redPen.Dispose() g.Dispose() 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>
Create Rectangle From X, Y, Width, Height
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class CreateRectangleFromXYWidthHeight
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 = Me.CreateGraphics() Dim x As Integer = 40 Dim y As Integer = 40 Dim height As Integer = 120 Dim width As Integer = 120 Dim pt As New Point(80, 80) Dim sz As New Size(100, 100) Dim rect1 As New Rectangle(pt, sz) Dim redPen As New Pen(Color.Red, 2) g.DrawRectangle(redPen, rect1) redPen.Dispose() g.Dispose() 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 Rectangle
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class DrawShapes
public Shared Sub Main Application.Run(New FrmComboBox) End Sub
End class Public Class FrmComboBox
Inherits 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 " contains shape list (circle, square, ellipse, pie) Friend WithEvents cboImage As System.Windows.Forms.ruboBox "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.cboImage = New System.Windows.Forms.ruboBox() Me.SuspendLayout() " "cboImage " Me.cboImage.DropDownWidth = 121 Me.cboImage.Items.AddRange(New Object() {"Circle", "Square", "Ellipse", "Pie", "Filled Circle", "Filled Square", "Filled Ellipse", "Filled Pie"}) Me.cboImage.Location = New System.Drawing.Point(24, 16) Me.cboImage.Name = "cboImage" Me.cboImage.Size = New System.Drawing.Size(121, 21) Me.cboImage.TabIndex = 0 " "FrmComboBox " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 273) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.cboImage}) Me.Name = "FrmComboBox" Me.Text = "ComboBoxTest" Me.ResumeLayout(False) End Sub
- End Region
Private Sub cboImage_SelectedIndexChanged _ (ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles cboImage.SelectedIndexChanged Dim myGraphics As Graphics = MyBase.CreateGraphics() Dim myPen As New Pen(Color.DarkRed) Dim mySolidBrush As New SolidBrush(Color.DarkRed) myGraphics.Clear(Color.White) Select Case cboImage.SelectedIndex Case 0 " case circle is selected myGraphics.DrawEllipse(myPen, 50, 50, 150, 150) Case 1 myGraphics.DrawRectangle(myPen, 50, 50, 150, 150) Case 2 myGraphics.DrawEllipse(myPen, 50, 85, 150, 115) Case 3 myGraphics.DrawPie(myPen, 50, 50, 150, 150, 0, 45) Case 4 myGraphics.FillEllipse(mySolidBrush, 50, 50, 150, 150) Case 5 myGraphics.FillRectangle(mySolidBrush, 50, 50, 150, 150) Case 6 myGraphics.FillEllipse(mySolidBrush, 50, 85, 150, 115) Case 7 myGraphics.FillPie(mySolidBrush, 50, 50, 150, 150, 0, 45) End Select End Sub
End Class</source>
Draw Rectangle in different size
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class DrawRectangleDifferentSize
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 a As Graphics = e.Graphics Dim mypen As Pen Dim myrect As Rectangle mypen = New Pen(System.Drawing.Color.Red, 6) a = Me.CreateGraphics a.Clear(Me.BackColor) a.DrawRectangle(mypen, 10, 10, 150, 150) a.DrawRectangle(mypen, 20, 20, 200, 200) a.DrawRectangle(mypen, 30, 30, 250, 250) a.DrawRectangle(mypen, 40, 40, 300, 300) myrect.X = 100 myrect.Y = 40 myrect.Width = 100 myrect.Height = 100 a.DrawRectangle(mypen, myrect) myrect.X = 120 myrect.Y = 60 myrect.Width = 100 myrect.Height = 100 a.DrawRectangle(mypen, myrect) a.Dispose() mypen.Dispose() 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 Rectangle with rectangle X, Y, Width and Height
<source lang="vbnet">Imports System.Drawing.Drawing2D Imports System.Drawing Imports System.Windows.Forms public class DrawRectangle
public Shared Sub Main Application.Run(New Form1) End Sub
End class Public Class Form1
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint Dim canvas As Graphics = e.Graphics canvas.DrawRectangle(New Pen(Color.Blue, -1), 0, 0, 80, 80) End Sub Private Sub Form1_Resize(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Resize Me.Refresh() End Sub
End Class
<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _ Partial Class Form1
Inherits System.Windows.Forms.Form "Form overrides dispose to clean up the component list. <System.Diagnostics.DebuggerNonUserCode()> _ Protected 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(515, 459) Me.DoubleBuffered = True Me.Name = "Form1" Me.Text = "Using a Path" Me.ResumeLayout(False) End Sub
End Class</source>
Fill Rectangle
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class DrawShapes
public Shared Sub Main Application.Run(New FrmComboBox) End Sub
End class Public Class FrmComboBox
Inherits 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 " contains shape list (circle, square, ellipse, pie) Friend WithEvents cboImage As System.Windows.Forms.ruboBox "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.cboImage = New System.Windows.Forms.ruboBox() Me.SuspendLayout() " "cboImage " Me.cboImage.DropDownWidth = 121 Me.cboImage.Items.AddRange(New Object() {"Circle", "Square", "Ellipse", "Pie", "Filled Circle", "Filled Square", "Filled Ellipse", "Filled Pie"}) Me.cboImage.Location = New System.Drawing.Point(24, 16) Me.cboImage.Name = "cboImage" Me.cboImage.Size = New System.Drawing.Size(121, 21) Me.cboImage.TabIndex = 0 " "FrmComboBox " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 273) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.cboImage}) Me.Name = "FrmComboBox" Me.Text = "ComboBoxTest" Me.ResumeLayout(False) End Sub
- End Region
Private Sub cboImage_SelectedIndexChanged _ (ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles cboImage.SelectedIndexChanged Dim myGraphics As Graphics = MyBase.CreateGraphics() Dim myPen As New Pen(Color.DarkRed) Dim mySolidBrush As New SolidBrush(Color.DarkRed) myGraphics.Clear(Color.White) Select Case cboImage.SelectedIndex Case 0 " case circle is selected myGraphics.DrawEllipse(myPen, 50, 50, 150, 150) Case 1 myGraphics.DrawRectangle(myPen, 50, 50, 150, 150) Case 2 myGraphics.DrawEllipse(myPen, 50, 85, 150, 115) Case 3 myGraphics.DrawPie(myPen, 50, 50, 150, 150, 0, 45) Case 4 myGraphics.FillEllipse(mySolidBrush, 50, 50, 150, 150) Case 5 myGraphics.FillRectangle(mySolidBrush, 50, 50, 150, 150) Case 6 myGraphics.FillEllipse(mySolidBrush, 50, 85, 150, 115) Case 7 myGraphics.FillPie(mySolidBrush, 50, 50, 150, 150, 0, 45) End Select End Sub
End Class</source>
Get Rectangle properties
<source lang="vbnet">Imports System.Drawing.Text Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class Test
public Shared Sub Main Dim pt As New Point(10, 10) Dim sz As New Size(60, 40) Dim rect1 As Rectangle = Rectangle.Empty Dim rect2 As New Rectangle(20, 30, 30, 10) " Set Rectangle properties If rect1.IsEmpty Then rect1.Location = pt rect1.Width = sz.Width rect1.Height = sz.Height End If " Get Rectangle properties Dim str As String = "Location:" + rect1.Location.ToString() str += ", X:" + rect1.X.ToString() str += ", Y:" + rect1.Y.ToString() str += ", Left:" + rect1.Left.ToString() str += ", Right" + rect1.Right.ToString() str += ", Top:" + rect1.Top.ToString() str += ", Bottom:" + rect1.Bottom.ToString() Console.WriteLine(str) End Sub
End class</source>
Location:{X=10,Y=10}, X:10, Y:10, Left:10, Right70, Top:10, Bottom:50
Is inside a Rectangle
<source lang="vbnet">Imports System Imports System.Drawing Imports System.Collections Imports System.ruponentModel Imports System.Windows.Forms Imports System.Data public class RectangleInside
public Shared Sub Main Application.Run(New Form1) End Sub
End class Public Class Form1
Inherits System.Windows.Forms.Form Dim bigRect As New Rectangle(50, 50, 100, 100)
- 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() " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(376, 277) Me.Name = "Form1" Me.Text = "Form1" 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) e.Graphics.FillRectangle(New SolidBrush(Color.Green), bigRect) End Sub Protected Overrides Sub OnMouseDown(ByVal e As MouseEventArgs) If e.Button = MouseButtons.Left Then " If bigRect.Contains(New Point(e.X, e.Y)) Then MessageBox.Show("Clicked inside rectangle") Else MessageBox.Show("Clicked outside rectangle") End If End If End Sub
End Class</source>
Rectangle.Inflate
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class RectangleInflate
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) " Create a Graphics object Dim g As Graphics = Me.CreateGraphics() " Create PointF, SizeF and RectangleF Dim pt As New PointF(30.8F, 20.7F) Dim sz As New SizeF(60.0F, 40.0F) Dim rect2 As New RectangleF(40.2F, 40.6F, 100.5F, 100.0F) Dim rect1 As New RectangleF(pt, sz) Dim rect3 As Rectangle = Rectangle.Ceiling(rect1) Dim rect4 As Rectangle = Rectangle.Truncate(rect1) Dim rect5 As Rectangle = Rectangle.Round(rect2) " Draw rectangles g.DrawRectangle(Pens.Black, rect3) g.DrawRectangle(Pens.Red, rect5) Dim inflateSize As New Size(0, 40) " Inflate rectangle rect5.Inflate(inflateSize) " Draw new rectangle g.DrawRectangle(Pens.Blue, rect5) 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>
Rectangle.Intersect
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class RectangleUnionIntersect
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 = Me.CreateGraphics() Dim pt As New PointF(30.8F, 20.7F) Dim sz As New SizeF(60.0F, 40.0F) Dim rect1 As New RectangleF(pt, sz) Dim rect2 As New RectangleF(40.2F, 40.6F, 100.5F, 100.0F) Dim rect3 As Rectangle = Rectangle.Ceiling(rect1) Dim rect4 As Rectangle = Rectangle.Truncate(rect1) Dim rect5 As Rectangle = Rectangle.Round(rect2) g.DrawRectangle(Pens.Black, rect3) g.DrawRectangle(Pens.Red, rect5) Dim isectRect As Rectangle = Rectangle.Intersect(rect3, rect5) g.FillRectangle(New SolidBrush(Color.Blue), isectRect) Dim inflateSize As New Size(0, 40) isectRect.Inflate(inflateSize) g.DrawRectangle(Pens.Blue, isectRect) rect4 = Rectangle.Empty rect4.Location = New Point(50, 50) rect4.X = 30 rect4.Y = 40 Dim unionRect As Rectangle = Rectangle.Union(rect4, rect5) g.DrawRectangle(Pens.Green, unionRect) g.Dispose() 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>
Rectangle.Union
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class RectangleUnion
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) " Create a Graphics object Dim g As Graphics = Me.CreateGraphics() " Create PointF, SizeF and RectangleF Dim pt As New PointF(30.8F, 20.7F) Dim sz As New SizeF(60.0F, 40.0F) Dim rect2 As New RectangleF(40.2F, 40.6F, 100.5F, 100.0F) Dim rect1 As New RectangleF(pt, sz) Dim rect3 As Rectangle = Rectangle.Ceiling(rect1) Dim rect4 As Rectangle = Rectangle.Truncate(rect1) Dim rect5 As Rectangle = Rectangle.Round(rect2) " Draw rectangles g.DrawRectangle(Pens.Black, rect3) g.DrawRectangle(Pens.Red, rect5) Dim unionRect As Rectangle = Rectangle.Union(rect4, rect5) " Draw new rectangle g.DrawRectangle(Pens.Green, unionRect)
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>
Rectangle.Union and Intersect
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class RectangleUnionIntersect
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 = Me.CreateGraphics() Dim pt As New PointF(30.8F, 20.7F) Dim sz As New SizeF(60.0F, 40.0F) Dim rect1 As New RectangleF(pt, sz) Dim rect2 As New RectangleF(40.2F, 40.6F, 100.5F, 100.0F) Dim rect3 As Rectangle = Rectangle.Ceiling(rect1) Dim rect4 As Rectangle = Rectangle.Truncate(rect1) Dim rect5 As Rectangle = Rectangle.Round(rect2) g.DrawRectangle(Pens.Black, rect3) g.DrawRectangle(Pens.Red, rect5) Dim isectRect As Rectangle = Rectangle.Intersect(rect3, rect5) g.FillRectangle(New SolidBrush(Color.Blue), isectRect) Dim inflateSize As New Size(0, 40) isectRect.Inflate(inflateSize) g.DrawRectangle(Pens.Blue, isectRect) rect4 = Rectangle.Empty rect4.Location = New Point(50, 50) rect4.X = 30 rect4.Y = 40 Dim unionRect As Rectangle = Rectangle.Union(rect4, rect5) g.DrawRectangle(Pens.Green, unionRect) g.Dispose() 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 RectangleF
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class UsingRectangleF
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 = Me.CreateGraphics() Dim x As Single = 40.0F Dim y As Single = 40.0F Dim height As Single = 120.0F Dim width As Single = 120.0F Dim pt As New PointF(80.0F, 80.0F) Dim sz As New SizeF(100.0F, 100.0F) Dim rect1 As New RectangleF(pt, sz) Dim rect2 As New RectangleF(x, y, width, height) Dim rect3 As New RectangleF(10.0F, 10.0F, 180.0F, 180.0F) Dim redPen As New Pen(Color.Red, 2) Dim greenBrush As New SolidBrush(Color.Blue) Dim blueBrush As New SolidBrush(Color.Green) g.DrawRectangle(redPen, rect3.X, rect3.Y, rect3.Width, rect3.Height) g.FillRectangle(blueBrush, rect2) g.FillRectangle(greenBrush, rect1) redPen.Dispose() blueBrush.Dispose() greenBrush.Dispose() g.Dispose()
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>