Font: GetHeight : Font « GUI Windows Form « C# / C Sharp






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

 








Related examples in the same category

1.All font properties
2.new Font("Times New Roman", 10, FontStyle.Italic)
3.new Font(strFont, fSize)
4.new Font(fontRegular, FontStyle.Italic);
5.Text on Baseline
6.Font: SizeInPoints
7.Font with different size
8.System Fonts: Icon Title FontSystem Fonts: Icon Title Font
9.Load all system installed fontsLoad all system installed fonts
10.Timer based animation for fontTimer based animation for font
11.Get all system installed fontGet all system installed font
12.Font listFont list