C#:删除按钮内边缘的间距

时间:2009-08-06 11:36:06

标签: c# string button

情况:我需要一个小按钮,上面有一些文字。

问题:按钮似乎认为在其边缘附近显示空白空间比显示我的文本更重要。

我不能为我的生活弄清楚如何去除边缘的空白东西。非常感谢任何帮助!

提前致谢。

-MonsterMaw

6 个答案:

答案 0 :(得分:5)

假设您正在谈论WinForms,您可以将按钮的FlatStyle属性设置为System

这样你就可以调整按钮的大小,使其足够小,使文本完全适合,不需要任何内部填充。

答案 1 :(得分:1)

您可以覆盖按钮上的OnPaint方法,然后根据需要绘制它。我想你可以使用base.OnPaint绘制没有任何文字的按钮然后用pevent.Graphics.DrawString在你自己上绘制文本?

答案 2 :(得分:1)

考虑这是否可取。用户期望从我们的应用程序中获得某种标准行为,因此如果您试图使规则变得不合标准,那么您至少应该考虑它可能不是正确的解决方案。

其他选项包括将UI命令移动到菜单/工具栏和链接,如果按钮没有为您执行此操作。

答案 3 :(得分:1)

LinkLabel是一个不错的选择(假设它适合您的整体UI)。它只是 文本,因此您根本不必担心边框或填充。

答案 4 :(得分:0)

您可以将Button的Text属性设置为空。然后在按钮顶部放置一个标签,如果一切都失败了......

编辑。不要忘记将标签的背景颜色设置为透明。

答案 5 :(得分:0)

这是一个非常古老的话题,但也许有人会发现它很有用。

请参阅下面的命题。我创建自定义Button控件并覆盖Text属性,以便能够使用其私有部分_Text并在String.Empty事件之前附加MyBase.OnPaint(e)。这使得该按钮无需文本即可绘制。后来我将旧文本重新分配给私有属性并自己绘制字符串。我将Inflate添加到Rectangle以使文本更好,只需触摸按钮的边框,而不是重叠它。我的命题适用于任何flatstyle属性。

Here comparison of standard button and no padding button in two flatstyles: standard and flat

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

C#版本:

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);
            }
        }

    }

}