减少填充在周围的WinForms按钮文本 [英] Reduce Padding Around Text in WinForms Button

查看:159
本文介绍了减少填充在周围的WinForms按钮文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有将要在触摸屏系统中使用的应用程序,它包含了若干按钮,是相当大的(〜100像素正方形)。

I have an application that is going to be used on a touch screen system, and it contains a number of buttons that are fairly large (~100px square).

每个按键都会有1至4行文本(通常每行一个字)的。

Each button will have between 1 and 4 lines of text (typically one word per line).

由于大量填充的按钮,我不得不减少文本的大小,使之成为几乎无法读取,但是如果我能够减少内部填充,使文本能画出正确的了边境,那么我会不会有问题。

Due to the large amount of padding in the button, I'm having to reduce the size of the text so that it becomes almost unreadable, however if I was able to reduce the internal padding so that the text would paint right up to the border, then I wouldn't have a problem.

我已经试图减少控制的填充下降到零以下,但它并不能帮助。

I've attempted to reduce the padding of the control down to zero as follows, but it doesn't help.

this.Text = _label;
this.Font = new Font(this.Font.FontFamily, (float) _size);
this.Padding = new Padding(0);

的问题的一个例子如下所示:

An example of the problem is shown below:

正如你可以看到有大量的单词概述,以适应在同一行的空间,但我怎么能做到这一点不减少字体大小?我不津津乐道不必重写控件的文字画code的想法。

As you can see there is plenty of space for the word 'OVERVIEW' to fit on one line, but how can I achieve this without reducing the font size? I don't relish the thought of having to rewrite the control's text painting code.

编辑:我已经注意到,越来越多的填充,以各种值高达300,没有什么区别,以控制内部填充。同样的信息,我使用的按钮是我从Windows.Forms.Button类继承的控制,因为我需要添加一些属性,但是我还没有干预任何按钮的自己的方法。

I've noticed that increasing the padding to various values as high as 300, makes no difference to the internal padding of the control. Also for information, the button I'm using is a control I've inherited from the Windows.Forms.Button class, as I need to add a few properties, however I haven't interfered with any of the Button's own methods.

推荐答案

您也只需重写Button控件要从中继承的OnPaint中()方法,而忽略调用base.OnPaint(),并更换用自己的平局code。

You also simply override the OnPaint() method of the Button control from which you're inheriting, and omit to call base.OnPaint(), and replace it with your own draw code.

    protected override void OnPaint(PaintEventArgs pevent)
    {
        //omit base.OnPaint completely...

        //base.OnPaint(pevent); 

        using (Pen p = new Pen(BackColor))
        {
            pevent.Graphics.FillRectangle(p.Brush, ClientRectangle);
        }

        //add code here to draw borders...

        using (Pen p = new Pen(ForeColor))
        {
            pevent.Graphics.DrawString("Hello World!", Font, p.Brush, new PointF(0, 0));
        }
    }

这篇关于减少填充在周围的WinForms按钮文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