📄 form1.vb
字号:
Imports System.Drawing.Drawing2D
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写处置以清理组件列表。
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
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意:以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Name = "Form1"
Me.Text = "区域"
End Sub
#End Region
Protected Overrides Sub OnPaint(ByVal e As Windows.Forms.PaintEventArgs)
Dim g As Graphics = e.Graphics
'定义一个含有多边形的路径
Dim points As PointF() = { _
New PointF(10, 10), _
New PointF(200, 20), _
New PointF(500, 200), _
New PointF(300, 250), _
New PointF(100, 150)}
Dim gp As New GraphicsPath()
gp.AddPolygon(points)
'利用该路径生成多边形区域
Dim reg As New Region(gp)
'在该区域中剔除一个矩形区域
reg.Exclude(New Rectangle(90, 60, 150, 60))
'绘出最后得到的区域
g.FillRegion(Brushes.Blue, reg)
'得到并绘制多边形区域的包围矩形
Dim rect As RectangleF = reg.GetBounds(g)
g.DrawRectangle(Pens.Red, rect.Left, rect.Top, rect.Width, rect.Height)
'确定下面两个点是否在区域中
Dim p1 As New PointF(300, 100)
Dim p2 As New PointF(100, 100)
InRegion(reg, p1)
InRegion(reg, p2)
gp.Dispose()
reg.Dispose()
End Sub
Private Sub InRegion(ByVal reg As Region, ByVal p As PointF)
'确定某点是否位于指定区域中,并在调试窗口中输出
If reg.IsVisible(p) Then
Console.WriteLine("点(" & Str(p.X) & "," & Str(p.Y) & ")在指定区域内")
Else
Console.WriteLine("点(" & Str(p.X) & "," & Str(p.Y) & ")不在指定区域内")
End If
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -