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).
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:
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.
Edit: 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.
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.
@Bryan - both solutions proposed by @Henk Roux and @chiper are not perfect. First generate button without any visual attributes and second forces us to add new attribute like
OwnerDrawText
.See my proposition below. I override
Text
property to be able to use its private part_Text
and attachString.Empty
just beforeMyBase.OnPaint(e)
event. This makes that button is draw without text. Later on I reassign back old text to the private property and draw string myself. I addInflate
toRectangle
to make text nicer just touching border of button, not overlapping it. My proposition works with anyflatstyle
.Here is comparison of standard and no padding button in two flatstyles: standard and flat
C# version:
I have created a successful radio automation application back then in '98 using MFC. First thing we did is that we created whole new set of GUI controls for it, since for example, pressing the button on the screen with the finger obscures it, and standard buttons aren't so fancy for it.
My advice would be not to go with deriving your button from standard WinForms button, but from the
Control
and do the drawing yourself. If it is the simple button like one you presented, you won't have much to do, just DrawString, and if it is somewhat more complicated, you'll have complete control over it.You don't have to draw the whole button yourself. Just leave Text property empty and assign your text to OwnerDrawText