![]() |
![]() |
|||
![]() |
||||
![]() |
![]() |
Introduction 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:
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:
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:
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:
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 ctlControl.Scale(sngRatio) 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 Follow @VSExtensibility |
Copyright © 2000-2021 MZTools Software. All Rights Reserved. Legal Notice |