Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Font Size
Message
General information
Forum:
ASP.NET
Category:
Other
Title:
Miscellaneous
Thread ID:
00755775
Message ID:
00755884
Views:
8
This message has been marked as the solution to the initial question of the thread.
Hi Wilson,

You can use the MeasureString method in the Graphics class. Here is some sample code that would center text on a form by using graphics. It needs to calculate the width and height of the string to do this:
using System;
using System.Drawing;
using System.Windows.Forms;

class MyForm: Form
{
     public MyForm()
     {
          Text = "Centered Form";
          BackColor = SystemColors.Window;
          ForeColor = SystemColors.WindowText;
          ResizeRedraw = true;
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          Graphics grfx      = pea.Graphics;
          string   str       = "MyString";
          SizeF    sizefText = grfx.MeasureString(str, Font);

          grfx.DrawString(str, Font, new SolidBrush(ForeColor), 
                          (ClientSize.Width  - sizefText.Width)  / 2, 
                          (ClientSize.Height - sizefText.Height) / 2);
     }
}
>Is there a way to know the the width and height of a string in pixels?
-----------------------------------------

Cathi Gero, CPA
Prenia Software & Consulting Services
Microsoft C# / .NET MVP
Mere Mortals for .NET MVP
cgero@prenia.com
www.prenia.com
Weblog: blogs.prenia.com/cathi
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform