Skip to content

Instantly share code, notes, and snippets.

@shellscape
Created September 15, 2011 12:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shellscape/1219126 to your computer and use it in GitHub Desktop.
Save shellscape/1219126 to your computer and use it in GitHub Desktop.
Outlines an issue with VisualStyleRenderer.GetFont / GetThemeFont whereby an actual value is never returned
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();
}
}
}
@mika76
Copy link

mika76 commented Jul 17, 2014

Interesting, have you reported this to Microsoft? I ask because I just opened a bug for VisualStyleRenderer not initializing a renderer for Menu Item. Check https://connect.microsoft.com/VisualStudio/feedback/details/921798/visualstylerenderer-visualstyleelement-menu-item-normal-returns-not-supported-always

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment