C #: removing spacing around the edges inside a button

Situation: I need a tiny button with text on it.

Problem. It seems like the button seems like displaying an empty space near its edges is more important than displaying my text.

I can’t understand for life how to remove this empty material around the edges. Any help is much appreciated!

Thanks in advance.

-MonsterMaw

+3
source share
6 answers

Assuming you are talking about WinForms, you can set a FlatStylebutton for the property System.

This will allow you to resize the button so that it is small enough so that the text can be accurate without any internal additions.

+5
source

OnPaint , , . , base.OnPaint, - , pevent.Graphics.DrawString?

+1

, . - , , , , , .

/ , .

+1

LinkLabel - ( , ). , .

+1

Text . , ...

. .

0

, , , - .

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

    }

}
0

Source: https://habr.com/ru/post/1714495/


All Articles