.net 在自定义控件上发布图形圆角
我试图创建一个自定义控件,它类似于一个带有圆角和边框的长矩形。它还将包含一些文本、图标等,但当以编程方式添加到表单中时,我遇到了手动绘制控件的问题 代码如下所示:.net 在自定义控件上发布图形圆角,.net,vb.net,user-controls,paint,rounded-corners,.net,Vb.net,User Controls,Paint,Rounded Corners,我试图创建一个自定义控件,它类似于一个带有圆角和边框的长矩形。它还将包含一些文本、图标等,但当以编程方式添加到表单中时,我遇到了手动绘制控件的问题 代码如下所示: Option Explicit On Option Strict On Imports System.Runtime.InteropServices Imports Transitions Imports System.Drawing.Drawing2D Public Class AlertPanel Private m
Option Explicit On
Option Strict On
Imports System.Runtime.InteropServices
Imports Transitions
Imports System.Drawing.Drawing2D
Public Class AlertPanel
Private m_Radius As Integer
Private m_BorderWidth As Integer
Private m_AlertType As AlertType
Private m_Icon As Image
''' <summary>
''' Indicates a Radius of the control's corners
''' </summary>
''' <returns>The corner Radius.</returns>
Public Property Radius As Integer
Get
Return m_Radius
End Get
Set(value As Integer)
m_Radius = value
End Set
End Property
''' <summary>
''' Indicates the width to draw the outer border of the control.
''' </summary>
''' <returns>The border width.</returns>
Public Property BorderWidth As Integer
Get
Return m_BorderWidth
End Get
Set(value As Integer)
m_BorderWidth = value
End Set
End Property
''' <summary>
''' Indicates the type of Alert for the control.
''' </summary>
''' <returns>The Alert type.</returns>
Public Property AlertType As AlertType
Get
Return m_AlertType
End Get
Set(value As AlertType)
m_AlertType = value
End Set
End Property
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub
Private Sub AlertPanel_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
Dim rect As Rectangle = Me.ClientRectangle 'Drawing Rounded Rectangle
rect.X = rect.X + 1
rect.Y = rect.Y + 1
rect.Width -= 2
rect.Height -= 2
Using bb As GraphicsPath = GetPath(rect, Radius)
'Draw the background
Using br As Brush = New SolidBrush(BackColor)
e.Graphics.SmoothingMode = SmoothingMode.HighQuality
e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic
e.Graphics.FillPath(br, bb)
End Using
'Draw the border
Using br As Brush = New SolidBrush(ForeColor)
rect.Inflate(-1, -1)
e.Graphics.SmoothingMode = SmoothingMode.HighQuality
e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic
e.Graphics.DrawPath(New Pen(br, BorderWidth), bb)
End Using
End Using
End Sub
Protected Function GetPath(ByVal rc As Rectangle, ByVal r As Int32) As GraphicsPath
Dim x As Int32 = rc.X, y As Int32 = rc.Y, w As Int32 = rc.Width, h As Int32 = rc.Height
r = r << 1
Dim path As GraphicsPath = New GraphicsPath()
If r > 0 Then
If (r > h) Then r = h
If (r > w) Then r = w
path.AddArc(x, y, r, r, 180, 90)
path.AddArc(x + w - r, y, r, r, 270, 90)
path.AddArc(x + w - r, y + h - r, r, r, 0, 90)
path.AddArc(x, y + h - r, r, r, 90, 90)
path.CloseFigure()
Else
path.AddRectangle(rc)
End If
Return path
End Function
End Class
留给我的是:
更新
在根据以下钚对我的代码进行一些编辑后,我现在得到以下结果:
最终更新
最终结果如下所示。非常感谢Proputoix提供的所有帮助
我正在尝试创建一个自定义控件
您没有说这是一个控件的子类化(如面板)还是作为用户控件构建的。我根据InitializeComponent()
猜测后者
主要的事情似乎是,UserControl
的标准边框仍在绘制中。添加此选项可将其关闭:
Public Sub New()
MyBase.BorderStyle = Windows.Forms.BorderStyle.None
...
您可能希望隐藏该属性,以便最终用户无法在“属性”中重新启用该属性
此外,对于较宽的边框,您会发现底部边框被剪裁了一个像素(在图像中有点明显-顶部水平面看起来比底部水平面厚)。将此添加到GetPath
方法:
h As Int32 = rc.Height - 1
结果:
另外,BackColor
被继承并应用于整个客户端区域,该区域保持为矩形。您可能也需要更换它。将实际的背景色保持为透明
,也可能对用户隐藏该属性,并使用新的FillColor
属性:
' in the paint event
Using br As Brush = New SolidBrush(FillColor)
...
使用FillColor
属性并将BackColor
强制为Transparent
:
公共子CtrlCornerOrder(ctrl作为控件,CurveSize作为整数)
将其作为类文件并按如下方式调用
CtrlCornerOrder(pnlList,15)
这里CtrlCornerOrder是我们的类文件名,Pnlist是我们的面板名,15是根据您在面板上制作曲线边的需要更改的值…我使用在绘制事件期间调用的函数在按钮控件上获得圆角
必须具有visual studio,才能创建新项目
选择一个新的windows窗体作为新项目
在表单中添加2个x按钮
双击表单上的任意位置以打开“代码”窗口
删除所有代码,包括Form1
子标题,并粘贴到下面的
调用函数ButtonOrderRadius(sender,50)
意味着您可以为各个按钮设置不同的borderRadius。
因为它使用对象,所以您可以将相同的函数应用于PictureBox和其他控件(但不是全部)
因此,您可以使用按钮医嘱半径(发送方,10)
还有像这样的50像素半径按钮orderradius(发送方,50)
只需将整数更改为函数中的第二个参数,
第一个参数必须是对象变量。IX,它是作为自定义控件构建的。我已经添加了您的修改,修复了剪辑,但没有如我的示例所示的黑色边框。如果我将控件放在窗体上,它似乎可以正常工作,但手动创建时就不行了。将控件设为新的AlertPanel
。。。在我的Form_Load事件上。无法重新编程。创建oneClose时,是否尝试在with块中添加.BorderStyle=Windows.Forms.BorderStyle.None
?它看起来是固定的-您有另一个问题的背景色是所有。对不起,普卢图尼克斯,请您详细说明。我只有一个区域用背景色填充RoundedRect路径。请你进一步确认这个问题好吗。非常感谢。
' in the paint event
Using br As Brush = New SolidBrush(FillColor)
...
Try
Dim p As New System.Drawing.Drawing2D.GraphicsPath
p.StartFigure()
p.AddArc(New Rectangle(0, 0, CurveSize, CurveSize), 180, 90)
'p.AddLine(CurveSize, 0, ctrl.Width - CurveSize, 0)
p.AddArc(New Rectangle(ctrl.Width - CurveSize, 0, CurveSize, CurveSize), -90, 90)
'p.AddLine(ctrl.Width, CurveSize, ctrl.Width, ctrl.Height - CurveSize)
p.AddArc(New Rectangle(ctrl.Width - CurveSize, ctrl.Height - CurveSize, CurveSize, CurveSize), 0, 90)
'p.AddLine(ctrl.Width - 40, ctrl.Height, 40, ctrl.Height)
p.AddArc(New Rectangle(0, ctrl.Height - CurveSize, CurveSize, CurveSize), 90, 90)
p.CloseFigure()
ctrl.Region = New Region(p)
p.Dispose()
Catch ex As Exception
MsgBox(Err.Number & vbCrLf & Err.Description, MsgBoxStyle.Information)
End Try
Imports System.Drawing.Drawing2D
Public Class Form1
Public Sub buttonBorderRadius(ByRef buttonObj As Object, ByVal borderRadiusINT As Integer)
Dim p As New Drawing2D.GraphicsPath()
p.StartFigure()
'TOP LEFT CORNER
p.AddArc(New Rectangle(0, 0, borderRadiusINT, borderRadiusINT), 180, 90)
p.AddLine(40, 0, buttonObj.Width - borderRadiusINT, 0)
'TOP RIGHT CORNER
p.AddArc(New Rectangle(buttonObj.Width - borderRadiusINT, 0, borderRadiusINT, borderRadiusINT), -90, 90)
p.AddLine(buttonObj.Width, 40, buttonObj.Width, buttonObj.Height - borderRadiusINT)
'BOTTOM RIGHT CORNER
p.AddArc(New Rectangle(buttonObj.Width - borderRadiusINT, buttonObj.Height - borderRadiusINT, borderRadiusINT, borderRadiusINT), 0, 90)
p.AddLine(buttonObj.Width - borderRadiusINT, buttonObj.Height, borderRadiusINT, buttonObj.Height)
'BOTTOM LEFT CORNER
p.AddArc(New Rectangle(0, buttonObj.Height - borderRadiusINT, borderRadiusINT, borderRadiusINT), 90, 90)
p.CloseFigure()
buttonObj.Region = New Region(p)
End Sub
Private Sub Button1_Paint(sender As Object, e As PaintEventArgs) Handles Button1.Paint
buttonBorderRadius(sender, 25)
End Sub
Private Sub Button2_Paint(sender As Object, e As PaintEventArgs) Handles Button2.Paint
buttonBorderRadius(sender, 50)
End Sub
End Class