, , , - .
. Button Text, _Text String.Empty MyBase.OnPaint(e). . . Inflate Rectangle, , , . flatstyle.
:
Imports System.ComponentModel
Imports System.Drawing
Imports System.Windows.Forms
Public Class ButtonNoPadding
Inherits Button
Private _textCurrent As String
Private _Text As String
<Category("Appearance")>
Public Overrides Property Text() As String
Get
Return _Text
End Get
Set(ByVal value As String)
If value <> _Text Then
_Text = value
Invalidate()
End If
End Set
End Property
Protected Overrides Sub OnPaint(e As PaintEventArgs)
_textCurrent = Text
_Text = String.Empty
MyBase.OnPaint(e)
_Text = _textCurrent
Using brush = New SolidBrush(ForeColor)
Using stringFormat = New StringFormat() With {.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center}
e.Graphics.DrawString(Text, Font, brush, Rectangle.Inflate(ClientRectangle, -2, -2), stringFormat)
End Using
End Using
End Sub
End Class
#:
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
public class ButtonNoPadding : Button
{
private string _textCurrent;
private string _Text;
[Category("Appearance")]
public override string Text {
get { return _Text; }
set {
if (value != _Text) {
_Text = value;
Invalidate();
}
}
}
protected override void OnPaint(PaintEventArgs e)
{
_textCurrent = Text;
_Text = string.Empty;
base.OnPaint(e);
_Text = _textCurrent;
using (brush == new SolidBrush(ForeColor)) {
using (stringFormat == new StringFormat {Alignment = StringAlignment.Center,LineAlignment = StringAlignment.Center}) {
e.Graphics.DrawString(Text, Font, brush, Rectangle.Inflate(ClientRectangle, -2, -2), stringFormat);
}
}
}
}