Csharp/C Sharp by API/System.Drawing — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 15:31, 26 мая 2010

FontFamily.GetCellAscent

  
  using System;
  using System.Drawing;
  using System.Collections;
  using System.ruponentModel;
  using System.Windows.Forms;
  using System.Data;
  public class Test{
    static void Main() {
      FontFamily myFamily = new FontFamily("Verdana");
      Font myFont = new Font(myFamily, 12);
      int fontHeight = myFont.Height;
      Console.WriteLine("Measurements are in GraphicsUnit." + myFont.Unit.ToString());
      Console.WriteLine("The Verdana family.");
      // Print our Family ties...
      Console.WriteLine("Ascent for bold Verdana: " + myFamily.GetCellAscent(FontStyle.Bold));
      Console.WriteLine("Descent for bold Verdana: " + myFamily.GetCellDescent(FontStyle.Bold));
      Console.WriteLine("Line spacing for bold Verdana: " + myFamily.GetLineSpacing(FontStyle.Bold));
      Console.WriteLine("Height for bold Verdana: " + myFamily.GetEmHeight(FontStyle.Bold));
    }
  }


FontFamily.GetCellDescent

  
  using System;
  using System.Drawing;
  using System.Drawing.Drawing2D;
  using System.Collections;
  using System.ruponentModel;
  using System.Windows.Forms;
  using System.Data;
  using System.Drawing.Imaging;
  public class Test
  {
    static void Main() 
    {
      // Create the format string
      String formatString = "{0,-16}{1,8}{2,9}{3,10}{4,14}";
      // Write the first line of the table
      Console.WriteLine(formatString, "Font Family Name", "Ascent", "Descent",
                      "EmHeight", "Line Spacing");
      // Write font metrics for Courier New font family
      FontFamily ff = new FontFamily("Courier New");
      Console.WriteLine(formatString, ff.GetName(0),
        ff.GetCellAscent(FontStyle.Regular),
        ff.GetCellDescent(FontStyle.Regular),
        ff.GetEmHeight(FontStyle.Regular),
        ff.GetLineSpacing(FontStyle.Regular));
    }
  }


