Font size based on the client rectangle of the control.

K

kiranreddyd

Hi,

I have a control that render text. This is done in OnPaint()
override by drawing the text. When the control is resized, the text
inside it should also be resized according to the size of the control.
Either height of the text or width of the text or both need to be
resized. So I need to determine the font size based on the current
client rectangle. How do I do this? Is there any other way of resizing
the text?

I am using .Net 2.0. Your help is highly appreciated.

Thanks in Advance.

Regards
Kiran
 
B

Bob Powell [MVP]

There is no easy answer for this. The font size needs to be tried and
readjusted, possibly using a binary chop algorithm, to obtain the correct
size.

The trouble is that changing the font size also changes the number of
characters on a line which changes the number of lines in a given rectangle
which.... you get the picture.

--
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.
 
K

kiranreddyd

Hi,

I am looking at functionality something like WordArt in MS Word. If
you increase the height the text height is increased without increasing
Width. The same happens for width too or both can be increased. How can
this be achieved? Thanks.

Regards
Kiran
 
B

Bob Powell [MVP]

The only way to change the font width independently of the height is to draw
the text using a matrix that distorts the glyphs.

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

this.SetStyle(ControlStyles.ResizeRedraw, true);

}

Font fn = new Font("Arial", 72);

String s = "Hello World";

protected override void OnPaint(PaintEventArgs e)

{

base.OnPaint(e);

SizeF sf = e.Graphics.MeasureString(s, fn);

Matrix mx = new Matrix(1.0f / sf.Width * this.ClientRectangle.Width, 0, 0,
1.0f / sf.Height * this.ClientRectangle.Height, 0, 0);

e.Graphics.Transform = mx;

e.Graphics.DrawString(s, fn, Brushes.Black, new PointF(0, 0));

}

}


--
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.
 
K

kiranreddyd

Hi,

I have two requirements regarding the previous scenario:
1) If I want to support TextAlignment of type ContentAlignmnet, how
do I do that?
2) If control key is pressed while resizing, the text should not get
resized but alignment should be applied. How do I achieve this.

Thanks in advance.

Regards
Kiran
 

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