【问题标题】:Remove Border On Focus From Button Control从按钮控件中删除焦点边框
【发布时间】:2011-02-19 15:54:14
【问题描述】:

我将我的 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

代码从平面 Button 控件中删除边框,但在 MouseOver 上,我添加了一个 1 像素的边框。在 MouseLeave 上,我删除了边框。这是为了显示一些视觉反馈。当按钮没有焦点时,这可以正常工作。但是,如果我单击按钮,给按钮焦点,鼠标移出并再次显示按钮周围大于 1 像素的边框。我在想象它将按钮的显式 1 像素边框与传统的“Winform 按钮具有焦点,因此在按钮周围添加边框”边框相结合。

如何禁用/删除“Winform 按钮具有焦点,所以添加边框”边框?或者,我应该只检查 ButtonMouseOver 以检查控件是否具有焦点,作为添加边框的条件,然后就完成了吗?无论出于何种原因,我都希望从焦点中删除自动边框:)

【问题讨论】:

  • 我无法为您解答,但您是否有理由尝试破解按钮来执行与构建 LinkLabel 相同的操作?

标签: .net vb.net winforms


【解决方案1】:

您可以覆盖按钮的 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

【讨论】:

    【解决方案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”是最适合使用的事件,但它在我的测试中有效。

    【讨论】:

      【解决方案3】:

      源自 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
      

      【讨论】:

        猜你喜欢
        • 2012-11-05
        • 2021-06-04
        • 2011-05-06
        • 1970-01-01
        • 2012-07-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-23
        相关资源
        最近更新 更多