Dynamically Resize Text

  • Thread starter Noah Coad [C# MCP]
  • Start date
N

Noah Coad [C# MCP]

I'm creating a program the must show the time (a clock) in a user control
and the text must fill the user control. How do I make text dynamically
resize to fill a label/panel/usercontrol? Thanks!!

-Noah Coad
Microsoft MVP & MCP (.NET/C#)
 
N

Noah Coad [.NET/C# MVP]

Thanks for the reply. I ment the actual size of the text within the
control. I suppose from your statement there isn't a "fill" for a font
size, that I'll have to play around with the sizes. That's the (sort of)
obvious way, I was looking for something quicker. Do you at least know if
there are classes (and if so, which ones) to determain the actual physical
size of some text?

Thanks,
-Noah Coad
MS MVP & MCP (.NET/C#)
 
R

Robert Surtees

Noah,

Give this a shot. Just set the size of the label to the size of the form in
the forms resize event. Used it for a similar problem. I think it's based
on some samples from Petzold's Programming MS Windows with C#

Enjoy,

-Robert


using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2d;

namespace exp
{
public class ExpandoLabel : Label
{
public ExpandoLabel()
{
}
protected override void OnPaint( PaintEventArgs e )
{
GraphicsPath path;
RectangleF rectfBounds, rectfTemp;

if( this.Text == "" )
{
Console.WriteLine( "null string in paint" );
return;
};

path = new GraphicsPath();

path.AddString( "Ty", this.Font.FontFamily, (int)
this.Font.Style, 100,
new Point( 0, 0 ), new
StringFormat() );

rectfTemp = path.GetBounds();

path = new GraphicsPath();
path.AddString( this.Text, this.Font.FontFamily, (int)
this.Font.Style, 100,
new Point( 0, 0 ), new
StringFormat() );

rectfBounds = path.GetBounds();
rectfBounds.Height = rectfTemp.Height;

PointF[] aptfDest = { new PointF( 0, 0 ),
new PointF( this.Width, 0 ),
new PointF( 0,
this.Height ) };

e.Graphics.Transform = new Matrix( rectfBounds, aptfDest );
e.Graphics.SetClip( path );
e.Graphics.FillPath( new SolidBrush( this.ForeColor ), path );
}
}
}
 

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