Determining the line height of a font is very important thing for programmers creating applications that use changing texts as part of their UI. However, Microsoft(for some reason) decided to remove the .Height attribute from the .net CF Font object.

However, the height and width of a text can still be determined – if your code has access to a Graphics object. The object’s MeasureString method returns a SizeF parameter containing the height and the width of the string passed in….the values, of course, are in pixels!

The code example below creates a moving bar that permanently changes its color. In the middle of the bar, the text contained in drawString is displayed in white – this animated GIF simulates the effect:
sample Determine a fonts line height in .NET CF(.NET compact framework)


ElseIf Globals.Globals.TextMode = textmode_enum.series60 Then
Dim drawString As String = TextParser.getDateText
Dim myFont As New Font(System.Drawing.FontFamily.GenericSansSerif, 12, FontStyle.Regular)
Dim myTextBrush As New SolidBrush(Color.White)
Dim myBackBrush As New SolidBrush(Color.FromArgb(Int((254 * Rnd())), Int((254 * Rnd())), Int((254 * Rnd()))))
Dim y As Integer = Int(((MainForm.Height - 4 - gfx.MeasureString(drawString, myFont).Height) * Rnd()))
gfx.FillRectangle(myBackBrush, 0, y, MainForm.Width, 4 + gfx.MeasureString(drawString, myFont).Height)
gfx.DrawString(drawString, myFont, myTextBrush, (MainForm.Width-gfx.MeasureString(drawString, myFont).Width)/2, y + 2)
End If

Enjoy!

P.s. If anyone of you knows how the MS boys format the code that they put onto their blogs so neatly….PLEASE let me know!
P.s.2 If anyone knows WHY Microsoft removed the Font.Height property from .NET CF, please let me know too!


Related posts:

  1. WiFi FoFum – Free WLAN-Scanner
  2. Finding .NET Compact Framework information on MSDN
  3. Building solutions with the Microsoft .NET Compact Framework
  4. Obfuscating .NET Compact Framework(.NET CF) applications – or – an insane pain in the butt
  5. iContacts