加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-17 07:17:55 所属栏目:百科 来源:网络整理
导读:我将我的 Winforms Button控件属性设置为在网页上显示为超链接.除了FlatAppearance对象中的边框外,我已经将一切格式化了.我有代码作为伪CSS(FormBackColor是一个字符串常量.): b.FlatStyle = FlatStyle.Flatb.BackColor = ColorTranslator.FromHtml(FormBac
我将我的 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,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,Btn.Width - 3,Btn.Height - 3)
    End Using
End Sub

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读