HOWTO: Use the system font in dialogs and toolwindows of a Visual Studio add-in

Author: Carlos J. Quintero (Microsoft MVP) Applies to: Microsoft Visual Studio .NET 2002
Date: August 2007   Microsoft Visual Studio .NET 2003
Updated: March 2013   Microsoft Visual Studio 2005
      Microsoft Visual Studio 2008
      Microsoft Visual Studio 2010
      Microsoft Visual Studio 2012


This article explains how to use the system font in the dialogs (modal forms) and toolwindows of a Visual Studio add-in.

The default (the user can change it in the Control Panel, Display Properties) system font  is different for each operating system:

  • Windows NT 4.0 and previous operating systems use MS Sans Serif 8.25pt.
  • Windows 2000 and Windows XP use Tahoma 8.25 pt, which happens to have similar metrics to the previous MS Sans Serif font.
  • Windows Vista uses Segoe UI 9pt, which happens to have very different metrics.

Visual Studio allows you to configure its fonts in the "Tools", "Options" window, "Environment", "Fonts and Colors" section. However, there is a different behavior between versions regarding the font used in dialogs and toolwindows:

  • Visual Studio .NET 2002 and 2003 dialogs and toolwindows do not honor the system font. Instead, they provide in the "Fonts and Colors" section a "Dialog and Tool Windows" font setting to specify the font to use. Of course, applications should honor the system font instead of ignoring it (Microsoft Office 2007 also violates this rule) but add-ins for Visual Studio .NET 2002/2003 should honor that font setting to have the same appearance than the Visual Studio dialogs of these versions.
  • Visual Studio 2005 and higher fix this problem and they honor the system font, removing the "Dialog and Tool Windows" font setting.

More Information

Add-ins for Visual Studio .NET 2002/2003 can get the "Dialog and Tool Windows" font using this code:

Dim colProperties As Properties
Dim sFontFamily As String
Dim sngFontSize As Single
Dim objValue As Object
Dim objFont As Font

colProperties = objDTE.Properties("FontsAndColors", "Dialogs and Tool Windows")

If Not (colProperties Is Nothing) Then

   objValue = colProperties.Item("FontFamily").Value
   sFontFamily = objValue.ToString

   objValue = colProperties.Item("FontSize").Value
   sngFontSize = CType(objValue, Single)

   objFont = New Font(sFontFamily, sngFontSize)

End If

Note: Since that code creates an object of the Font class, which implements the IDisposable interface, when the add-in is unloaded it should call the Dispose method of the Font class.

Add-ins for Visual Studio 2005 or higher can use the new SystemFonts class of the .NET Framework 2.0:

objFont = SystemFonts.MessageBoxFont

Note: you can't use SystemFonts.DefaultFont because it returns "MS Sans Serif" for all operating systems, and you can't use SystemFonts.DialogFont because it returns "Tahoma" even if the operating system is Windows Vista (which uses Segoe UI) or if the user has set a different font in the Control Panel, Display Settings. Instead, SystemFonts.MessageFont seems to return the correct font.

Once you have the correct font to assign to forms or usercontrols of toolwindows, the next issue to handle is how to scale the form or usercontrol. The form or user control needs to be scaled because of:

  • Different monitor DPI settings: while most monitors are configured for 96 DPI, large monitors can be configured for 120 DPI or higher.
  • Different system font sizes: while the Tahoma font of Windows XP uses 8.25pt, the Segoe UI font of Windows Vista use 9pt.

If not scaled, the size of labels, buttons, etc. could be not big enough to accomodate a larger font than the one used to design the form or usercontrol.

The .NET Framework provides a property to autoscale a form when you change its font:

  • Form.AutoScale (.NET 1.x): True (default value)
  • Form.AutoScaleMode (.NET 2.0 and higher): AutoScaleMode.Font (default value)

However, the autoscaling of the .NET Framework produces forms much larger than desired. This is noticeable if you are using Windows Vista and show the "Data", "Add New Data Source..." window, which is quite large and you can resize it to a smaller size. This happens because that window is an autoscaled .NET 2.0 form. Compare it to the "Tools", "Options" window, which also uses the system font but shows a much more suitable size.

Instead of using the autoscaling of the .NET Framework, you can use the following function, which deactivates the autoscaling and scales the form with a factor that produces a more suitable size. The function works with forms or usercontrols:

(.NET 1.x)

Public Sub SetFontAndScale(ByVal ctlControl As Control, ByVal objFont As Font)

   Dim sngRatio As Single

   sngRatio = objFont.Size / ctlControl.Font.Size
   If TypeOf ctlControl Is Form Then
      CType(ctlControl, Form).AutoScale = False
   End If 

   ctlControl.Font = objFont

End Sub

(.NET 2.0)

Public Sub SetFontAndScale(ByVal ctlControl As Control, ByVal objFont As Font)

   Dim sngRatio As Single
   sngRatio = objFont.Size / ctlControl.Font.Size
   If TypeOf ctlControl Is Form Then
      CType(ctlControl, Form).AutoScaleMode = AutoScaleMode.None
   End If 
   ctlControl.Font = objFont
   ctlControl.Scale(New SizeF(sngRatio, sngRatio))
End Sub

Go to the 'Visual Studio Extensibility (VSX)' web site for more articles like this (Articles section)