如何去除按钮的角?
How can I remove corners from a Button?
我正在尝试删除未包含在按钮边框中的角。
我用这个方法绘制我的按钮边框:
Private Sub BTN_Connexion_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles BTN_Connexion.Paint
Me.DrawRectangle(e.Graphics, New Pen(Color.White), 0, 0, BTN_Connexion.Width - 1, BTN_Connexion.Height - 1, 10)
End Sub
Public Sub DrawRectangle(ByVal g As Graphics, ByVal pen As Pen, ByVal x As Int32, ByVal y As Int32, ByVal width As Int32, ByVal height As Int32, ByVal radius As Int32)
'Create a rectangle
Dim area As RectangleF = New RectangleF(x, y, width, height)
Dim path As Drawing2D.GraphicsPath = New Drawing2D.GraphicsPath
'Add the corners
path.AddArc(area.Left, area.Top, radius * 2, radius * 2, 180, 90) 'Upper-Left
path.AddArc(area.Right - (radius * 2), area.Top, radius * 2, radius * 2, 270, 90) 'Upper-Right
path.AddArc(area.Right - (radius * 2), area.Bottom - (radius * 2), radius * 2, radius * 2, 0, 90) 'Lower-Right
path.AddArc(area.Left, area.Bottom - (radius * 2), radius * 2, radius * 2, 90, 90) 'Lower-Left
path.CloseAllFigures()
'Draw the rounded rectangle
g.DrawPath(pen, path)
End Sub
边角可以采用主体颜色。这样它们就不会出现在按钮上。但我不知道这是否是个好习惯,而且我不知道该怎么做。
那么你能告诉我如何去除这些角吗?
我已经解决了我的问题,这里是解决方案:
在要实现圆角按钮的表单中执行以下操作:
Private Sub BTN_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles BTN_Connect.Paint, BTN_Disconnect.Paint
DirectCast(sender, Button).Region = New Region(SpecificDesign.DrawRoundRectangle(0, 0, BTN_Connect.Width - 1, BTN_Connect.Height - 1, 10))
End Sub
classSpecificDesign
包含:
Public Class SpecificDesign
''' <summary>
''' Create the path of a rounded rectangle.
''' </summary>
''' <param name="left">Ordinate of the left top corner</param>
''' <param name="Top">Abscissa of the left top corner</param>
''' <param name="width">Width of the rounded rectangle (Left and right lines)</param>
''' <param name="height">Height of the rounded rectangle(Top and bottom lines)</param>
''' <param name="radius">Radius of the rounded corner </param>
''' <returns></returns>
Public Shared Function DrawRoundRectangle(ByVal left As Int32, ByVal Top As Int32, ByVal width As Int32, ByVal height As Int32, ByVal radius As Int32) As Drawing2D.GraphicsPath
Dim path As Drawing2D.GraphicsPath = New Drawing2D.GraphicsPath
path.AddArc(left, Top, radius * 2, radius * 2, 180, 90) 'Upper-Left
path.AddArc(left + width - (radius * 2), Top, radius * 2, radius * 2, 270, 90) 'Upper-Right
path.AddArc(left + width - (radius * 2), Top + height - (radius * 2), radius * 2, radius * 2, 0, 90) 'Lower-Right
path.AddArc(left, Top + height - (radius * 2), radius * 2, radius * 2, 90, 90) 'Lower-Left
path.CloseAllFigures()
Return path
End Function
End Class
我正在尝试删除未包含在按钮边框中的角。
我用这个方法绘制我的按钮边框:
Private Sub BTN_Connexion_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles BTN_Connexion.Paint
Me.DrawRectangle(e.Graphics, New Pen(Color.White), 0, 0, BTN_Connexion.Width - 1, BTN_Connexion.Height - 1, 10)
End Sub
Public Sub DrawRectangle(ByVal g As Graphics, ByVal pen As Pen, ByVal x As Int32, ByVal y As Int32, ByVal width As Int32, ByVal height As Int32, ByVal radius As Int32)
'Create a rectangle
Dim area As RectangleF = New RectangleF(x, y, width, height)
Dim path As Drawing2D.GraphicsPath = New Drawing2D.GraphicsPath
'Add the corners
path.AddArc(area.Left, area.Top, radius * 2, radius * 2, 180, 90) 'Upper-Left
path.AddArc(area.Right - (radius * 2), area.Top, radius * 2, radius * 2, 270, 90) 'Upper-Right
path.AddArc(area.Right - (radius * 2), area.Bottom - (radius * 2), radius * 2, radius * 2, 0, 90) 'Lower-Right
path.AddArc(area.Left, area.Bottom - (radius * 2), radius * 2, radius * 2, 90, 90) 'Lower-Left
path.CloseAllFigures()
'Draw the rounded rectangle
g.DrawPath(pen, path)
End Sub
边角可以采用主体颜色。这样它们就不会出现在按钮上。但我不知道这是否是个好习惯,而且我不知道该怎么做。
那么你能告诉我如何去除这些角吗?
我已经解决了我的问题,这里是解决方案:
在要实现圆角按钮的表单中执行以下操作:
Private Sub BTN_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles BTN_Connect.Paint, BTN_Disconnect.Paint
DirectCast(sender, Button).Region = New Region(SpecificDesign.DrawRoundRectangle(0, 0, BTN_Connect.Width - 1, BTN_Connect.Height - 1, 10))
End Sub
classSpecificDesign
包含:
Public Class SpecificDesign
''' <summary>
''' Create the path of a rounded rectangle.
''' </summary>
''' <param name="left">Ordinate of the left top corner</param>
''' <param name="Top">Abscissa of the left top corner</param>
''' <param name="width">Width of the rounded rectangle (Left and right lines)</param>
''' <param name="height">Height of the rounded rectangle(Top and bottom lines)</param>
''' <param name="radius">Radius of the rounded corner </param>
''' <returns></returns>
Public Shared Function DrawRoundRectangle(ByVal left As Int32, ByVal Top As Int32, ByVal width As Int32, ByVal height As Int32, ByVal radius As Int32) As Drawing2D.GraphicsPath
Dim path As Drawing2D.GraphicsPath = New Drawing2D.GraphicsPath
path.AddArc(left, Top, radius * 2, radius * 2, 180, 90) 'Upper-Left
path.AddArc(left + width - (radius * 2), Top, radius * 2, radius * 2, 270, 90) 'Upper-Right
path.AddArc(left + width - (radius * 2), Top + height - (radius * 2), radius * 2, radius * 2, 0, 90) 'Lower-Right
path.AddArc(left, Top + height - (radius * 2), radius * 2, radius * 2, 90, 90) 'Lower-Left
path.CloseAllFigures()
Return path
End Function
End Class