由於用戶ChristopherKyleHorton,誰發現這個問題的possible duplicate(雖然標有一般的.Net關鍵字,引發大多數是C#的答案),我由用戶yarecki沿下班。
我改進了一下,並對它進行了廣泛的評論。如果您還需要一個真正平坦的按鈕(沒有邊框,包括所有像素在內的文字使用情況),請將以下代碼放入新的類庫項目中,並在工具箱中包含對其的引用。
'In order to see the Text property in the appearance section of the properties
'window, the Category attribute needs to be modified, which requires the
'inclusion of the ComponentModel namespace.
Imports System.ComponentModel
Public Class FlatButton
Inherits Button
'============================================================================
'VARIABLES.
'============================================================================
'Property variables.
Private gsText As String
'============================================================================
'PROPERTIES.
'============================================================================
'Modifying the category attribute requires the import of the ComponentModel
'namespace.
<Category("Appearance")>
Public Overrides Property Text() As String
Get
Return gsText
End Get
Set(ByVal sValue As String)
If sValue <> gsText Then 'If the new text differs from the old
gsText = sValue 'one indeed, store it.
Me.Invalidate() 'Invalidate causes a paint message to be
End If 'sent to the control.
End Set
End Property
'============================================================================
'EVENTS.
'============================================================================
'When the flat button control is being *displayed* for the first time, the
'HandleCreated event is fired. In it, the button's typical characteristics
'are set.
Private Sub FlatButton_HandleCreated(sender As Object, e As EventArgs) _
Handles Me.HandleCreated
FlatStyle = FlatStyle.Flat 'Make the button flat.
FlatAppearance.BorderSize = 0 'Display no borders at all.
End Sub
'Fires when the Text property is modified, enforced by the Invalidate
'method coded there.
Protected Overrides Sub OnPaint(e As PaintEventArgs)
Dim sSaveText As String 'Saves the current text.
sSaveText = Me.Text 'Save the current text.
gsText = String.Empty 'Blank the Text property.
MyBase.OnPaint(e) 'Erase the client area.
gsText = sSaveText 'Restore the Text property.
'(Using has the effect to automatically dispose the used objects when
'done.)
'Acquire a new SolidBrush resource with the ForeColor color.
Using oBrush = New SolidBrush(ForeColor)
'Acquire a new StringFormat resource centering the text.
Using oStringFormat = New StringFormat() With
{.Alignment = StringAlignment.Center,
.LineAlignment = StringAlignment.Center}
'Draw the string contained in Text with the acquired brush in the
'acquired string format, in the button's client rectangle, which
'is enlarged by 3 pixels in each direction. The original client
'rectangle has a padding of 3 pixels to allow for a 2 pixels wide
'border (3D), and a 1-pixel margin between the border and the
'actual text.
e.Graphics.DrawString(Me.Text, Me.Font, oBrush,
Rectangle.Inflate(ClientRectangle, 3, 3), oStringFormat)
End Using 'Dispose the StringFormat object.
End Using 'Dispose the oBrush object
End Sub
End Class
採用這種控制確實減少文本週圍填充,前(左)和(右)後的比較:
希望這可以幫助別人也是如此。
可能重複[減少WinForms按鈕中的文本週圍的填充](https://stackoverflow.com/questions/6107108/reduce-padding-around-text-in-winforms-button) –
@ChristopherKyleHorton謝謝,我明白了。那麼,這不是VB.Net,所以我在那裏掙扎了一下。這也似乎yarecki的第三個答案得出結論,其他兩個答案都有缺點,但他提出了自己的答案。只有這個答案對我來說更難以掌握。不過,我會看到,如果我能理解它的話。 – Herb
作爲一名VB.NET開發人員,我可以將您的評論與C#中的答案聯繫起來。不幸的是,後者似乎在網上獲得了比前者更多的討論,但它們都是.NET語言,並且有許多概念。過了一段時間,我不得不自己教自己如何將我在其他地方找到的C#示例翻譯成我需要的VB.NET等價物。我希望你不要陷入太多困難。 –