.net 从按钮控件中删除焦点上的边框
我正在将Winforms按钮控件属性设置为在网页上显示为超链接。除了FlatAppearance对象中的边框外,我已将所有内容格式化。我有代码充当伪CSS(FormBackColor是一个字符串常量): 以下是鼠标移出/越过功能,作为正在进行的操作的参考:.net 从按钮控件中删除焦点上的边框,.net,vb.net,winforms,.net,Vb.net,Winforms,我正在将Winforms按钮控件属性设置为在网页上显示为超链接。除了FlatAppearance对象中的边框外,我已将所有内容格式化。我有代码充当伪CSS(FormBackColor是一个字符串常量): 以下是鼠标移出/越过功能,作为正在进行的操作的参考: Public Shared Sub ButtonMouseOver(ByVal sender As Object, ByVal e As EventArgs) Dim b As Button = DirectCast(sender,
Public Shared Sub ButtonMouseOver(ByVal sender As Object, ByVal e As EventArgs)
Dim b As Button = DirectCast(sender, Button)
Dim fa As FlatButtonAppearance = b.FlatAppearance
fa.BorderSize = 1
End Sub
Public Shared Sub ButtonMouseOut(ByVal sender As Object, ByVal e As EventArgs)
Dim b As Button = DirectCast(sender, Button)
Dim fa As FlatButtonAppearance = b.FlatAppearance
fa.BorderSize = 0
End Sub
该代码从平面按钮控件中删除边框,但在MouseOver上除外,我添加了一个1像素的边框。在MouseLeave上,我删除了边界。这是为了显示一些视觉反馈。当按钮没有焦点时,此功能可以正常工作。但是,如果我单击按钮,使按钮具有焦点,则再次向外移动鼠标会在按钮周围显示一个大于1像素的边框。我想象它将我的按钮的显式1像素边框与传统的“Winform按钮有焦点,所以在按钮周围添加一个边框”边框相结合
如何禁用/删除“Winform按钮具有焦点,因此添加边框”边框?或者,我应该只做一个checkin按钮来检查控件是否有焦点,这是添加边框的一个条件,然后就可以使用它了吗?无论出于何种原因,我更愿意从焦点中删除自动边框:)您可以覆盖按钮的OnPaint事件,并使用窗体的背景色在绘制的边框上重新绘制:
AddHandler Button1.Paint, AddressOf ButtonPaint
Private Sub ButtonPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs)
Dim Btn = DirectCast(sender, Button)
Using P As New Pen(Me.BackColor)
e.Graphics.DrawRectangle(P, 1, 1, Btn.Width - 3, Btn.Height - 3)
End Using
End Sub
实现这一点的另一种方法是继承Windows.Forms.Button类并重写事件。这就避免了必须为主程序中的每个按钮处理这些事件
Public Class BorderlessFlatButton
Inherits Windows.Forms.Button
Protected Overrides Sub OnCreateControl()
MyBase.OnCreateControl()
Me.FlatAppearance.MouseOverBackColor = Me.BackColor
Me.FlatAppearance.MouseDownBackColor = Me.BackColor
Me.FlatAppearance.BorderSize = 0
End Sub
Protected Overrides Sub OnMouseEnter(ByVal e As System.EventArgs)
MyBase.OnMouseEnter(e)
Me.FlatAppearance.BorderSize = 1
End Sub
Protected Overrides Sub OnMouseLeave(ByVal e As System.EventArgs)
MyBase.OnMouseLeave(e)
Me.FlatAppearance.BorderSize = 0
End Sub
Protected Overrides Sub OnPaint(ByVal pevent As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(pevent)
Using P As New Pen(Me.BackColor)
pevent.Graphics.DrawRectangle(P, 1, 1, Me.Width - 3, Me.Height - 3)
End Using
End Sub
End Class
注意:我不能100%确定“
OnCreateControl
”是使用的最佳事件,但它在我的测试中起了作用。从按钮开始,设置控件样式,使控件不可选择:
Imports System.Windows.Forms
Imports System.Drawing
Public Class MyButton
Inherits Button
Public Sub New()
InitializeComponent()
Me.BackColor = Color.LightGray
Me.FlatStyle = Windows.Forms.FlatStyle.Flat
Me.FlatAppearance.BorderColor = SystemColors.ControlDarkDark
Me.FlatAppearance.MouseDownBackColor = Color.Cyan
Me.FlatAppearance.MouseOverBackColor = SystemColors.ControlDark
Me.TabStop = False
Me.SetStyle(ControlStyles.Selectable, False)
End Sub
End Class
我没有答案给你,但是你有没有理由尝试破解一个按钮来做链接标签的设计?
Imports System.Windows.Forms
Imports System.Drawing
Public Class MyButton
Inherits Button
Public Sub New()
InitializeComponent()
Me.BackColor = Color.LightGray
Me.FlatStyle = Windows.Forms.FlatStyle.Flat
Me.FlatAppearance.BorderColor = SystemColors.ControlDarkDark
Me.FlatAppearance.MouseDownBackColor = Color.Cyan
Me.FlatAppearance.MouseOverBackColor = SystemColors.ControlDark
Me.TabStop = False
Me.SetStyle(ControlStyles.Selectable, False)
End Sub
End Class