Skip to content

Instantly share code, notes, and snippets.

@drott
Created December 11, 2012 14:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drott/d4169b1068c203432846 to your computer and use it in GitHub Desktop.
Save drott/d4169b1068c203432846 to your computer and use it in GitHub Desktop.
Font Metrics Using oldschool API
namespace pinvokeGetTextMetrics
{
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Runtime.InteropServices;
class pinvokeMapped {
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
internal struct TEXTMETRIC
{
public int tmHeight;
public int tmAscent;
public int tmDescent;
public int tmInternalLeading;
public int tmExternalLeading;
public int tmAveCharWidth;
public int tmMaxCharWidth;
public int tmWeight;
public int tmOverhang;
public int tmDigitizedAspectX;
public int tmDigitizedAspectY;
public char tmFirstChar;
public char tmLastChar;
public char tmDefaultChar;
public char tmBreakChar;
public byte tmItalic;
public byte tmUnderlined;
public byte tmStruckOut;
public byte tmPitchAndFamily;
public byte tmCharSet;
};
public static TEXTMETRIC GetTextMetrics(Graphics graphics, Font font)
{
IntPtr hDC = graphics.GetHdc();
TEXTMETRIC textMetric;
IntPtr hFont = font.ToHfont();
try
{
IntPtr hFontPreviouse = SelectObject(hDC, hFont);
bool result = GetTextMetrics(hDC, out textMetric);
SelectObject(hDC, hFontPreviouse);
}
finally
{
DeleteObject(hFont);
graphics.ReleaseHdc(hDC);
}
return textMetric;
}
[DllImport("Gdi32.dll", CharSet=CharSet.Auto)]
public static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj);
[DllImport("Gdi32.dll", CharSet=CharSet.Auto)]
public static extern bool GetTextMetrics(IntPtr hdc, out TEXTMETRIC lptm);
[DllImport("Gdi32.dll", CharSet=CharSet.Auto)]
public static extern bool DeleteObject(IntPtr hdc);
}
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
FontFamily fontFamily = new FontFamily("Liberation Sans");
Font liberation = new Font(fontFamily, 16, FontStyle.Regular, GraphicsUnit.Pixel);
pinvokeMapped.TEXTMETRIC liberationMetrics = pinvokeMapped.GetTextMetrics(g, liberation);
g.DrawString("Liberation Sans Metrics on this GC: tmAscent: " + liberationMetrics.tmAscent +
" tmDescent: " + liberationMetrics.tmDescent +
" tmHeight " + liberationMetrics.tmHeight,
liberation, System.Drawing.Brushes.Blue, new Point(30, 30));
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (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.SuspendLayout();
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(677, 211);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment