VB.Net by API/System.Drawing/Rectangle — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 12:50, 26 мая 2010
Содержание
New Rectangle
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
New Rectangle(Int x, Int y, Int w, Int y)
Imports System
Imports System.Windows.Forms
Imports System.Drawing
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
Public Sub New()
MyBase.New()
Me.ClientSize = New System.Drawing.Size(292, 273)
End Sub
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
Dim G As Graphics = e.Graphics
G.Clear(Color.BurlyWood)
Dim r As Rectangle = New Rectangle(50, 50, 100, 100)
Dim b As Brush = Brushes.Crimson
G.FillRectangle(b, r)
End Sub
End Class
Rectangle.Ceiling
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
Rectangle.Inflate
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
Rectangle.Intersect
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
Rectangle.Round
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
Imports System.Math
public class MeasureCharacterRange
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 txt As String = "www.vbex.ru"
Dim the_font As New Font("Times New Roman", 50, FontStyle.Bold, GraphicsUnit.Pixel)
Dim layout_rect As New RectangleF(0, 0, Me.ClientSize.Width, Me.ClientSize.Height)
Dim string_format As New StringFormat
string_format.LineAlignment = StringAlignment.Center
string_format.Alignment = StringAlignment.Center
e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit
Dim character_ranges(txt.Length - 1) As CharacterRange
For i As Integer = 0 To txt.Length - 1
character_ranges(i) = New CharacterRange(i, 1)
Next i
string_format.SetMeasurableCharacterRanges(character_ranges)
Dim character_regions() As Region = e.Graphics.MeasureCharacterRanges(txt, the_font, layout_rect, string_format)
For Each rgn As Region In character_regions
Dim character_bounds As RectangleF = rgn.GetBounds(e.Graphics)
Dim character_rect As Rectangle = Rectangle.Round(character_bounds)
e.Graphics.DrawRectangle(Pens.White, character_rect)
Next rgn
e.Graphics.DrawString(txt, the_font, Brushes.Black, layout_rect, string_format)
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
Rectangle.Top
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
Rectangle.Truncate
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
Rectangle.Union
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
Rectangle.X
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
Rectangle.Y
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