accidental blowout of the project files
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
using Microsoft.Windows.Sdk;
|
||||
|
||||
namespace TheXamlGuy.NotificationFlyout.Common.Extensions
|
||||
{
|
||||
internal class ExecutionMode
|
||||
{
|
||||
internal static bool IsRunningWithIdentity()
|
||||
{
|
||||
uint packageNameLength = 0;
|
||||
int result = PInvoke.GetCurrentPackageFullName(ref packageNameLength, "1024");
|
||||
|
||||
return result != 15700;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
using Microsoft.Windows.Sdk;
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using Windows.UI.Xaml.Media;
|
||||
using Windows.UI.Xaml.Media.Imaging;
|
||||
|
||||
namespace TheXamlGuy.NotificationFlyout.Common.Extensions
|
||||
{
|
||||
public static class ImageSourceExtensions
|
||||
{
|
||||
public static async Task<Icon> ConvertToIconAsync(this ImageSource imageSource, uint dpi)
|
||||
{
|
||||
var bitmapImage = (BitmapImage)imageSource;
|
||||
if (!ExecutionMode.IsRunningWithIdentity())
|
||||
{
|
||||
var uri = $"{AppDomain.CurrentDomain.BaseDirectory}{bitmapImage.UriSource}".Replace("ms-appx:///", "").Replace("/", "\\");
|
||||
|
||||
using var stream = File.OpenRead(uri);
|
||||
return ExtractIcon(dpi, stream);
|
||||
}
|
||||
else
|
||||
{
|
||||
var storageFile = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(bitmapImage.UriSource);
|
||||
|
||||
using var stream = await storageFile.OpenStreamForReadAsync();
|
||||
return ExtractIcon(dpi, stream);
|
||||
}
|
||||
}
|
||||
|
||||
private static Icon ExtractIcon(uint dpi, Stream stream)
|
||||
{
|
||||
var bitmap = (Bitmap)Image.FromStream(stream);
|
||||
var icon = Icon.FromHandle(bitmap.GetHicon());
|
||||
|
||||
return new Icon(icon, new Size(PInvoke.GetSystemMetricsForDpi((int)SystemMetricFlag.SM_CXICON, dpi), PInvoke.GetSystemMetricsForDpi((int)SystemMetricFlag.SM_CYICON, dpi)));
|
||||
}
|
||||
|
||||
private enum SystemMetricFlag : int
|
||||
{
|
||||
SM_CXICON = 11,
|
||||
SM_CYICON = 12,
|
||||
SM_CXSMICON = 49,
|
||||
SM_CYSMICON = 50
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
using System;
|
||||
|
||||
namespace TheXamlGuy.NotificationFlyout.Common.Extensions
|
||||
{
|
||||
public static class OperatingSystemExtensions
|
||||
{
|
||||
public static bool IsGreaterThan(this OperatingSystem operatingSystem, OperatingSystemVersion version) => operatingSystem.Version.Build > (int)version;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
|
||||
namespace TheXamlGuy.NotificationFlyout.Common.Extensions
|
||||
{
|
||||
public enum OperatingSystemVersion : int
|
||||
{
|
||||
Windows10 = 10240,
|
||||
Windows10_1511 = 10586,
|
||||
Windows10_1607 = 14393,
|
||||
Windows10_1703 = 15063,
|
||||
Windows10_1709 = 16299,
|
||||
Windows10_1803 = 17134,
|
||||
Windows10_1809 = 17763,
|
||||
Windows10_1903 = 18362
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using Microsoft.Windows.Sdk;
|
||||
using Windows.Foundation;
|
||||
|
||||
namespace TheXamlGuy.NotificationFlyout.Common.Extensions
|
||||
{
|
||||
internal static class RECTExtensions
|
||||
{
|
||||
internal static Rect ToRect(this RECT rect)
|
||||
{
|
||||
if (rect.right - rect.left < 0 || rect.bottom - rect.top < 0) return new Rect(rect.left, rect.top, 0, 0);
|
||||
return new Rect(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using Microsoft.Win32;
|
||||
|
||||
namespace TheXamlGuy.NotificationFlyout.Common.Extensions
|
||||
{
|
||||
public static class RegistryKeyExtensions
|
||||
{
|
||||
public static T GetValue<T>(this RegistryKey key, string valueName, T defaultValue = default)
|
||||
{
|
||||
return string.IsNullOrWhiteSpace(valueName) ? defaultValue : key.GetValue(valueName, defaultValue) is T value ? value : defaultValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using Microsoft.Windows.Sdk;
|
||||
|
||||
namespace TheXamlGuy.NotificationFlyout.Common.Helpers
|
||||
{
|
||||
internal class CursorHelper
|
||||
{
|
||||
public static POINT GetPhysicalCursorPos()
|
||||
{
|
||||
PInvoke.GetPhysicalCursorPos(out var point);
|
||||
return point;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace TheXamlGuy.NotificationFlyout.Common.Helpers
|
||||
{
|
||||
public partial class NotificationIconHelper
|
||||
{
|
||||
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
|
||||
}
|
||||
|
||||
[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);
|
||||
|
||||
[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,110 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace TheXamlGuy.NotificationFlyout.Common.Helpers
|
||||
{
|
||||
public partial class NotificationIconHelper : IDisposable, IWndProcHandler
|
||||
{
|
||||
private const int CallbackMessage = 0x400;
|
||||
private const uint IconVersion = 0x4;
|
||||
|
||||
private readonly object _lock = new();
|
||||
private bool _isDisposed;
|
||||
private NotifyIconData _notifyIconData;
|
||||
|
||||
private NotificationIconHelper()
|
||||
{
|
||||
WndProcHandlerSubscriber.Current.Subscribe(this);
|
||||
CreateNotificationIcon();
|
||||
}
|
||||
|
||||
~NotificationIconHelper()
|
||||
{
|
||||
Dispose(false);
|
||||
}
|
||||
|
||||
public event EventHandler<NotificationIconInvokedEventArgs> IconInvoked;
|
||||
|
||||
public static NotificationIconHelper Create() => new();
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
public void Handle(uint message, IntPtr wParam, IntPtr lParam)
|
||||
{
|
||||
if (message == CallbackMessage)
|
||||
{
|
||||
switch ((uint)lParam)
|
||||
{
|
||||
case (uint)WndProcMessages.WM_LBUTTONUP:
|
||||
InvokeIconInvoked(PointerButton.Left);
|
||||
break;
|
||||
|
||||
case (uint)WndProcMessages.WM_MBUTTONUP:
|
||||
InvokeIconInvoked(PointerButton.Middle);
|
||||
break;
|
||||
|
||||
case (uint)WndProcMessages.WM_RBUTTONUP:
|
||||
InvokeIconInvoked(PointerButton.Right);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void SetIcon(IntPtr iconHandle)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
_notifyIconData.IconHandle = iconHandle;
|
||||
WriteNotifyIconData(NotifyIconCommand.Modify, NotifyIconDataMember.Icon);
|
||||
}
|
||||
}
|
||||
|
||||
private void CreateNotificationIcon()
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
_notifyIconData = new NotifyIconData();
|
||||
|
||||
_notifyIconData.cbSize = (uint)Marshal.SizeOf(_notifyIconData);
|
||||
_notifyIconData.WindowHandle = WndProcListener.Current.Handle;
|
||||
_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(PointerButton pointerButton) => IconInvoked?.Invoke(this, new NotificationIconInvokedEventArgs(pointerButton));
|
||||
|
||||
private void RemoveNotificationIcon() => WriteNotifyIconData(NotifyIconCommand.Delete, NotifyIconDataMember.Message);
|
||||
|
||||
private void WriteNotifyIconData(NotifyIconCommand command, NotifyIconDataMember flags)
|
||||
{
|
||||
_notifyIconData.ValidMembers = flags;
|
||||
lock (_lock)
|
||||
{
|
||||
Shell_NotifyIcon(command, ref _notifyIconData);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using System;
|
||||
|
||||
namespace TheXamlGuy.NotificationFlyout.Common.Helpers
|
||||
{
|
||||
public class NotificationIconInvokedEventArgs : EventArgs
|
||||
{
|
||||
internal NotificationIconInvokedEventArgs(PointerButton pointerButton) => PointerButton = pointerButton;
|
||||
|
||||
public PointerButton PointerButton { get; private set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
|
||||
namespace TheXamlGuy.NotificationFlyout.Common.Helpers
|
||||
{
|
||||
public enum PointerButton
|
||||
{
|
||||
Left = 0,
|
||||
Middle = 1,
|
||||
Right = 2,
|
||||
XButton1 = 3,
|
||||
XButton2 = 4
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
using Microsoft.Windows.Sdk;
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using Windows.Foundation;
|
||||
|
||||
namespace TheXamlGuy.NotificationFlyout.Common.Helpers
|
||||
{
|
||||
public class Screen
|
||||
{
|
||||
private const int CCHDEVICENAME = 32;
|
||||
private const int PRIMARY_MONITOR = unchecked((int)0xBAADF00D);
|
||||
private const int SM_CMONITORS = 80;
|
||||
private static readonly bool _multiMonitorSupport;
|
||||
|
||||
private readonly IntPtr _monitorHandle;
|
||||
|
||||
static Screen()
|
||||
{
|
||||
_multiMonitorSupport = PInvoke.GetSystemMetrics(SM_CMONITORS) != 0;
|
||||
}
|
||||
|
||||
internal Screen(IntPtr monitorHandle)
|
||||
{
|
||||
if (!_multiMonitorSupport || monitorHandle == (IntPtr)PRIMARY_MONITOR)
|
||||
{
|
||||
Bounds = SystemInformationHelper.VirtualScreen;
|
||||
Primary = true;
|
||||
DeviceName = "DISPLAY";
|
||||
}
|
||||
else
|
||||
{
|
||||
var monitorData = GetMonitorData(monitorHandle);
|
||||
|
||||
Bounds = new Rect(monitorData.MonitorRect.left, monitorData.MonitorRect.top, monitorData.MonitorRect.right - monitorData.MonitorRect.left, monitorData.MonitorRect.bottom - monitorData.MonitorRect.top);
|
||||
Primary = (monitorData.Flags & (int)MonitorFlag.MONITOR_DEFAULTTOPRIMARY) != 0;
|
||||
DeviceName = monitorData.DeviceName;
|
||||
}
|
||||
|
||||
_monitorHandle = monitorHandle;
|
||||
}
|
||||
|
||||
private enum MonitorFlag : uint
|
||||
{
|
||||
MONITOR_DEFAULTTONULL = 0,
|
||||
MONITOR_DEFAULTTOPRIMARY = 1,
|
||||
MONITOR_DEFAULTTONEAREST = 2
|
||||
}
|
||||
|
||||
public Rect Bounds { get; }
|
||||
|
||||
public string DeviceName { get; }
|
||||
|
||||
public bool Primary { get; }
|
||||
|
||||
public Rect WorkingArea => GetWorkingArea();
|
||||
|
||||
public static Screen FromHandle(IntPtr handle)
|
||||
{
|
||||
return _multiMonitorSupport ? new Screen(PInvoke.MonitorFromWindow((HWND)handle, (uint)MonitorFlag.MONITOR_DEFAULTTONEAREST)) : new Screen((IntPtr)PRIMARY_MONITOR);
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (obj is not Screen monitor) return false;
|
||||
return _monitorHandle == monitor._monitorHandle;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return (int)_monitorHandle;
|
||||
}
|
||||
|
||||
[DllImport("user32.dll", EntryPoint = "GetMonitorInfo", CharSet = CharSet.Auto, SetLastError = true)]
|
||||
private static extern bool GetMonitorInfoEx(IntPtr hMonitor, ref MonitorData lpmi);
|
||||
|
||||
private MonitorData GetMonitorData(IntPtr monitorHandle)
|
||||
{
|
||||
var monitorData = new MonitorData();
|
||||
monitorData.Size = Marshal.SizeOf(monitorData);
|
||||
GetMonitorInfoEx(monitorHandle, ref monitorData);
|
||||
|
||||
return monitorData;
|
||||
}
|
||||
|
||||
private Rect GetWorkingArea()
|
||||
{
|
||||
if (!_multiMonitorSupport || _monitorHandle == (IntPtr)PRIMARY_MONITOR)
|
||||
{
|
||||
return SystemInformationHelper.WorkingArea;
|
||||
}
|
||||
|
||||
var monitorData = GetMonitorData(_monitorHandle);
|
||||
return new Rect(monitorData.WorkAreaRect.left, monitorData.WorkAreaRect.top, monitorData.WorkAreaRect.right - monitorData.WorkAreaRect.left, monitorData.WorkAreaRect.bottom - monitorData.WorkAreaRect.top);
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
|
||||
private struct MonitorData
|
||||
{
|
||||
public int Size;
|
||||
public RECT MonitorRect;
|
||||
public RECT WorkAreaRect;
|
||||
public uint Flags;
|
||||
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHDEVICENAME)]
|
||||
public string DeviceName;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
using Microsoft.Windows.Sdk;
|
||||
using System.Runtime.InteropServices;
|
||||
using Windows.Foundation;
|
||||
|
||||
namespace TheXamlGuy.NotificationFlyout.Common.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();
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
using System;
|
||||
|
||||
namespace TheXamlGuy.NotificationFlyout.Common.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,66 @@
|
||||
using Microsoft.Win32;
|
||||
using System;
|
||||
using TheXamlGuy.NotificationFlyout.Common.Extensions;
|
||||
using Windows.UI.ViewManagement;
|
||||
|
||||
namespace TheXamlGuy.NotificationFlyout.Common.Helpers
|
||||
{
|
||||
public class SystemPersonalisationHelper : IWndProcHandler
|
||||
{
|
||||
private static readonly Lazy<SystemPersonalisationHelper> _current = new(() => new SystemPersonalisationHelper());
|
||||
|
||||
private readonly UISettings _settings = new();
|
||||
private readonly string PersonalizeKey = @"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize";
|
||||
private SystemTheme _currentTheme;
|
||||
private bool _isColorPrevalence;
|
||||
|
||||
private SystemPersonalisationHelper()
|
||||
{
|
||||
WndProcHandlerSubscriber.Current.Subscribe(this);
|
||||
|
||||
_currentTheme = GetTheme();
|
||||
_isColorPrevalence = GetIsColorPrevalence();
|
||||
}
|
||||
|
||||
public event EventHandler<SystemPersonalisationChangedEventArgs> ThemeChanged;
|
||||
|
||||
public static SystemPersonalisationHelper Current => _current.Value;
|
||||
public bool IsColorPrevalence => GetIsColorPrevalence();
|
||||
public SystemTheme Theme => GetTheme();
|
||||
|
||||
public void Handle(uint message, IntPtr wParam, IntPtr lParam)
|
||||
{
|
||||
if (message == (int)WndProcMessages.WM_SETTINGCHANGE)
|
||||
{
|
||||
RaiseThemeChangedEvent();
|
||||
}
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
|
||||
namespace TheXamlGuy.NotificationFlyout.Common.Helpers
|
||||
{
|
||||
public enum SystemTheme
|
||||
{
|
||||
Dark,
|
||||
Light,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
using Microsoft.Windows.Sdk;
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace TheXamlGuy.NotificationFlyout.Common.Helpers
|
||||
{
|
||||
public partial class TaskbarHelper
|
||||
{
|
||||
private const string ShellTrayHandleName = "Shell_TrayWnd";
|
||||
private const int SPI_SETWORKAREA = 0x002F;
|
||||
private readonly uint WM_TASKBARCREATED = PInvoke.RegisterWindowMessage("TaskbarCreated");
|
||||
|
||||
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,
|
||||
}
|
||||
|
||||
[DllImport("user32.dll", SetLastError = true)]
|
||||
private static extern IntPtr DefWindowProcW(IntPtr handle, uint msg, IntPtr wParam, IntPtr lParam);
|
||||
|
||||
private static IntPtr GetSystemTrayHandle() => WindowHelper.GetHandle(ShellTrayHandleName);
|
||||
|
||||
[DllImport("shell32.dll", SetLastError = true)]
|
||||
private static extern IntPtr SHAppBarMessage(AppBarMessage dwMessage, ref AppBarData pData);
|
||||
|
||||
[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,51 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using TheXamlGuy.NotificationFlyout.Common.Extensions;
|
||||
|
||||
namespace TheXamlGuy.NotificationFlyout.Common.Helpers
|
||||
{
|
||||
public partial class TaskbarHelper : IWndProcHandler
|
||||
{
|
||||
private TaskbarHelper() => WndProcHandlerSubscriber.Current.Subscribe(this);
|
||||
|
||||
public event EventHandler TaskbarChanged;
|
||||
|
||||
public static TaskbarHelper Create() => new();
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public void Handle(uint message, IntPtr wParam, IntPtr lParam)
|
||||
{
|
||||
if (message == WM_TASKBARCREATED || message == (int)WndProcMessages.WM_SETTINGCHANGE && (int)wParam == SPI_SETWORKAREA)
|
||||
{
|
||||
TaskbarChanged?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
|
||||
namespace TheXamlGuy.NotificationFlyout.Common.Helpers
|
||||
{
|
||||
public enum TaskbarPosition
|
||||
{
|
||||
Left = 0,
|
||||
Top = 1,
|
||||
Right = 2,
|
||||
Bottom = 3
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using Windows.Foundation;
|
||||
|
||||
namespace TheXamlGuy.NotificationFlyout.Common.Helpers
|
||||
{
|
||||
public struct TaskbarState
|
||||
{
|
||||
public TaskbarPosition Position;
|
||||
public Rect Rect;
|
||||
public Screen Screen;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using Microsoft.Windows.Sdk;
|
||||
using System;
|
||||
|
||||
namespace TheXamlGuy.NotificationFlyout.Common.Helpers
|
||||
{
|
||||
public class WindowHelper
|
||||
{
|
||||
public static IntPtr GetHandle(string windowName) => PInvoke.FindWindow(windowName, null);
|
||||
|
||||
public static uint GetDpi(IntPtr handle) => PInvoke.GetDpiForWindow((HWND)handle);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace TheXamlGuy.NotificationFlyout.Common.Helpers
|
||||
{
|
||||
internal partial class WndProcHelper
|
||||
{
|
||||
[DllImport("user32.dll", SetLastError = true)]
|
||||
private static extern IntPtr CreateWindowExW(uint dwExStyle, [MarshalAs(UnmanagedType.LPWStr)] string lpClassName, [MarshalAs(UnmanagedType.LPWStr)] string lpWindowName, uint dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lpParam);
|
||||
|
||||
[DllImport("user32.dll", SetLastError = true)]
|
||||
private static extern IntPtr DefWindowProcW(IntPtr handle, uint msg, IntPtr wParam, IntPtr lParam);
|
||||
|
||||
[DllImport("user32.dll", SetLastError = true)]
|
||||
private static extern bool DestroyWindow(IntPtr handle);
|
||||
|
||||
[DllImport("user32.dll", SetLastError = true)]
|
||||
private static extern ushort RegisterClassW([In] ref WNDCLASSW lpWndClass);
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
private struct WNDCLASSW
|
||||
{
|
||||
public uint style;
|
||||
public WndProcHandler lpfnWndProc;
|
||||
public int cbClsExtra;
|
||||
public int cbWndExtra;
|
||||
public IntPtr hInstance;
|
||||
public IntPtr hIcon;
|
||||
public IntPtr hCursor;
|
||||
public IntPtr hbrBackground;
|
||||
[MarshalAs(UnmanagedType.LPWStr)] public string lpszMenuName;
|
||||
[MarshalAs(UnmanagedType.LPWStr)] public string lpszClassName;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
using System;
|
||||
|
||||
namespace TheXamlGuy.NotificationFlyout.Common.Helpers
|
||||
{
|
||||
internal partial class WndProcHelper : IDisposable
|
||||
{
|
||||
private WndProcHandler _handler;
|
||||
|
||||
private WndProcHelper() => CreateWndProcWindow();
|
||||
|
||||
private delegate IntPtr WndProcHandler(IntPtr hwnd, uint uMsg, IntPtr wparam, IntPtr lparam);
|
||||
|
||||
public event EventHandler<WndProcHelperMessageEventArgs> WndProcMessage;
|
||||
|
||||
public IntPtr Handle { get; private set; }
|
||||
|
||||
public static WndProcHelper Create() => new();
|
||||
|
||||
public void Dispose() => DestroyWindow(Handle);
|
||||
|
||||
private void CreateWndProcWindow()
|
||||
{
|
||||
var windowName = Guid.NewGuid().ToString();
|
||||
_handler = WndProc;
|
||||
|
||||
WNDCLASSW wndProcWindow;
|
||||
|
||||
wndProcWindow.style = 0;
|
||||
wndProcWindow.lpfnWndProc = _handler;
|
||||
wndProcWindow.cbClsExtra = 0;
|
||||
wndProcWindow.cbWndExtra = 0;
|
||||
wndProcWindow.hInstance = IntPtr.Zero;
|
||||
wndProcWindow.hIcon = IntPtr.Zero;
|
||||
wndProcWindow.hCursor = IntPtr.Zero;
|
||||
wndProcWindow.hbrBackground = IntPtr.Zero;
|
||||
wndProcWindow.lpszMenuName = "";
|
||||
wndProcWindow.lpszClassName = windowName;
|
||||
|
||||
RegisterClassW(ref wndProcWindow);
|
||||
|
||||
Handle = CreateWindowExW(0, windowName, "", 0, 0, 0, 1, 1, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
|
||||
}
|
||||
|
||||
private IntPtr WndProc(IntPtr handle, uint message, IntPtr wParam, IntPtr lParam)
|
||||
{
|
||||
WndProcMessage?.Invoke(this, new WndProcHelperMessageEventArgs(message, wParam, lParam));
|
||||
return DefWindowProcW(handle, message, wParam, lParam);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
|
||||
namespace TheXamlGuy.NotificationFlyout.Common.Helpers
|
||||
{
|
||||
internal class WndProcHelperMessageEventArgs : EventArgs
|
||||
{
|
||||
public WndProcHelperMessageEventArgs(uint message, IntPtr wParam, IntPtr lParam)
|
||||
{
|
||||
Message = message;
|
||||
WParam = wParam;
|
||||
LParam = lParam;
|
||||
}
|
||||
|
||||
public IntPtr LParam { get; private set; }
|
||||
public uint Message { get; private set; }
|
||||
public IntPtr WParam { get; private set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
|
||||
namespace TheXamlGuy.NotificationFlyout.Common.Helpers
|
||||
{
|
||||
internal enum WndProcMessages
|
||||
{
|
||||
WM_LBUTTONUP = 0x0202,
|
||||
WM_MBUTTONUP = 0x0208,
|
||||
WM_RBUTTONUP = 0x0205,
|
||||
WM_MOUSEMOVE = 0x0200,
|
||||
WM_SETTINGCHANGE = 0x001A,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
Shell_NotifyIcon
|
||||
DefWindowProcW
|
||||
MonitorFromWindow
|
||||
RegisterWindowMessage
|
||||
SHAppBarMessage
|
||||
GetWindowLong
|
||||
GetDpiForWindow
|
||||
FindWindow
|
||||
GetPhysicalCursorPos
|
||||
GetSystemMetricsForDpi
|
||||
GetCurrentPackageFullName
|
||||
GetSystemMetrics
|
||||
SetWindowLong
|
||||
SetWindowPos
|
||||
@@ -0,0 +1,5 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
[assembly: InternalsVisibleTo("TheXamlGuy.NotificationFlyout.Wpf.UI")]
|
||||
[assembly: InternalsVisibleTo("TheXamlGuy.NotificationFlyout.Wpf.UI.Extensions")]
|
||||
[assembly: InternalsVisibleTo("TheXamlGuy.NotificationFlyout.Wpf.UI.Controls")]
|
||||
@@ -0,0 +1,25 @@
|
||||
<Project Sdk="MSBuild.Sdk.Extras">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>netcoreapp3.1</TargetFrameworks>
|
||||
<LangVersion>9.0</LangVersion>
|
||||
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
|
||||
<Company>TheXamlGuy</Company>
|
||||
<Authors>TheXamlGuy</Authors>
|
||||
<Product>NotificationFlyout</Product>
|
||||
<Version>1.0.0</Version>
|
||||
<Platforms>AnyCPU;x64</Platforms>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Windows.SDK.Contracts" Version="10.0.19041.1" Condition="'$(TargetFramework)' == 'netcoreapp3.1'" />
|
||||
<PackageReference Include="Microsoft.Win32.Registry" Version="6.0.0-preview.1.21102.12" />
|
||||
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.1.319-beta">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="System.Drawing.Common" Version="6.0.0-preview.1.21102.12" />
|
||||
<PackageReference Include="System.Memory" Version="4.5.4" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,9 @@
|
||||
using System;
|
||||
|
||||
namespace TheXamlGuy.NotificationFlyout.Common.Helpers
|
||||
{
|
||||
public interface IWndProcHandler
|
||||
{
|
||||
void Handle(uint message, IntPtr wParam, IntPtr lParam);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace TheXamlGuy.NotificationFlyout.Common.Helpers
|
||||
{
|
||||
internal interface IWndProcHandlerCollection : IList<WndProcHandlerReference>
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
|
||||
namespace TheXamlGuy.NotificationFlyout.Common.Helpers
|
||||
{
|
||||
public interface IWndProcHandlerSubscriber
|
||||
{
|
||||
void Subscribe<TWndProcHandler>(TWndProcHandler handler) where TWndProcHandler : IWndProcHandler;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace TheXamlGuy.NotificationFlyout.Common.Helpers
|
||||
{
|
||||
internal class WndProcHandlerCollection : List<WndProcHandlerReference>, IWndProcHandlerCollection
|
||||
{
|
||||
private static readonly Lazy<WndProcHandlerCollection> _current = new(() => new WndProcHandlerCollection());
|
||||
public static WndProcHandlerCollection Current => _current.Value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using System;
|
||||
|
||||
namespace TheXamlGuy.NotificationFlyout.Common.Helpers
|
||||
{
|
||||
internal class WndProcHandlerReference
|
||||
{
|
||||
private readonly WeakReference _reference;
|
||||
|
||||
public WndProcHandlerReference(object handler) => _reference = new WeakReference(handler);
|
||||
|
||||
public bool IsDead => _reference.Target == null;
|
||||
|
||||
public void Handle(uint message, IntPtr wParam, IntPtr lParam)
|
||||
{
|
||||
if (_reference.Target == null) return;
|
||||
|
||||
var target = _reference.Target;
|
||||
if (target is IWndProcHandler handler)
|
||||
{
|
||||
handler.Handle(message, wParam, lParam);
|
||||
}
|
||||
}
|
||||
|
||||
public bool Matches(object instance) => _reference.Target == instance;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
namespace TheXamlGuy.NotificationFlyout.Common.Helpers
|
||||
{
|
||||
public class WndProcHandlerSubscriber : IWndProcHandlerSubscriber
|
||||
{
|
||||
private static readonly Lazy<WndProcHandlerSubscriber> _current = new(() => new WndProcHandlerSubscriber());
|
||||
public static WndProcHandlerSubscriber Current => _current.Value;
|
||||
|
||||
public void Subscribe<TWndProcHandler>(TWndProcHandler handler) where TWndProcHandler : IWndProcHandler
|
||||
{
|
||||
var handlers = WndProcHandlerCollection.Current;
|
||||
lock (handlers)
|
||||
{
|
||||
if (handlers.Any(x => x.Matches(handler))) return;
|
||||
handlers.Add(new WndProcHandlerReference(handler));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
namespace TheXamlGuy.NotificationFlyout.Common.Helpers
|
||||
{
|
||||
public class WndProcListener
|
||||
{
|
||||
private static readonly Lazy<WndProcListener> _current = new(() => new WndProcListener());
|
||||
|
||||
private readonly WndProcHelper _wndProcHelper;
|
||||
|
||||
private WndProcListener() => _wndProcHelper = WndProcHelper.Create();
|
||||
|
||||
public static WndProcListener Current => _current.Value;
|
||||
|
||||
public IntPtr Handle => _wndProcHelper.Handle;
|
||||
|
||||
public void Start()
|
||||
{
|
||||
_wndProcHelper.WndProcMessage -= OnWndProcMessage;
|
||||
_wndProcHelper.WndProcMessage += OnWndProcMessage;
|
||||
}
|
||||
|
||||
private void OnWndProcMessage(object sender, WndProcHelperMessageEventArgs args)
|
||||
{
|
||||
WndProcHandlerReference[] handlers;
|
||||
var subscribers = WndProcHandlerCollection.Current;
|
||||
|
||||
lock (subscribers)
|
||||
{
|
||||
handlers = subscribers.ToArray();
|
||||
}
|
||||
|
||||
foreach (var handler in handlers)
|
||||
{
|
||||
handler.Handle(args.Message, args.WParam, args.LParam);
|
||||
}
|
||||
|
||||
var deadHandlers = handlers.Where(x => x.IsDead).ToList();
|
||||
if (deadHandlers.Count > 0)
|
||||
{
|
||||
lock (subscribers)
|
||||
{
|
||||
foreach (var deadHandler in deadHandlers) subscribers.Remove(deadHandler);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user