How to get the appropriate font size for DrawString method?

G

Guest

I need to put a bunch of characters in a grid like control, and am using
DrawString method to put the character. The problem is:

1. The size of drawing rect is fixed, 32x32
2. For some fonts, 14 points if perfect for this size of rect. However, for
others, it is too small.

Is there a way to know in advance what is the widest and highest character
in terms of pixels so that I can choose the font size appropriately?

The function I am used to draw the text:
g.DrawString(m_chars[index++].ToString(), this.Font, b, rect, format);
 
J

John Liu

I thought about using g.MeasureString(m_chars, this.Font) to find out
whether the font is too small or too big compared to rect.
Then you can incrementally adjust and test with larger font sizes.

jliu - johnliu.net
 
G

Guest

Thanks for John'e reply. Actually I need to find if the widest character is
inside the box. I found GetTextMetrics() API is very helpful.
 

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