FontFamily.GetEmHeight

  
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form {
    protected override void OnPaint(PaintEventArgs e) {
    Graphics g = e.Graphics;
    g.FillRectangle(Brushes.White, this.ClientRectangle);
    FontFamily ff = new FontFamily("Times New Roman");
    float emSizeInGU = 24f;
      Font f = new Font(ff, emSizeInGU);
    int emSizeInDU = ff.GetEmHeight(FontStyle.Regular);
    int ascentInDU = ff.GetCellAscent(FontStyle.Regular);
    int descentInDU = ff.GetCellDescent(FontStyle.Regular);
    int lineSpacingInDU = ff.GetLineSpacing(FontStyle.Regular);
    float ascentInGU = ascentInDU * (emSizeInGU / emSizeInDU);
    float descentInGU = descentInDU * (emSizeInGU / emSizeInDU);
    float lineSpacingInGU = lineSpacingInDU * (emSizeInGU / emSizeInDU);
    PointF textOrigin = new PointF(20, 20);
    PointF nextLineOrigin = new PointF(textOrigin.X,textOrigin.Y + f.Height);
    g.DrawString("AxgQ", f, Brushes.Black, textOrigin);
    g.DrawString("AxgQ", f, Brushes.Black, nextLineOrigin);
    int lineLen = 100;
    g.DrawLine(Pens.Blue,textOrigin,new PointF(textOrigin.X + lineLen, textOrigin.Y));
    g.DrawLine(Pens.Red,nextLineOrigin,new PointF(nextLineOrigin.X + lineLen, nextLineOrigin.Y));
    PointF p = new PointF(textOrigin.X,textOrigin.Y + lineSpacingInGU);
    g.DrawLine(Pens.Blue, p,new PointF(p.X + lineLen, p.Y));
    p = new PointF(nextLineOrigin.X,nextLineOrigin.Y + lineSpacingInGU);
    g.DrawLine(Pens.Red, p,new PointF(p.X + lineLen, p.Y));
    p = new PointF(textOrigin.X,textOrigin.Y + lineSpacingInGU - ascentInGU);
    g.DrawLine(Pens.Blue, p,new PointF(p.X + lineLen, p.Y));
    p = new PointF(nextLineOrigin.X, nextLineOrigin.Y +lineSpacingInGU - ascentInGU);
    g.DrawLine(Pens.Red, p, new PointF(p.X + lineLen, p.Y));
    p = new PointF(textOrigin.X,textOrigin.Y + lineSpacingInGU + descentInGU);
    g.DrawLine(Pens.Blue, p,new PointF(p.X + lineLen, p.Y));
    p = new PointF(nextLineOrigin.X,nextLineOrigin.Y + lineSpacingInGU + descentInGU);
    g.DrawLine(Pens.Red, p,new PointF(p.X + lineLen, p.Y));
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}


FontFamily.GetLineSpacing

  
  using System;
  using System.Drawing;
  using System.Collections;
  using System.ruponentModel;
  using System.Windows.Forms;
  using System.Data;
  public class Test{
    static void Main() {
      FontFamily myFamily = new FontFamily("Verdana");
      Font myFont = new Font(myFamily, 12);
      int fontHeight = myFont.Height;
      Console.WriteLine("Measurements are in GraphicsUnit." + myFont.Unit.ToString());
      Console.WriteLine("The Verdana family.");
      // Print our Family ties...
      Console.WriteLine("Ascent for bold Verdana: " + myFamily.GetCellAscent(FontStyle.Bold));
      Console.WriteLine("Descent for bold Verdana: " + myFamily.GetCellDescent(FontStyle.Bold));
      Console.WriteLine("Line spacing for bold Verdana: " + myFamily.GetLineSpacing(FontStyle.Bold));
      Console.WriteLine("Height for bold Verdana: " + myFamily.GetEmHeight(FontStyle.Bold));
    }
  }


FontFamily.IsStyleAvailable

  
/*
GDI+ Programming in C# and VB .NET
by Nick Symmonds
Publisher: Apress
ISBN: 159059035X
*/
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
namespace FontAttr_c
{
    /// <summary>
    /// Summary description for FontAttr.
    /// </summary>
    public class FontAttr : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Button cmdGo;
    private System.Windows.Forms.Panel P1;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ruponentModel.Container components = null;
        public FontAttr()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();
            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }
        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
      this.cmdGo = new System.Windows.Forms.Button();
      this.P1 = new System.Windows.Forms.Panel();
      this.SuspendLayout();
      // 
      // cmdGo
      // 
      this.cmdGo.Location = new System.Drawing.Point(264, 288);
      this.cmdGo.Name = "cmdGo";
      this.cmdGo.Size = new System.Drawing.Size(56, 24);
      this.cmdGo.TabIndex = 0;
      this.cmdGo.Text = "GO";
      this.cmdGo.Click += new System.EventHandler(this.cmdGo_Click);
      // 
      // P1
      // 
      this.P1.AutoScroll = true;
      this.P1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
      this.P1.Location = new System.Drawing.Point(16, 32);
      this.P1.Name = "P1";
      this.P1.Size = new System.Drawing.Size(304, 240);
      this.P1.TabIndex = 1;
      // 
      // FontAttr
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(342, 323);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                  this.P1,
                                                                  this.cmdGo});
      this.MaximizeBox = false;
      this.MinimizeBox = false;
      this.Name = "FontAttr";
      this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
      this.Text = "FontAttr";
      this.Load += new System.EventHandler(this.FontAttr_Load);
      this.ResumeLayout(false);
    }
        #endregion
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new FontAttr());
        }
    private void FontAttr_Load(object sender, System.EventArgs e)
    {
    }
    private void EnumInstalledFonts()
    {
      FontStyle Style;
      int y = 0;
      foreach (FontFamily ff in FontFamily.Families)
      {
        if ( ff.IsStyleAvailable(Style = FontStyle.Regular) )
          AddString(ff, ref y, Style);
        if ( ff.IsStyleAvailable(Style = FontStyle.Bold) )
          AddString(ff, ref y, Style);
        if ( ff.IsStyleAvailable(Style = FontStyle.Italic) )
          AddString(ff, ref y, Style);
        if ( ff.IsStyleAvailable(Style = FontStyle.Strikeout) )
          AddString(ff, ref y, Style);
        if ( ff.IsStyleAvailable(Style = FontStyle.Underline) )
          AddString(ff, ref y, Style);
      }
    }
    private void AddString(FontFamily ff, ref int y, FontStyle Style)
    {
      using ( Font fnt = new Font(ff, 12, Style, GraphicsUnit.Pixel) )
      {
        int LineSpace = (int)(ff.GetLineSpacing(Style) * 
                              fnt.Size / ff.GetEmHeight(Style));
        y += LineSpace + 2;
        PictureBox P = new PictureBox();
        P.Height = LineSpace;
        P.Width = P1.Width;
        Bitmap B = new Bitmap(P.Width, P.Height);
        using (Graphics G = Graphics.FromImage(B))
        {
          G.DrawString(ff.Name + " : Style = " + Style.ToString(), 
                       fnt, Brushes.Black, 0, 0);
        }
        P.Image=B;
        P1.Controls.Add(P);
        P1.Controls[P1.Controls.Count-1].Location = new Point(2, y);
        if ( y < P1.Height )
          P1.Refresh();
      }
    }
    private void cmdGo_Click(object sender, System.EventArgs e)
    {
      P1.Controls.Clear();
      EnumInstalledFonts();
    }
    }
}


