Even a chimp can write code

Friday, May 26, 2006

Theme support in Windows Presentation Foundation

Question: I want to create a custom Windows Presentation Foundation button whose look and feel depends on the Windows desktop theme currently in use. How do I get notified when the user changes the theme?

Neil Kronlage provided this answer:
The WPF theme engine will switch styles automatically for you if you place them in separate ResourceDictionary XAML files compiled in your application.

The files need to be named

themes\<ThemeName>.<ThemeColor>.xaml

for example, these are the themes that Microsoft produces:

themes\luna.normalcolor.xaml
themes\luna.homestead.xaml
themes\luna.metallic.xaml
themes\royale.normalcolor.xaml
themes\aero.normalcolor.xaml

The case for classic is slightly different:

themes\classic.xaml

Windows Presentation Foundation does not provide an event when the theme changes. If you need to go beyond what is provided by styles in a resource dictionary, you will need to listen to the WM_THEMECHANGE message in a window hook.


Tags:

Email this | Bookmark this