Font Metrics Illustration : Font Metrics « 2D « VB.Net






Font Metrics Illustration

Font Metrics Illustration
Imports System
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Text
Imports System.Drawing.Imaging
Imports System.Runtime.InteropServices

Public Class MainClass

  Shared Sub Main()
     Dim form1 As Form1 = new Form1
     Application.Run(form1)
  End Sub
  
End Class





Public Class Form1
  Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

  Public Sub New()
    MyBase.New()

    'This call is required by the Windows Form Designer.
    InitializeComponent()

    'Add any initialization after the InitializeComponent() call

  End Sub

  'Form overrides dispose to clean up the component list.
  Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    If disposing Then
      If Not (components Is Nothing) Then
        components.Dispose()
      End If
    End If
    MyBase.Dispose(disposing)
  End Sub

  'Required by the Windows Form Designer
  Private components As System.ComponentModel.IContainer

  'NOTE: The following procedure is required by the Windows Form Designer
  'It can be modified using the Windows Form Designer.  
  'Do not modify it using the code editor.
  <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    Me.SuspendLayout()
    '
    'Form1
    '
    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    Me.ClientSize = New System.Drawing.Size(492, 273)
    Me.MaximizeBox = False
    Me.MinimizeBox = False
    Me.Name = "Form1"
    Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
    Me.Text = "Form1"
    Me.ResumeLayout(False)

  End Sub

#End Region

  Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
    Dim ff As FontFamily = New FontFamily("Arial Black")
    Dim fnt As Font = New Font(ff, 75, FontStyle.Regular, GraphicsUnit.Pixel)

    Dim G As Graphics = Me.CreateGraphics()
    G.SmoothingMode = SmoothingMode.AntiAlias
    G.TextRenderingHint = TextRenderingHint.AntiAlias

    Dim LineSpace As Int32 = CInt(ff.GetLineSpacing(fnt.Style) * _
                          fnt.Size / ff.GetEmHeight(fnt.Style))
    Dim Descent As Int32 = CInt(ff.GetCellDescent(fnt.Style) * _
                          fnt.Size / ff.GetEmHeight(fnt.Style))
    Dim Ascent As Int32 = CInt(ff.GetCellAscent(fnt.Style) * _
                          fnt.Size / ff.GetEmHeight(fnt.Style))

    Dim BaseLineStart As Point = New Point(15, CInt(Me.Height * 3 / 5))
    Dim BaseLineEnd As Point = New Point(Me.Width - 15, CInt(Me.Height * 3 / 5))

    Dim StringPoint As Point = New Point(75, CInt(BaseLineStart.Y - Ascent))

    G.Clear(Color.AliceBlue)
    G.DrawString("A j Q", fnt, Brushes.Blue, Point.op_Implicit(StringPoint))
    G.DrawLine(Pens.Black, BaseLineStart, BaseLineEnd)

    Dim LineSize As Size = New Size(0, LineSpace)
    Dim AscentSize As Size = New Size(0, Ascent)
    Dim DescentSize As Size = New Size(0, Descent)
    G.DrawLine(Pens.Black, Point.op_Subtraction(BaseLineStart, LineSize), _
                           Point.op_Subtraction(BaseLineEnd, LineSize))
    G.DrawLine(Pens.Red, Point.op_Subtraction(BaseLineStart, AscentSize), _
                           Point.op_Subtraction(BaseLineEnd, AscentSize))
    G.DrawLine(Pens.DarkGreen, Point.op_Addition(BaseLineStart, DescentSize), _
                                 Point.op_Addition(BaseLineEnd, DescentSize))

    Dim AnnoFont As Font = New Font("Arial", 10)
    G.DrawString("Line Space = " + LineSpace.ToString(), AnnoFont, _
      Brushes.Black, _
      20, _
      CInt(BaseLineStart.Y - LineSpace - 12))

    G.DrawString("Ascent = " + Ascent.ToString(), AnnoFont, _
      Brushes.Red, _
      250, _
      CInt(BaseLineStart.Y - Ascent - 12))

    G.DrawString("Descent = " + Descent.ToString(), AnnoFont, _
      Brushes.DarkGreen, _
      350, _
      CInt(BaseLineStart.Y + Descent / 8))
  End Sub
End Class

           
       








Related examples in the same category