Font.FontFamily

  
using System;
using System.Drawing;
using System.Windows.Forms;
   
class AllAboutFont: Form
{
     public static void Main()
     {
          Application.Run(new AllAboutFont());
     }
     public AllAboutFont()
     {
          ResizeRedraw = true; 
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
     }     
     protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
     {
          grfx.DrawString(
                    "Name: "               + Font.Name            + "\n" +
                    "FontFamily: "         + Font.FontFamily      + "\n" +
                    "FontStyle: "          + Font.Style           + "\n" +
                    "Bold: "               + Font.Bold            + "\n" +
                    "Italic: "             + Font.Italic          + "\n" +
                    "Underline: "          + Font.Underline       + "\n" +
                    "Strikeout: "          + Font.Strikeout       + "\n" +
                    "Size: "               + Font.Size            + "\n" +
                    "GraphicsUnit: "       + Font.Unit            + "\n" +
                    "SizeInPoints: "       + Font.SizeInPoints    + "\n" +
                    "Height: "             + Font.Height          + "\n" +
                    "GdiCharSet: "         + Font.GdiCharSet      + "\n" +
                    "GdiVerticalFont : "   + Font.GdiVerticalFont + "\n" +
                    "GetHeight(): "        + Font.GetHeight()     + "\n" +
                    "GetHeight(grfx): "    + Font.GetHeight(grfx) + "\n" +
                    "GetHeight(100 DPI): " + Font.GetHeight(100),
                    Font, new SolidBrush(clr), Point.Empty);
     }
}


Font.GetHeight

  
using System;
using System.Drawing;
using System.Windows.Forms;
   
class TextOnBaseline: Form{
     public static void Main() {
          Application.Run(new TextOnBaseline());
     }
     public TextOnBaseline() {
          ResizeRedraw = true; 
     }
     protected override void OnPaint(PaintEventArgs pea) {
          DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
     }
     protected void DoPage(Graphics grfx, Color clr, int cx, int cy) {
          float yBaseline = cy / 2;
          Pen   pen       = new Pen(clr);
   
          grfx.DrawLine(pen, 0, yBaseline, cx, yBaseline);
   
          Font font = new Font("Times New Roman", 144);
   
          float cyLineSpace = font.GetHeight(grfx);
          int   iCellSpace  = font.FontFamily.GetLineSpacing(font.Style);
          int   iCellAscent = font.FontFamily.GetCellAscent(font.Style);
          float cyAscent    = cyLineSpace * iCellAscent / iCellSpace;
   
          grfx.DrawString("Baseline", font, new SolidBrush(clr),
                          0, yBaseline - cyAscent);
     }
}


Font.Height

  
using System;
using System.Drawing;
using System.Windows.Forms;
public class FontConstructor3 : Form
{
  public FontConstructor3()
  {
        Text = "Font Constructor";
    Size = new Size(350,200);
    FontFamily ff = new FontFamily("Times New Roman");
    Font fnt = new Font(ff, 12, FontStyle.Bold | FontStyle.Italic);
    Font = fnt;
    RichTextBox rtxt = new RichTextBox();
    rtxt.Text = "first line.\n" +
          "This is a second line of text.";
    rtxt.Text += "\nFont Name:\t" + Font.Name;
    rtxt.Text += "\nFont Family:\t" + Font.FontFamily;
    rtxt.Text += "\nFont Styles:\t" + Font.Style;
    rtxt.Text += "\nFont Size:\t" + Font.Size;
    rtxt.Text += "\nFont Height:\t" + Font.Height;
    rtxt.Text += "\nFont Units:\t" + Font.Unit;
    rtxt.Multiline = true;
    rtxt.Dock = DockStyle.Fill;
    rtxt.Parent = this;
  }
  static void Main() 
  {
    Application.Run(new FontConstructor3());
  }
}


Font.Name

  
using System;
using System.Drawing;
using System.Windows.Forms;
public class FontConstructor3 : Form
{
  public FontConstructor3()
  {
        Text = "Font Constructor";
    Size = new Size(350,200);
    FontFamily ff = new FontFamily("Times New Roman");
    Font fnt = new Font(ff, 12, FontStyle.Bold | FontStyle.Italic);
    Font = fnt;
    RichTextBox rtxt = new RichTextBox();
    rtxt.Text = "first line.\n" +
          "This is a second line of text.";
    rtxt.Text += "\nFont Name:\t" + Font.Name;
    rtxt.Text += "\nFont Family:\t" + Font.FontFamily;
    rtxt.Text += "\nFont Styles:\t" + Font.Style;
    rtxt.Text += "\nFont Size:\t" + Font.Size;
    rtxt.Text += "\nFont Height:\t" + Font.Height;
    rtxt.Text += "\nFont Units:\t" + Font.Unit;
    rtxt.Multiline = true;
    rtxt.Dock = DockStyle.Fill;
    rtxt.Parent = this;
  }
  static void Main() 
  {
    Application.Run(new FontConstructor3());
  }
}


Font.Size

  
using System;
using System.Drawing;
using System.Windows.Forms;
public class FontConstructor3 : Form
{
  public FontConstructor3()
  {
        Text = "Font Constructor";
    Size = new Size(350,200);
    FontFamily ff = new FontFamily("Times New Roman");
    Font fnt = new Font(ff, 12, FontStyle.Bold | FontStyle.Italic);
    Font = fnt;
    RichTextBox rtxt = new RichTextBox();
    rtxt.Text = "first line.\n" +
          "This is a second line of text.";
    rtxt.Text += "\nFont Name:\t" + Font.Name;
    rtxt.Text += "\nFont Family:\t" + Font.FontFamily;
    rtxt.Text += "\nFont Styles:\t" + Font.Style;
    rtxt.Text += "\nFont Size:\t" + Font.Size;
    rtxt.Text += "\nFont Height:\t" + Font.Height;
    rtxt.Text += "\nFont Units:\t" + Font.Unit;
    rtxt.Multiline = true;
    rtxt.Dock = DockStyle.Fill;
    rtxt.Parent = this;
  }
  static void Main() 
  {
    Application.Run(new FontConstructor3());
  }
}


Font.SizeInPoints

  

using System;
using System.Drawing;
using System.Windows.Forms;
   
