我将Winforms Button控件属性设置为在网页上显示为超链接。除了FlatAppearance对象中的边框外,我已经将一切格式化了。我有代码作为伪CSS(FormBackColor是一个字符串常量。):
b.FlatStyle = FlatStyle.Flat
b.BackColor = ColorTranslator.FromHtml(FormBackColor)
b.ForeColor = Color.Blue
b.Font = New Font(b.Font.FontFamily, b.Font.Size, FontStyle.Underline)
b.Cursor = Cursors.Hand
Dim fa As FlatButtonAppearance = b.FlatAppearance
fa.BorderSize = 0
fa.MouseOverBackColor = b.BackColor
fa.MouseDownBackColor = b.BackColor
AddHandler b.MouseEnter, AddressOf ButtonMouseOver
AddHandler b.MouseLeave, AddressOf ButtonMouseOut
以下是鼠标输出/结束功能,作为对正在发生的事情的参考:
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按钮具有焦点,因此添加边框”边框?或者,我应该只检查ButtonMouseOver以检查控件是否具有焦点,是添加边框的条件,并且只是完成它?无论出于何种原因,我都希望从焦点中移除自动边框:)
答案 0 :(得分:4)
您可以覆盖按钮的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
答案 1 :(得分:2)
另一种实现此目的的方法是继承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
”是最好的使用事件,但它在我的测试中有效。
答案 2 :(得分:0)
从Button中导出并设置控件样式,以便无法选择控件:
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