Fixed stackoverflow...

This commit is contained in:
Daniel Clark
2021-02-05 21:32:32 +00:00
parent b411f82243
commit 2be7a1128c
9 changed files with 61 additions and 49 deletions
@@ -1,12 +0,0 @@
using Microsoft.Win32;
namespace NotificationFlyout.Wpf.UI.Extensions
{
public static class RegistryKeyExtensions
{
public static T GetValue<T>(this RegistryKey self, string valueName, T defaultValue)
{
return self.GetValue(valueName, defaultValue) is T t ? t : defaultValue;
}
}
}
@@ -1,5 +1,4 @@
using Microsoft.Windows.Sdk;
using NotificationFlyout.Wpf.UI.Extensions;
using NotificationFlyout.Wpf.UI.Extensions;
using System;
using System.Runtime.InteropServices;
using System.Windows;
@@ -97,6 +96,9 @@ namespace NotificationFlyout.Wpf.UI.Helpers
}
}
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr DefWindowProcW(IntPtr handle, uint msg, IntPtr wParam, IntPtr lParam);
[DllImport("shell32.dll", SetLastError = true)]
private static extern int Shell_NotifyIcon(NotifyIconCommand notifyCommand, ref NotifyIconData notifyIconData);
@@ -133,7 +135,6 @@ namespace NotificationFlyout.Wpf.UI.Helpers
}
private void RemoveNotificationIcon() => WriteNotifyIconData(NotifyIconCommand.Delete, NotifyIconDataMember.Message);
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == CallbackMessage)
@@ -164,8 +165,8 @@ namespace NotificationFlyout.Wpf.UI.Helpers
IconInvoked?.Invoke(this, new NotificationIconInvokedEventArgs { MouseButton = mouseButton });
}
}
return (IntPtr)(int)PInvoke.DefWindowProc((HWND)hwnd, (uint)msg, (WPARAM)(UIntPtr)(uint)wParam, (LPARAM)lParam);
return DefWindowProcW(hwnd, (uint)msg, wParam, (lParam));
}
private void WriteNotifyIconData(NotifyIconCommand command, NotifyIconDataMember flags)
@@ -0,0 +1,15 @@
using Microsoft.Win32;
namespace NotificationFlyout.Wpf.UI.Helpers
{
internal static class RegistryHelper
{
public static TValue GetDwordValue<TValue>(string key, string valueName)
{
using var baseKey = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64);
using var subKey = baseKey.OpenSubKey(key);
return (TValue)subKey.GetValue(valueName, 0);
}
}
}
@@ -1,30 +1,21 @@
using Microsoft.Win32;
using NotificationFlyout.Wpf.UI.Extensions;
using NotificationFlyout.Wpf.UI.Extensions;
using System;
namespace NotificationFlyout.Wpf.UI.Helpers
{
public static class SystemSettingsHelper
{
private static readonly string PersonalizeKey = @"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize";
public static SystemTheme DefaultSystemTheme => GetDefaultSystemTheme();
public static SystemTheme DefaultSystemTheme => GetDefaultSystemTheme();
private static SystemTheme GetDefaultSystemTheme()
{
return Environment.OSVersion.IsGreaterThan(OperatingSystemVersion.Windows10_1809) &&
ReadDword(PersonalizeKey, "SystemUsesLightTheme")
? SystemTheme.Light
: SystemTheme.Dark;
return Environment.OSVersion.IsGreaterThan(OperatingSystemVersion.Windows10_1809) && DoesSystemUsesLightTheme() ? SystemTheme.Light : SystemTheme.Dark;
}
private static bool ReadDword(string key, string valueName, int defaultValue = 0)
private static bool DoesSystemUsesLightTheme()
{
using var baseKey = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64);
using var subKey = baseKey.OpenSubKey(key);
return subKey.GetValue<int>(valueName, defaultValue) > 0;
var personalizeKey = @"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize";
return RegistryHelper.GetDwordValue<int>(personalizeKey, "SystemUsesLightTheme") > 0;
}
public static bool IsTransparencyEnabled => ReadDword(PersonalizeKey, "EnableTransparency");
}
}
@@ -3,6 +3,6 @@
public enum SystemTheme
{
Dark,
Light
Light,
}
}
@@ -72,11 +72,17 @@ namespace NotificationFlyout.Wpf.UI.Helpers
return state;
}
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr DefWindowProcW(IntPtr handle, uint msg, IntPtr wParam, IntPtr lParam);
private static IntPtr GetSystemTrayHandle()
{
return WindowHelper.GetHandle(ShellTrayHandleName);
}
[DllImport("shell32.dll", SetLastError = true)]
private static extern IntPtr SHAppBarMessage(AppBarMessage dwMessage, ref AppBarData pData);
private AppBarData GetAppBarData(IntPtr handle)
{
return new AppBarData
@@ -86,20 +92,6 @@ namespace NotificationFlyout.Wpf.UI.Helpers
};
}
[StructLayout(LayoutKind.Sequential)]
private struct AppBarData
{
public uint cbSize;
public IntPtr hWnd;
public uint uCallbackMessage;
public AppBarEdge uEdge;
public RECT rect;
public int lParam;
}
[DllImport("shell32.dll", SetLastError = true)]
private static extern IntPtr SHAppBarMessage(AppBarMessage dwMessage, ref AppBarData pData);
private void GetAppBarPosition(ref AppBarData appBarData)
{
SHAppBarMessage(AppBarMessage.GetTaskbarPos, ref appBarData);
@@ -112,7 +104,18 @@ namespace NotificationFlyout.Wpf.UI.Helpers
TaskbarChanged?.Invoke(this, EventArgs.Empty);
}
return (IntPtr)(int)PInvoke.DefWindowProc((HWND)hwnd, (uint)msg, (WPARAM)(UIntPtr)(uint)wParam, (LPARAM)lParam);
return DefWindowProcW(hwnd, (uint)msg, wParam, (lParam));
}
[StructLayout(LayoutKind.Sequential)]
private struct AppBarData
{
public uint cbSize;
public IntPtr hWnd;
public uint uCallbackMessage;
public AppBarEdge uEdge;
public RECT rect;
public int lParam;
}
}
}