当前位置 : 主页 > 编程语言 > c语言 >

.net – 从按钮控制中移除焦点上的边框

来源:互联网 收集:自由互联 发布时间:2021-06-24
我将我的 Winforms Button控件属性设置为在网页上显示为超链接.除了FlatAppearance对象中的边框外,我已经将一切格式化了.我有代码作为伪CSS(FormBackColor是一个字符串常量.): b.FlatStyle = FlatS
我将我的 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以检查控件是否具有焦点,是添加边框的条件,并且只是完成它?无论出于何种原因,我宁愿从焦点中移除自动边框:)

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