Partager via


How to determine if visual styles are enabled?

[I thought I would write this piece as a response to Cory Smith's recent posts (here is the first post and then the followup) on the topic]

If you are a control author or simply custom painting controls in your app, you have probably felt the need for a flag that tells you whether to render with visual styles or not. You could use this flag to switch between your visual styles rendering code and fallback code that works when visual styles aren't available or enabled.

In v1.1, the Framework unfortunately does not provide a property you could query to determine this. You need to p/invoke into UxTheme API - Cory has posted a code snippet that shows how to do this.

Here are the conditions you need to check:

  • Does the current OS support visual styles? Use OSFeature.IsPresent(OSFeature.Themes) (OSFeature is a class in System.Windows.Forms)
  • Is a visual style currently active and applied to my application? This has to do with the fact that a user can turn off visual styles by either switching to Windows Classic mode or stopping/disabling the Themes service. Use a combination of IsAppThemed and IsThemeActive to determine this.
  • Here is the third condition and this is important - Is my app using ComCtl 6 to render its controls? This is equivalent to asking if Application.EnableVisualStyles() has been called or a manifest has been provided (see my earlier post on this for more information). To check this, you can p/invoke DllGetVersion and check if the version of comctl32.dll is 6 or greater.

No doubt, this is much too complicated an answer for a simple question - do I render with Visual Styles? In Whidbey, we plan to provide a single property you can query - something like Application.RenderWithVisualStyles (we are currently working out the naming and placement of this property).

For more on visual styles support in Whidbey, see this post. We have received a great deal of feedback (especially through blog posts!) on how we can improve our visual styles support and we have tried to address most of the issues developers ran into. So when you get hold of the Whidbey Beta, check it out and let us know if we have missed anything! And remember the Application.DoEvents() workaround to get imagelist images to show up? Well, you don't need it in Whidbey, so now you can have one less line of code in your Main() method :-)

Comments