如何消除点击按钮时周围出现的白线?

如何消除点击按钮时周围出现的白线?

使用语言:VB.NET
原网站:https://stackoverflow.com/questions/53862825/how-to-remove-the-white-lines-surrounding-a-button-appearing-when-i-click-it/53867195?noredirect=1#comment94614321_53867195


问题描述

在我用鼠标点击,然后弹出一个文件选择对话框前,按钮没有异常,但之后它的周围出现了一圈白线。
ScreenShot
只有一句代码openFileDialog1.ShowDialog()
按钮的FlatStyle属性为flatBackgroundImage是一张PNG格式的图像。
白线出现后,点击窗体它就会消除。


解答

一个简单的办法是把按钮的FlatAppearance.BorderColor属性设置成Parent.BackColor,即它的“容器”的背景色。这会重写焦点框。MouseUp事件可以被用来设置其值,它将在新窗口出现前被引发。

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属性来重绘边框,也可以用ControlPaint类中的DrawBorder方法(比使用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

或者,也可以自己重绘控件的边框:
(要注意的是ClientRectangleWidthHeight必须被缩小1像素)

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
posted @ 2019-01-08 18:55  shadowfish0  阅读(981)  评论(0编辑  收藏  举报