Visual Basic WinForm 中的圆形 Hitbox
Circular Hitbox in Visual Basic WinForm
如何更改图片为圆形的 PictureBox 的点击框?通过点击框,我的意思是 ClickEvent 注册的地方。
您可以使用 Control.Region 属性,使用圆形 GraphicsPath 创建区域。此区域用于 Click 事件,以及 MouseEnter、MouseHover 和 MouseLeave 事件。
带有 PictureBox 的示例:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim path As New Drawing2D.GraphicsPath()
path.AddArc(0, 0, PictureBox1.Width, PictureBox1.Height, 0, 360) 'A circle at 0,0 relative to the PictureBox, sharing its Width and Height
PictureBox1.Region = New Region(path)
End Sub
如果你想在中间画一个圆圈(而不是椭圆形),那么使用类似的东西:
Dim path As New Drawing2D.GraphicsPath()
Dim radius As Integer = Math.Min(PictureBox1.Width - 1, PictureBox1.Height - 1) / 2
Dim rc As New Rectangle(New Point(PictureBox1.Width / 2, PictureBox1.Height / 2), New Size(1, 1))
rc.Inflate(radius, radius)
path.AddEllipse(rc)
PictureBox1.Region = New Region(path)
如何更改图片为圆形的 PictureBox 的点击框?通过点击框,我的意思是 ClickEvent 注册的地方。
您可以使用 Control.Region 属性,使用圆形 GraphicsPath 创建区域。此区域用于 Click 事件,以及 MouseEnter、MouseHover 和 MouseLeave 事件。
带有 PictureBox 的示例:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim path As New Drawing2D.GraphicsPath()
path.AddArc(0, 0, PictureBox1.Width, PictureBox1.Height, 0, 360) 'A circle at 0,0 relative to the PictureBox, sharing its Width and Height
PictureBox1.Region = New Region(path)
End Sub
如果你想在中间画一个圆圈(而不是椭圆形),那么使用类似的东西:
Dim path As New Drawing2D.GraphicsPath()
Dim radius As Integer = Math.Min(PictureBox1.Width - 1, PictureBox1.Height - 1) / 2
Dim rc As New Rectangle(New Point(PictureBox1.Width / 2, PictureBox1.Height / 2), New Size(1, 1))
rc.Inflate(radius, radius)
path.AddEllipse(rc)
PictureBox1.Region = New Region(path)