vb.net 带轮廓的自定义圆形按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46494861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 20:20:17  来源:igfitidea点击:

Custom rounded button with outline

vb.netwinformscustom-controls

提问by Cristian Cundari

I am tring to create a custom button width rounded corner and a white outline which follows its shape. On the OnPaint event I've added the following code.

我正在尝试创建一个自定义按钮宽度的圆角和一个遵循其形状的白色轮廓。在 OnPaint 事件中,我添加了以下代码。

Public Class RoundedButton
    Protected Overrides Sub OnPaint(e As PaintEventArgs)
        MyBase.OnPaint(e)
        Dim grPath As GraphicsPath = New GraphicsPath(FillMode.Winding)
        grPath.AddArc(0, 0, ClientSize.Height, ClientSize.Height, 90, 180)
        grPath.AddLine(grPath.GetLastPoint, New Point(ClientSize.Width - grPath.GetLastPoint.X * 2, 0))
        grPath.AddArc(New RectangleF(grPath.GetLastPoint, New Size(ClientSize.Height, ClientSize.Height)), 270, 180)
        grPath.CloseFigure()

        Me.Region = New Region(grPath)

        Dim mypen As New Pen(Color.White, 2)
        mypen.Alignment = PenAlignment.Inset
        e.Graphics.DrawPath(mypen, grPath)
    End Sub
End Class

If I try to use it in a form, it works only if Backcolor property is set to Transparent. If not I can't see the pen path. I would like to change the backcolor without loose the rounded white border of the pen. I would like to obtain something like this: enter image description here

如果我尝试在表单中使用它,则仅当 Backcolor 属性设置为 Transparent 时才有效。如果没有,我就看不到笔路径。我想在不松开笔的圆形白色边框的情况下更改背景颜色。我想获得这样的东西: 在此处输入图片说明

回答by doamatto

With all projects that you want custom buttons, it is usually easiest to curve a PictureBox with a picture of your button, then using your coding language (VB.NET, C#, C++) to add an on click function to make the action.

对于所有需要自定义按钮的项目,通常最简单的方法是将带有按钮图片的 PictureBox 曲线化,然后使用您的编码语言(VB.NET、C#、C++)添加点击功能来执行操作。

Here is a mini example using VB.NET:

这是一个使用 VB.NET 的小例子:

Private Sub pictureBox1_Click(sender As Object, e As EventArgs) Handles pictureBox1.Click
    Process.start("http://stackoverflow.com")
End Sub

Hope I helped, Matt

希望我有所帮助,马特