首页 > 解决方案 > 如何删除单击时出现的按钮周围的白线

问题描述

它工作正常,直到我单击它并弹出一个文件对话框,然后它周围出现白线。
我不知道如何删除这些丑陋的线条。

截屏

唯一的代码是openFileDialog1.ShowDialog().

它是一个按钮,它FlatStyle是一个图像flat,它BackgroundImage是一个PNG图像。
之后出现白线,如果我单击表单,它将消失。

标签: vb.netwinforms

解决方案


一个简单的解决方法是将 ButtonFlatAppearance.BorderColor设置为其Parent.BackColor. 它将覆盖焦点矩形。该MouseUp事件可用于设置值,它将在新窗口打开之前引发(Control.Leave永远不会引发该事件):

Private Sub SomeButton_MouseUp(sender As Object, e As MouseEventArgs) Handles SomeButton.MouseUp
    Dim ctl As Button = DirectCast(sender, Button)
    ctl.FlatAppearance.BorderColor = ctl.Parent.BackColor
End Sub

使用该Control.Paint事件,我们还可以使用该Control.BackColor属性来绘制边框,均使用ControlPaintDrawBorder方法(比使用ButtonRenderer类更简单):

Private Sub SomeButton_Paint(sender As Object, e As PaintEventArgs) Handles SomeButton.Paint
    Dim ctl As Button = DirectCast(sender, Button)
    ControlPaint.DrawBorder(e.Graphics, ctl.ClientRectangle, ctl.BackColor, ButtonBorderStyle.Solid)
End Sub

并自己绘制控件的边框:(
请注意,在尺寸和尺寸ClientRectangle上,尺寸必须缩小 1 个像素。这是设计使然)。WidthHeight

Private Sub SomeButton_Paint(sender As Object, e As PaintEventArgs) Handles SomeButton.Paint
    Dim ctl As Control = DirectCast(sender, Control)
    Dim r As Rectangle = ctl.ClientRectangle
    Using pen As Pen = New Pen(ctl.BackColor, 1)
        e.Graphics.DrawRectangle(pen, r.X, r.Y, r.Width - 1, r.Height - 1)
    End Using
End Sub

推荐阅读