How to draw text that will fill a panel(form, etc.)

B

BT

Hi,

I would like to draw centered text that takes up the entire paintable
area of my form.

I know how to use a StringFormat to center the text both horizontally
and vertically.

What I don't know how to do is choose the correct height (and width?)
for my font.

When the form is resized the font size (and width?) will have to be
updated.

Thanks,
BT
 
B

Bob Powell [MVP]

You can only chose font heights so getting an exact match is always
difficult. I've seen several schemes where the programmer increases the font
height gradually or using a binary chop until the approximate area is
filled. Such schemes are often time consuming.

If you don't mind distortion you can draw the text onto a bitmap and stretch
it to the form or create a transform that distorts the text to the area to
be filled.
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
B

BT

Thank you for the response Bob. Here is what I have written. I chose
to take the "try something until it fits route" because the
scaling/transforming options were too blocky when the client rectangle
was relatively small. Any suggestions as to how the code might be
improved would be greatly appreciated!

private Font CreateFittedFont(FontFamily family, FontStyle style,
Control control, string stringToFit)
{
Size clientSize = new Size(control.ClientRectangle.Width,
control.ClientRectangle.Height);

// Ensure we don't try to make a font with a height of 0
if (clientSize.Height <= 1)
{
return new Font(family, 1, style);
}

// Start out by trying a font size that *should* be too big
int curFontHeight = clientSize.Height;
Font fittedFont = new Font(family, curFontHeight, style);

using (Graphics graphics = control.CreateGraphics())
{
while (true)
{
Size curStringSize =
graphics.MeasureString(stringToFit, fittedFont).ToSize();

if ((curStringSize.Height < clientSize.Height) &&
(curStringSize.Width < clientSize.Width))
break;

if (null != fittedFont)
fittedFont.Dispose();

curFontHeight--;

if (curFontHeight <= 1)
{
fittedFont = new Font(family, 1, style);
break;
}

fittedFont = new Font(family, curFontHeight,
style);
}
}

return fittedFont;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top