class HowdyWorld: Form
{
     public static void Main()
     {
          Application.Run(new HowdyWorld());
     }
     public HowdyWorld()
     {
          ResizeRedraw = true; 
          MinimumSize = SystemInformation.MinimumWindowSize + new Size(0,1);
          
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
     }     
     protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
     {
          Font  font   = new Font("Times New Roman", 10, FontStyle.Italic);
          SizeF sizef  = grfx.MeasureString(Text, font);
          float fScale = Math.Min(cx / sizef.Width, cy / sizef.Height);
   
          font = new Font(font.Name, fScale * font.SizeInPoints, font.Style);
          sizef = grfx.MeasureString(Text, font);
   
          grfx.DrawString(Text, font, new SolidBrush(clr), 
                          (cx  - sizef.Width ) / 2, (cy - sizef.Height) / 2);
     }
}


FontStyle.Italic

   
using System;
using System.Drawing;
using System.Windows.Forms;
class BoldAndItalic : Form {
    public static void Main() {
        Application.Run(new BoldAndItalic());
    }
    public BoldAndItalic() {
        Text = "Bold and Italic Text";
        ResizeRedraw = true;
    }
    protected override void OnPaint(PaintEventArgs pea) {
        DoPage(pea.Graphics, ForeColor, ClientSize.Width, ClientSize.Height);
    }
    protected void DoPage(Graphics grfx, Color clr, int cx, int cy) {
        float x = 0;
        float y = 0;
        grfx.DrawString("text", new Font(this.Font, FontStyle.Bold), new SolidBrush(Color.AliceBlue), x, y);
    }
}


FontStyle.Regular

  
using System;
using System.Drawing;
using System.Windows.Forms;
public class FontFamilies : Form
{
  public FontFamilies()
  {
    Size = new Size(350,200);
    RichTextBox rtxt = new RichTextBox();
    rtxt.Multiline = true;
    rtxt.Dock = DockStyle.Fill;
    rtxt.Parent = this;
    FontFamily[] ffArray = FontFamily.Families;
    foreach( FontFamily ff in ffArray )
    {
      if (ff.IsStyleAvailable(FontStyle.Regular))
      {
        rtxt.Text += ff.Name + "\n"; 
      }
    }
  }
  static void Main() 
  {
    Application.Run(new FontFamilies());
  }
}


FontStyle.Strikeout

  
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
   public class UsingFonts : System.Windows.Forms.Form
   {
      private System.ruponentModel.Container components = null;
      public UsingFonts()
      {
         InitializeComponent();
      }
      private void InitializeComponent()
      {
         this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
         this.ClientSize = new System.Drawing.Size(504, 109);
         this.Name = "UsingFonts";
         this.Text = "UsingFonts";
      }
      static void Main() 
      {
         Application.Run( new UsingFonts() );
      }
      protected override void OnPaint(PaintEventArgs paintEvent )
      {
         Graphics graphicsObject = paintEvent.Graphics;
         SolidBrush brush = new SolidBrush( Color.DarkBlue );
         // arial, 12 pt bold
         FontStyle style = FontStyle.Bold;
         Font arial = new Font( new FontFamily( "Arial" ), 12, style );
         // times new roman, 12 pt regular
         style = FontStyle.Regular;
         Font timesNewRoman = new Font( "Times New Roman", 12, style );
         // courier new, 16 pt bold and italic
         style = FontStyle.Bold | FontStyle.Italic;
         Font courierNew = new Font( "Courier New", 16, style );
         // tahoma, 18 pt strikeout
         style = FontStyle.Strikeout;
         Font tahoma = new Font( "Tahoma", 18, style );
         graphicsObject.DrawString( arial.Name + 
            " 12 point bold.", arial, brush, 10, 10 );
         graphicsObject.DrawString( timesNewRoman.Name + 
            " 12 point plain.", timesNewRoman, brush, 10, 30 );
         graphicsObject.DrawString( courierNew.Name + 
            " 16 point bold and italic.", courierNew, 
            brush, 10, 54 );
         graphicsObject.DrawString( tahoma.Name +
            " 18 point strikeout.", tahoma, brush, 10, 75 );
      } 
   }


