Re: Font size based on the client rectangle of the control.



Thanks Bob! This is excatly what I want.

Regards
Kiran

Bob Powell [MVP] wrote:

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.



<kiranreddyd@xxxxxxxxx> wrote in message
news:1158644036.131479.100900@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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


Bob Powell [MVP] wrote:
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.



<kiranreddyd@xxxxxxxxx> wrote in message
news:1158583443.933730.235040@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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



.



Relevant Pages

  • Re: Drawing to second monitor using Graphics and GetDC
    ... Bob Powell [MVP] wrote: ... LayeredWindow API and either a form or a simple window based on ... Find great Windows Forms articles in Windows Forms Tips and Tricks ...
    (microsoft.public.dotnet.framework.drawing)
  • Re: Pasted clipboard text looks bad
    ... Bob Powell [MVP] ... Find great Windows Forms articles in Windows Forms Tips and Tricks ... Answer those GDI+ questions with the GDI+ FAQ ...
    (microsoft.public.dotnet.framework.drawing)
  • Re: Multiframe Bitmap resolutions?!?!?
    ... Find great Windows Forms articles in Windows Forms Tips and Tricks ... Answer those GDI+ questions with the GDI+ FAQ ... I use the Bob Powell GDI+ method ... That makes my tiff document with wrong sizes for the documents... ...
    (microsoft.public.dotnet.framework.drawing)
  • Re: DrawString failing to write accented characters, VB.Net
    ... Find great Windows Forms articles in Windows Forms Tips and Tricks ... Answer those GDI+ questions with the GDI+ FAQ ... Bob Powell [MVP] wrote: ...
    (microsoft.public.dotnet.framework.drawing)
  • Re: Hi, PrivateFontCollection and IsStyleAvailable problems.
    ... you can download the font file from this link: ... Bob Powell [MVP] wrote: ... Find great Windows Forms articles in Windows Forms Tips and Tricks ...
    (microsoft.public.dotnet.framework.drawing)