Split sample project up
This commit is contained in:
@@ -0,0 +1,200 @@
|
||||
using NotificationFlyout.Wpf.UI.Extensions;
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Windows;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Interop;
|
||||
|
||||
namespace NotificationFlyout.Wpf.UI.Helpers
|
||||
{
|
||||
public class NotificationIconHelper : IDisposable
|
||||
{
|
||||
private const int CallbackMessage = 0x400;
|
||||
private const uint IconVersion = 0x4;
|
||||
|
||||
private readonly object _lock = new();
|
||||
private readonly IntPtr _windowHandle;
|
||||
private bool _isDisposed;
|
||||
private NotifyIconData _notifyIconData;
|
||||
|
||||
private NotificationIconHelper(Window window)
|
||||
{
|
||||
_windowHandle = window.GetHandle();
|
||||
|
||||
var source = HwndSource.FromHwnd(_windowHandle);
|
||||
source.AddHook(new HwndSourceHook(WndProc));
|
||||
|
||||
CreateNotificationIcon();
|
||||
}
|
||||
|
||||
|
||||
~NotificationIconHelper()
|
||||
{
|
||||
Dispose(false);
|
||||
}
|
||||
|
||||
public event EventHandler<NotificationIconInvokedEventArgs> IconInvoked;
|
||||
|
||||
private enum NotifyIconBalloonType
|
||||
{
|
||||
None = 0x00,
|
||||
Info = 0x01,
|
||||
Warning = 0x02,
|
||||
Error = 0x03,
|
||||
User = 0x04,
|
||||
NoSound = 0x10,
|
||||
LargeIcon = 0x20,
|
||||
RespectQuietTime = 0x80
|
||||
}
|
||||
|
||||
private enum NotifyIconCommand : uint
|
||||
{
|
||||
Add = 0x0,
|
||||
Delete = 0x2,
|
||||
Modify = 0x1,
|
||||
SetVersion = 0x4
|
||||
}
|
||||
|
||||
[Flags]
|
||||
private enum NotifyIconDataMember : uint
|
||||
{
|
||||
Message = 0x01,
|
||||
Icon = 0x02,
|
||||
Tip = 0x04,
|
||||
State = 0x08,
|
||||
Info = 0x10,
|
||||
Realtime = 0x40,
|
||||
UseLegacyToolTips = 0x80
|
||||
}
|
||||
|
||||
private enum NotifyIconState : uint
|
||||
{
|
||||
Visible = 0x00,
|
||||
Hidden = 0x01
|
||||
}
|
||||
|
||||
public static NotificationIconHelper Create(Window window)
|
||||
{
|
||||
return new NotificationIconHelper(window);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
public void SetIcon(IntPtr iconHandle)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
_notifyIconData.IconHandle = iconHandle;
|
||||
WriteNotifyIconData(NotifyIconCommand.Modify, NotifyIconDataMember.Icon);
|
||||
}
|
||||
}
|
||||
|
||||
[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);
|
||||
|
||||
private void CreateNotificationIcon()
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
_notifyIconData = new NotifyIconData();
|
||||
|
||||
_notifyIconData.cbSize = (uint)Marshal.SizeOf(_notifyIconData);
|
||||
_notifyIconData.WindowHandle = _windowHandle;
|
||||
_notifyIconData.TaskbarIconId = 0x0;
|
||||
_notifyIconData.CallbackMessageId = CallbackMessage;
|
||||
_notifyIconData.VersionOrTimeout = IconVersion;
|
||||
|
||||
_notifyIconData.IconHandle = IntPtr.Zero;
|
||||
|
||||
_notifyIconData.IconState = NotifyIconState.Hidden;
|
||||
_notifyIconData.StateMask = NotifyIconState.Hidden;
|
||||
|
||||
WriteNotifyIconData(NotifyIconCommand.Add, NotifyIconDataMember.Message | NotifyIconDataMember.Icon | NotifyIconDataMember.Tip);
|
||||
}
|
||||
}
|
||||
|
||||
private void Dispose(bool disposing)
|
||||
{
|
||||
if (_isDisposed || !disposing) return;
|
||||
|
||||
lock (_lock)
|
||||
{
|
||||
_isDisposed = true;
|
||||
RemoveNotificationIcon();
|
||||
}
|
||||
}
|
||||
|
||||
private void InvokeIconInvoked(MouseButton mouseButton)
|
||||
{
|
||||
IconInvoked?.Invoke(this, new NotificationIconInvokedEventArgs { MouseButton = mouseButton });
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
switch ((uint)lParam)
|
||||
{
|
||||
case (uint)WndProcMessages.WM_LBUTTONUP:
|
||||
InvokeIconInvoked(MouseButton.Left);
|
||||
break;
|
||||
case (uint)WndProcMessages.WM_MBUTTONUP:
|
||||
InvokeIconInvoked(MouseButton.Middle);
|
||||
break;
|
||||
case (uint)WndProcMessages.WM_RBUTTONUP:
|
||||
InvokeIconInvoked(MouseButton.Right);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return DefWindowProcW(hwnd, (uint)msg, wParam, (lParam));
|
||||
}
|
||||
|
||||
private void WriteNotifyIconData(NotifyIconCommand command, NotifyIconDataMember flags)
|
||||
{
|
||||
_notifyIconData.ValidMembers = flags;
|
||||
lock (_lock)
|
||||
{
|
||||
Shell_NotifyIcon(command, ref _notifyIconData);
|
||||
}
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
|
||||
private struct NotifyIconData
|
||||
{
|
||||
public uint cbSize;
|
||||
public IntPtr WindowHandle;
|
||||
public uint TaskbarIconId;
|
||||
public NotifyIconDataMember ValidMembers;
|
||||
public uint CallbackMessageId;
|
||||
public IntPtr IconHandle;
|
||||
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
|
||||
public string ToolTipText;
|
||||
|
||||
public NotifyIconState IconState;
|
||||
public NotifyIconState StateMask;
|
||||
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
|
||||
public string BalloonText;
|
||||
|
||||
public uint VersionOrTimeout;
|
||||
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
|
||||
public string BalloonTitle;
|
||||
|
||||
public NotifyIconBalloonType BalloonFlags;
|
||||
public Guid TaskbarIconGuid;
|
||||
public IntPtr CustomBalloonIconHandle;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
using System;
|
||||
using System.Windows.Input;
|
||||
|
||||
namespace NotificationFlyout.Wpf.UI.Helpers
|
||||
{
|
||||
public class NotificationIconInvokedEventArgs : EventArgs
|
||||
{
|
||||
public MouseButton MouseButton { get; internal set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
using Microsoft.Windows.Sdk;
|
||||
using System.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Windows;
|
||||
|
||||
namespace NotificationFlyout.Wpf.UI.Helpers
|
||||
{
|
||||
public static class SystemInformationHelper
|
||||
{
|
||||
private const int SM_CXSCREEN = 0;
|
||||
private const int SM_CYSCREEN = 1;
|
||||
private const int SPI_GETWORKAREA = 48;
|
||||
|
||||
public static Rect VirtualScreen => GetVirtualScreen();
|
||||
public static Rect WorkingArea => GetWorkingArea();
|
||||
|
||||
public static int GetCurrentDpi()
|
||||
{
|
||||
return (int)typeof(SystemParameters).GetProperty("Dpi", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null, null);
|
||||
}
|
||||
|
||||
public static double GetCurrentDpiScaleFactor()
|
||||
{
|
||||
return (double)GetCurrentDpi() / 96;
|
||||
}
|
||||
|
||||
private static Rect GetVirtualScreen()
|
||||
{
|
||||
var size = new Size(PInvoke.GetSystemMetrics(SM_CXSCREEN), PInvoke.GetSystemMetrics(SM_CYSCREEN));
|
||||
return new Rect(0, 0, size.Width, size.Height);
|
||||
}
|
||||
|
||||
private static Rect GetWorkingArea()
|
||||
{
|
||||
var rect = new RECT();
|
||||
|
||||
SystemParametersInfo(SPI_GETWORKAREA, 0, ref rect, 0);
|
||||
return new Rect(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
|
||||
}
|
||||
|
||||
[DllImport("user32.dll", CharSet = CharSet.Auto)]
|
||||
private static extern bool SystemParametersInfo(int nAction, int nParam, ref RECT rc, int nUpdate);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
using Microsoft.Win32;
|
||||
using NotificationFlyout.Wpf.UI.Extensions;
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Windows;
|
||||
using System.Windows.Interop;
|
||||
using Windows.UI.ViewManagement;
|
||||
|
||||
namespace NotificationFlyout.Wpf.UI.Helpers
|
||||
{
|
||||
public class SystemPersonalisationHelper
|
||||
{
|
||||
private readonly UISettings _settings = new();
|
||||
private readonly string PersonalizeKey = @"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize";
|
||||
private SystemTheme _currentTheme;
|
||||
private bool _isColorPrevalence;
|
||||
|
||||
private SystemPersonalisationHelper(Window window)
|
||||
{
|
||||
var source = HwndSource.FromHwnd(window.GetHandle());
|
||||
source.AddHook(new HwndSourceHook(WndProc));
|
||||
|
||||
_currentTheme = GetTheme();
|
||||
_isColorPrevalence = GetIsColorPrevalence();
|
||||
}
|
||||
|
||||
public event EventHandler<SystemPersonalisationChangedEventArgs> ThemeChanged;
|
||||
|
||||
public bool IsColorPrevalence => GetIsColorPrevalence();
|
||||
public SystemTheme Theme => GetTheme();
|
||||
|
||||
public static SystemPersonalisationHelper Create(Window window)
|
||||
{
|
||||
return new SystemPersonalisationHelper(window);
|
||||
}
|
||||
|
||||
[DllImport("user32.dll", SetLastError = true)]
|
||||
private static extern IntPtr DefWindowProcW(IntPtr handle, uint msg, IntPtr wParam, IntPtr lParam);
|
||||
|
||||
private bool GetIsColorPrevalence()
|
||||
{
|
||||
using var baseKey = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64);
|
||||
using var subKey = baseKey.OpenSubKey(PersonalizeKey);
|
||||
return subKey.GetValue<int>("ColorPrevalence", 0) > 0;
|
||||
}
|
||||
|
||||
private SystemTheme GetTheme()
|
||||
{
|
||||
var color = _settings.GetColorValue(UIColorType.Background).ToString();
|
||||
return color == "#FFFFFFFF" ? SystemTheme.Light : SystemTheme.Dark;
|
||||
}
|
||||
|
||||
private void RaiseThemeChangedEvent()
|
||||
{
|
||||
var theme = GetTheme();
|
||||
var isColorPrevalence = GetIsColorPrevalence();
|
||||
|
||||
if (theme != _currentTheme || _isColorPrevalence != isColorPrevalence)
|
||||
{
|
||||
_currentTheme = theme;
|
||||
_isColorPrevalence = isColorPrevalence;
|
||||
|
||||
ThemeChanged?.Invoke(this, new SystemPersonalisationChangedEventArgs(theme, isColorPrevalence));
|
||||
}
|
||||
}
|
||||
|
||||
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
|
||||
{
|
||||
if (msg == (int)WndProcMessages.WM_SETTINGCHANGE)
|
||||
{
|
||||
RaiseThemeChangedEvent();
|
||||
}
|
||||
|
||||
return DefWindowProcW(hwnd, (uint)msg, wParam, (lParam));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace NotificationFlyout.Wpf.UI.Helpers
|
||||
{
|
||||
public enum SystemTheme
|
||||
{
|
||||
Dark,
|
||||
Light,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
using Microsoft.Windows.Sdk;
|
||||
using NotificationFlyout.Wpf.UI.Extensions;
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Windows;
|
||||
using System.Windows.Interop;
|
||||
|
||||
namespace NotificationFlyout.Wpf.UI.Helpers
|
||||
{
|
||||
public class TaskbarHelper
|
||||
{
|
||||
private const string ShellTrayHandleName = "Shell_TrayWnd";
|
||||
private const int SPI_SETWORKAREA = 0x002F;
|
||||
|
||||
private readonly uint WM_TASKBARCREATED = PInvoke.RegisterWindowMessage("TaskbarCreated");
|
||||
|
||||
private TaskbarHelper(Window window)
|
||||
{
|
||||
var handle = window.GetHandle();
|
||||
|
||||
var source = HwndSource.FromHwnd(handle);
|
||||
source.AddHook(new HwndSourceHook(WndProc));
|
||||
}
|
||||
|
||||
public event EventHandler TaskbarChanged;
|
||||
|
||||
private enum AppBarEdge : uint
|
||||
{
|
||||
Left = 0,
|
||||
Top = 1,
|
||||
Right = 2,
|
||||
Bottom = 3
|
||||
}
|
||||
|
||||
private enum AppBarMessage : uint
|
||||
{
|
||||
New = 0x00000000,
|
||||
Remove = 0x00000001,
|
||||
QueryPos = 0x00000002,
|
||||
SetPos = 0x00000003,
|
||||
GetState = 0x00000004,
|
||||
GetTaskbarPos = 0x00000005,
|
||||
Activate = 0x00000006,
|
||||
GetAutoHideBar = 0x00000007,
|
||||
SetAutoHideBar = 0x00000008,
|
||||
WindowPosChanged = 0x00000009,
|
||||
SetState = 0x0000000A,
|
||||
}
|
||||
|
||||
public static TaskbarHelper Create(Window window)
|
||||
{
|
||||
return new TaskbarHelper(window);
|
||||
}
|
||||
|
||||
public TaskbarState GetCurrentState()
|
||||
{
|
||||
var handle = GetSystemTrayHandle();
|
||||
var state = new TaskbarState
|
||||
{
|
||||
Screen = Screen.FromHandle(handle)
|
||||
};
|
||||
|
||||
var appBarData = GetAppBarData(handle);
|
||||
GetAppBarPosition(ref appBarData);
|
||||
|
||||
state.Rect = appBarData.rect.ToRect();
|
||||
state.Position = (TaskbarPosition)appBarData.uEdge;
|
||||
|
||||
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
|
||||
{
|
||||
cbSize = (uint)Marshal.SizeOf(typeof(AppBarData)),
|
||||
hWnd = handle
|
||||
};
|
||||
}
|
||||
|
||||
private void GetAppBarPosition(ref AppBarData appBarData)
|
||||
{
|
||||
SHAppBarMessage(AppBarMessage.GetTaskbarPos, ref appBarData);
|
||||
}
|
||||
|
||||
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
|
||||
{
|
||||
if (msg == WM_TASKBARCREATED || msg == (int)WndProcMessages.WM_SETTINGCHANGE && (int)wParam == SPI_SETWORKAREA)
|
||||
{
|
||||
TaskbarChanged?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
namespace NotificationFlyout.Wpf.UI
|
||||
{
|
||||
|
||||
public enum TaskbarPosition
|
||||
{
|
||||
Left = 0,
|
||||
Top = 1,
|
||||
Right = 2,
|
||||
Bottom = 3
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using System.Windows;
|
||||
|
||||
namespace NotificationFlyout.Wpf.UI
|
||||
{
|
||||
public struct TaskbarState
|
||||
{
|
||||
public TaskbarPosition Position;
|
||||
public Rect Rect;
|
||||
public Screen Screen;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
|
||||
namespace NotificationFlyout.Wpf.UI.Helpers
|
||||
{
|
||||
public class SystemPersonalisationChangedEventArgs : EventArgs
|
||||
{
|
||||
internal SystemPersonalisationChangedEventArgs(SystemTheme theme, bool isColorPrevalence)
|
||||
{
|
||||
Theme = theme;
|
||||
IsColorPrevalence = isColorPrevalence;
|
||||
}
|
||||
|
||||
public SystemTheme Theme { get; private set; }
|
||||
|
||||
public bool IsColorPrevalence { get; private set; }
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using Microsoft.Windows.Sdk;
|
||||
using System;
|
||||
|
||||
namespace NotificationFlyout.Wpf.UI.Helpers
|
||||
{
|
||||
public class WindowHelper
|
||||
{
|
||||
public static IntPtr GetHandle(string windowName)
|
||||
{
|
||||
return PInvoke.FindWindow(windowName, null);
|
||||
}
|
||||
|
||||
public static uint GetDpi(IntPtr handle)
|
||||
{
|
||||
return PInvoke.GetDpiForWindow((HWND)handle);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
namespace NotificationFlyout.Wpf.UI.Helpers
|
||||
{
|
||||
internal enum WndProcMessages
|
||||
{
|
||||
WM_LBUTTONUP = 0x0202,
|
||||
WM_MBUTTONUP = 0x0208,
|
||||
WM_RBUTTONUP = 0x0205,
|
||||
WM_MOUSEMOVE = 0x0200,
|
||||
WM_SETTINGCHANGE = 0x001A,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user