Font.Unit

  
using System;
using System.Drawing;
using System.Windows.Forms;
public class FontConstructor3 : Form
{
  public FontConstructor3()
  {
        Text = "Font Constructor";
    Size = new Size(350,200);
    FontFamily ff = new FontFamily("Times New Roman");
    Font fnt = new Font(ff, 12, FontStyle.Bold | FontStyle.Italic);
    Font = fnt;
    RichTextBox rtxt = new RichTextBox();
    rtxt.Text = "first line.\n" +
          "This is a second line of text.";
    rtxt.Text += "\nFont Name:\t" + Font.Name;
    rtxt.Text += "\nFont Family:\t" + Font.FontFamily;
    rtxt.Text += "\nFont Styles:\t" + Font.Style;
    rtxt.Text += "\nFont Size:\t" + Font.Size;
    rtxt.Text += "\nFont Height:\t" + Font.Height;
    rtxt.Text += "\nFont Units:\t" + Font.Unit;
    rtxt.Multiline = true;
    rtxt.Dock = DockStyle.Fill;
    rtxt.Parent = this;
  }
  static void Main() 
  {
    Application.Run(new FontConstructor3());
  }
}


new Font(String fontName, int size)

    
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class FormPaintEvent : System.Windows.Forms.Form
{
  private System.ruponentModel.Container components = null;
  public FormPaintEvent()
  {
    InitializeComponent();
    this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
  }
  protected override void Dispose( bool disposing )
  {
    if( disposing )
    {
      if (components != null) 
      {
        components.Dispose();
      }
    }
    base.Dispose( disposing );
  }
  private void InitializeComponent()
  {
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.Text = "Form1";
  }
  [STAThread]
  static void Main() 
  {
    Application.Run(new FormPaintEvent());
  }
  private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
  {
    Graphics g = e.Graphics;
    g.DrawString("String...", new Font("Times New Roman", 20), new SolidBrush(Color.Black), 40, 10);
  }
}


new Font("Times New Roman", 10, FontStyle.Italic)

  

using System;
using System.Drawing;
using System.Windows.Forms;
   
class HowdyWorld: Form
{
     public static void Main()
     {
          Application.Run(new HowdyWorld());
     }
     public HowdyWorld()
     {
          ResizeRedraw = true; 
          MinimumSize = SystemInformation.MinimumWindowSize + new Size(0,1);
          
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
     }     
     protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
     {
          Font  font   = new Font("Times New Roman", 10, FontStyle.Italic);
          SizeF sizef  = grfx.MeasureString(Text, font);
          float fScale = Math.Min(cx / sizef.Width, cy / sizef.Height);
   
          grfx.DrawString(Text, font, new SolidBrush(clr), 
                          (cx  - sizef.Width ) / 2, (cy - sizef.Height) / 2);
     }
}


SystemFonts.IconTitleFont

 
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
public class Form1 : Form
{
    private System.Windows.Forms.Label label2;
    
  public Form1() {
        this.Font = SystemFonts.IconTitleFont;
        this.AutoScaleDimensions = new System.Drawing.SizeF(6.0F, 13.0F);
        InitializeComponent();
        SystemEvents.UserPreferenceChanged += new UserPreferenceChangedEventHandler(SystemEvents_UserPreferenceChanged);
  }
    private void SystemEvents_UserPreferenceChanged(object sender, UserPreferenceChangedEventArgs e)
    {
        if (e.Category == UserPreferenceCategory.Window)
        {
            this.Font = SystemFonts.IconTitleFont;
        }
    }
    private void InitializeComponent()
    {
        this.label2 = new System.Windows.Forms.Label();
        this.SuspendLayout();
        // 
        // label2
        // 
        this.label2.Location = new System.Drawing.Point(12, 9);
        this.label2.Name = "label2";
        this.label2.Size = new System.Drawing.Size(220, 55);
        this.label2.TabIndex = 2;
        this.label2.Text = "Try changing the Small Fonts/Large Fonts setting for th" +
            "e computer.";
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(244, 138);
        this.Controls.Add(this.label2);
        this.Text = "Form1";
        this.ResumeLayout(false);
    }
  [STAThread]
  static void Main()
  {
    Application.EnableVisualStyles();
    Application.Run(new Form1());
  }
}