1. 程式人生 > >如何消除點擊按鈕時周圍出現的白線?

如何消除點擊按鈕時周圍出現的白線?

ros vertica dev screens pen filedia align important pub

如何消除點擊按鈕時周圍出現的白線?

StackOverFlow

使用語言:VB.NET
原網站:


問題描述

在我用鼠標點擊,然後彈出一個文件選擇對話框前,按鈕沒有異常,但之後它的周圍出現了一圈白線。
技術分享圖片
只有一句代碼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

或者,也可以自己重繪控件的邊框:
(要註意的是ClientRectangle

WidthHeight必須被縮小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

如何消除點擊按鈕時周圍出現的白線?