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:
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)
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!