我试图在我的Button区域周围绘制边框,但我无法弄清楚如何将Region属性转换为可用的GDI +数据。
目前,我将区域设置为等于CreateRoundRectRgn API的结果:
Option Strict On
Option Explicit On
Imports System.Runtime.InteropServices
Public Class RoundButton
Inherits Button
Private _radius As Integer = 10
Public Property Radius As Integer
Get
Return _radius
End Get
Set(ByVal value As Integer)
If value < 0 OrElse value > 90 Then
Throw New ArgumentOutOfRangeException("The radius cannot be less than 0 or greater than 90.")
End If
If _radius <> value Then
_radius = value
Me.OnRadiusChanged()
End If
End Set
End Property
Protected Overridable Sub OnRadiusChanged()
RaiseEvent RadiusChanged(Me, EventArgs.Empty)
End Sub
Public Event RadiusChanged(ByVal sender As Object, ByVal e As EventArgs)
Protected Overrides Sub OnPaint(e As PaintEventArgs)
MyBase.OnPaint(e)
Dim border_size As Integer = If(MyBase.FlatStyle = FlatStyle.Flat, MyBase.FlatAppearance.BorderSize, 1)
'Set the region
MyBase.Region = Region.FromHrgn(CreateRoundRectRgn(0, 0, MyBase.Width - border_size, MyBase.Height - border_size, _radius, _radius))
'Fill the color
Using background As Brush = New SolidBrush(MyBase.BackColor)
e.Graphics.FillRegion(background, MyBase.Region)
End Using
End Sub
<DllImport("Gdi32.dll", EntryPoint:="CreateRoundRectRgn")>
Private Shared Function CreateRoundRectRgn(ByVal nLeftRect As Integer, ByVal nTopRect As Integer, ByVal nRightRect As Integer, ByVal nBottomRect As Integer, ByVal nWidthEllipse As Integer, ByVal nHeightEllipse As Integer) As IntPtr
End Function
End Class
这很好 IF 我不想添加边框,但由于我想添加边框,我需要想一下如何转换控件中的数据&#39; s Region到可用的GDI +数据。我试图做的是从区域的GetRegionScans属性获取矩形,如下所示:
'Draw the border
Using border As Pen = New Pen(border_color, border_size)
e.Graphics.DrawRectangles(border, MyBase.Region.GetRegionScans(New Drawing2D.Matrix()))
End Using
但是这会在我的Button中绘制一堆矩形,而不是勾勒出边框。