public
Last active

Outlines an issue with VisualStyleRenderer.GetFont / GetThemeFont whereby an actual value is never returned

  • Download Gist
gistfile1.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
 
namespace VisualStyleGetFontTest {
class Program {
static void Main(string[] args) {
 
//VisualStyleElement.Button.CheckBox.CheckedNormal
 
Type vsElementType = typeof(VisualStyleElement);
Type[] elementTypes = vsElementType.GetNestedTypes();
 
// enum the groups
foreach (Type elementType in elementTypes) {
 
// enum the elements
foreach (Type subType in elementType.GetNestedTypes()) {
// enum the parts
foreach (PropertyInfo prop in subType.GetProperties(BindingFlags.Public | BindingFlags.Static)) {
VisualStyleElement element = prop.GetValue(null, null) as VisualStyleElement;
Font font;
 
try {
VisualStyleRenderer renderer = new VisualStyleRenderer(element);
 
using (Graphics g = Graphics.FromHwnd(IntPtr.Zero)) {
font = renderer.GetFont(g, FontProperty.GlyphFont);
}
 
Console.WriteLine(elementType.Name + "." + subType.Name + "." + prop.Name + " - font: " + (font == null ? "null" : font.ToString()));
}
catch (ArgumentException) {
Console.WriteLine(elementType.Name + "." + subType.Name + "." + prop.Name + " - font: ArgumentException");
}
}
}
 
}
 
Console.Read();
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.