Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Font Size
Message
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Titre:
Divers
Thread ID:
00755775
Message ID:
00755884
Vues:
15
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
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform