vb.net 如何更改按钮的边框颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44815270/
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
How to change Button's border colors?
提问by SystemD
I'm new to Visual Basic and I want to change the border color of a Button, but I don't see any option to do that in the IDE (Visual Studio 2017). Is there a way to do this?
我是 Visual Basic 的新手,我想更改 a 的边框颜色Button,但我在 IDE (Visual Studio 2017) 中没有看到任何可以执行此操作的选项。有没有办法做到这一点?
回答by Blackwood
The way to do this is not very obvious as the default Buttondoesn't allow for a coloured border.
这样做的方法不是很明显,因为默认设置Button不允许使用彩色边框。
First you have to set the Button's FlatStyleproperty to FlatStyle.Flat. Then you have to set the Button's FlatAppearance.BorderColorproperty to the colour of your choice.
首先,您必须将Button的FlatStyle属性设置为FlatStyle.Flat。然后您必须将Button的FlatAppearance.BorderColor属性设置为您选择的颜色。
You can do both of those things in the Visual Studio form designer if you want, or you can do it in code like this:
如果需要,您可以在 Visual Studio 表单设计器中执行这两项操作,或者您可以在如下代码中执行此操作:
Button1.Flatstyle = FlatStyle.Flat
Button1.FlatAppearance.BorderColor = Color.Yellow
回答by ???ěxě?
You can do this is a few different ways. One option (quick and easy) is to subclass the System.Windows.Forms.Buttonclass and then override the OnPaintmethod...
您可以通过几种不同的方式做到这一点。一种选择(快速简便)是对类进行子System.Windows.Forms.Button类化,然后覆盖该OnPaint方法......
For example:
例如:
Protected Overrides Sub OnPaint(ByVal pevent As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(pevent)
Dim rect As New Rectangle(0, 0, Me.Width, Me.Height)
Dim mPen As New Pen(Color.Red, 3)
pevent.Graphics.DrawRectangle(mPen, rect)
End Sub
Another option is to create your own button control, this takes time and you could benefit better as you would have more control in what you would like to do. If your button's FlatStyleproperty is set to "Flat" you could change the FlatApperanceproperty in designer such as border-size and etc...
另一种选择是创建您自己的按钮控件,这需要时间,您可以更好地受益,因为您可以更好地控制自己想做的事情。如果您的按钮的FlatStyle属性设置为“平面”,您可以更改FlatApperance设计器中的属性,例如边框大小等...
回答by Ang Xiong Lin
Button1.BorderColor = Drawing.Color.Red
回答by Jim Lim
You can subclass System.Windows.Forms.Button class then create your own by overriding OnPaint protected method like that:
您可以继承 System.Windows.Forms.Button 类,然后通过像这样覆盖 OnPaint 保护方法来创建自己的类:
Protected Overrides Sub OnPaint _
(ByVal pevent As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(pevent)
Dim rect As New Rectangle(0, 0, Me.Width, Me.Height)
Dim mypen As New Pen(Color.Green, 5)
pevent.Graphics.DrawRectangle(mypen, rect)
End Sub
However , if your button's FlatStyle property is set to "Flat", you can change FlatApperance property in designer such as border-size, border-color etc.
但是,如果您的按钮的 FlatStyle 属性设置为“Flat”,您可以在设计器中更改 FlatApperance 属性,例如边框大小、边框颜色等。

