Font: GetHeight

   
 


using System;
using System.Drawing;
using System.Windows.Forms;
   
class FontMenuForm: Form
{
     protected string strText = "Sample Text";
     protected Font font = new Font("Times New Roman", 24, FontStyle.Italic);
   
     public static void Main()
     {
          Application.Run(new FontMenuForm());
     }
     public FontMenuForm()
     {
          ResizeRedraw = true;
          Menu = new MainMenu();
          Menu.MenuItems.Add("&Font!", new EventHandler(MenuFontOnClick));
     }
     void MenuFontOnClick(object obj, EventArgs ea)
     {
          FontDialog dlg = new FontDialog();
          dlg.Font = font;
   
          if (dlg.ShowDialog() == DialogResult.OK)
          {
               font = dlg.Font;
               Invalidate();
          }
     }
     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)
     {
          SizeF sizef = grfx.MeasureString(strText, font);
          Brush brush = new SolidBrush(clr);
   
          grfx.DrawString(strText, font, brush, (cx - sizef.Width) / 2,
                                                (cy - sizef.Height) / 2);
     }
     public float GetAscent(Graphics grfx, Font font)
     {
          return font.GetHeight(grfx) * 
                    font.FontFamily.GetCellAscent(font.Style) /
                         font.FontFamily.GetLineSpacing(font.Style);
     }
     public float GetDescent(Graphics grfx, Font font)
     {
          return font.GetHeight(grfx) * 
                    font.FontFamily.GetCellDescent(font.Style) /
                         font.FontFamily.GetLineSpacing(font.Style);
     }
     public float PointsToPageUnits(Graphics grfx, Font font)
     {
          float fFontSize;
   
          if (grfx.PageUnit == GraphicsUnit.Display)
               fFontSize = 100 * font.SizeInPoints / 72;
          else
               fFontSize = grfx.DpiX * font.SizeInPoints / 72;
   
          return fFontSize;
     }
}

    


Font with different size

   
 


using System;
using System.Drawing;
using System.Windows.Forms;
   
class TwentyFourPointPrinterFonts: Form
{
     public static void Main()
     {
          Application.Run(new TwentyFourPointPrinterFonts());
     }
     public TwentyFourPointPrinterFonts()
     {
          Text = "Twenty-Four Point Printer Fonts";
          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)
     {
          Brush  brush     = new SolidBrush(clr);
          float  y         = 0;
          Font   font;
          string strFamily = "Times New Roman";
   
          font = new Font(strFamily, 24);
          grfx.DrawString("No GraphicsUnit, 24 points", font, brush, 0, y);
          y += font.GetHeight(grfx);
   
          font = new Font(strFamily, 24, GraphicsUnit.Point);
          grfx.DrawString("GraphicsUnit.Point, 24 units", font, brush, 0, y);
          y += font.GetHeight(grfx);
   
          font = new Font(strFamily, 1/ 3f, GraphicsUnit.Inch);
          grfx.DrawString("GraphicsUnit.Inch, 1/3 units", font, brush, 0, y);
          y += font.GetHeight(grfx);
   
          font = new Font(strFamily, 25.4f / 3, GraphicsUnit.Millimeter);
          grfx.DrawString("GraphicsUnit.Millimeter, 25.4/3 units", 
                          font, brush, 0, y);
          y += font.GetHeight(grfx);
   
          font = new Font(strFamily, 100, GraphicsUnit.Document);
          grfx.DrawString("GraphicsUnit.Document, 100 units", 
                          font, brush, 0, y);
          y += font.GetHeight(grfx);
   
          font = new Font(strFamily, 100f / 3, GraphicsUnit.Pixel);
          grfx.DrawString("GraphicsUnit.Pixel, " + 100f / 3 + " units",
                          font, brush, 0, y);
          y += font.GetHeight(grfx);
   
          font = new Font(strFamily, 100f / 3, GraphicsUnit.World);
          grfx.DrawString("GraphicsUnit.World, " + 100f / 3 + " units", 
                          font, brush, 0, y);
     }
}

    


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);
     }
}

    


Text on Baseline

   
 
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);
     }
}

    


new Font(fontRegular, 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);
    }
}

    


new Font(strFont, fSize)

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

class FontSizes: Form
{
public static void Main()
{
Application.Run(new FontSizes());
}
public FontSizes()
{
Text = “Font Sizes”;
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)
{
string strFont = “Times New Roman”;
Brush brush = new SolidBrush(clr);
float y = 0;

for (float fSize = 6; fSize <= 12; fSize += 0.25f) { Font font = new Font(strFont, fSize); grfx.DrawString(strFont + " in " + fSize + " points", font, brush, 0, y); } } } [/csharp]

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);
     }
}