Permalink
Browse files

Fixing theme override not sticking if set in app ctor

  • Loading branch information...
danielmoore committed May 29, 2012
1 parent 6ac516e commit c763c2968e858bcc8f545a3059c7c4ca16657eca
Showing with 21 additions and 5 deletions.
  1. +15 −2 App.xaml.cs
  2. +6 −3 ThemeHelper.cs
View
@@ -1,5 +1,6 @@
using System.Reflection;
using System.Windows;
+using System;
namespace NorthHorizon.Samples.SystemThemeChange
{
@@ -8,10 +9,22 @@ namespace NorthHorizon.Samples.SystemThemeChange
/// </summary>
public partial class App : Application
{
- public App()
+ public App() : this(Environment.GetCommandLineArgs()) { }
+
+ public App(string[] args)
{
//SetTheme("luna", "normalcolor");
- ThemeHelper.SetTheme("luna", "normalcolor");
+ //ThemeHelper.SetTheme("luna", "normalcolor");
+
+ if (args.Length >= 2)
+ {
+ string theme = args[1], color = null;
+
+ if (args.Length >= 3)
+ color = args[2];
+
+ ThemeHelper.SetTheme(theme, color);
+ }
}
/// <summary>
View
@@ -18,6 +18,7 @@ public static class ThemeHelper
private const int ThemeChangedMessage = 0x31a;
private static readonly MethodInfo FilteredSystemThemeFilterMessageMethod = typeof(ThemeHelper).GetMethod("FilteredSystemThemeFilterMessage", StaticNonPublic);
+ private static readonly MethodInfo SystemThemeFilterMessageMethod = typeof(ThemeHelper).GetMethod("EmptySystemThemeFilterMessage", StaticNonPublic);
private static readonly Assembly PresentationFramework = Assembly.GetAssembly(typeof(Window));
@@ -73,19 +74,21 @@ private static void SetHwndNotifyHook(MethodInfo method)
{
var hookDelegate = Delegate.CreateDelegate(HwndWrapperHook, FilteredSystemThemeFilterMessageMethod);
+ // Make sure _hwndNotify and _hwndNotifyHook are set!
+ SystemResources_EnsureResourceChangeListener.Invoke(null, null);
+
// Note that because the HwndwWrapper uses a WeakReference list, we don't need
// to remove the old value. Simply killing the reference is good enough.
SystemResources_hwndNotifyHookField.SetValue(null, hookDelegate);
- // Make sure _hwndNotify is set!
- SystemResources_EnsureResourceChangeListener.Invoke(null, null);
-
// this does SystemResources._hwndNotify.Value.AddHook(hookDelegate)
var hwndNotify = SystemResources_hwndNotifyField.GetValue(null);
var hwndNotifyValue = SecurityCriticalDataClass_ValueProperty.GetValue(hwndNotify, null);
HwndWrapper_AddHookMethod.Invoke(hwndNotifyValue, new object[] { hookDelegate });
}
+ private static IntPtr EmptySystemThemeFilterMessage(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { return IntPtr.Zero; }
+
private static IntPtr InvokeSystemThemeFilterMessage(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
var args = new object[] { hwnd, msg, wParam, lParam, handled };

0 comments on commit c763c29

Please sign in to comment.