首次上传

This commit is contained in:
hanqiangqiang
2026-06-15 10:56:30 +08:00
commit b0213b94d6
4160 changed files with 736101 additions and 0 deletions
@@ -0,0 +1,10 @@
namespace Hardcodet.Wpf.TaskbarNotification
{
public enum BalloonIcon
{
None = 0,
Info = 1,
Warning = 2,
Error = 3
}
}
@@ -0,0 +1,98 @@
using System;
using System.Drawing;
using System.Runtime.InteropServices;
namespace Hardcodet.Wpf.TaskbarNotification.Interop
{
public class AppBarInfo
{
public enum ScreenEdge
{
Undefined = -1,
Left = 0,
Top = 1,
Right = 2,
Bottom = 3
}
private struct APPBARDATA
{
public uint cbSize;
public IntPtr hWnd;
public uint uCallbackMessage;
public uint uEdge;
public RECT rc;
public int lParam;
}
private struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
private const int ABE_BOTTOM = 3;
private const int ABE_LEFT = 0;
private const int ABE_RIGHT = 2;
private const int ABE_TOP = 1;
private const int ABM_GETTASKBARPOS = 5;
private const uint SPI_GETWORKAREA = 48u;
private APPBARDATA m_data;
public ScreenEdge Edge => (ScreenEdge)m_data.uEdge;
public Rectangle WorkArea => GetRectangle(m_data.rc);
[DllImport("user32.dll")]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("shell32.dll")]
private static extern uint SHAppBarMessage(uint dwMessage, ref APPBARDATA data);
[DllImport("user32.dll")]
private static extern int SystemParametersInfo(uint uiAction, uint uiParam, IntPtr pvParam, uint fWinIni);
private Rectangle GetRectangle(RECT rc)
{
return new Rectangle(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top);
}
public void GetPosition(string strClassName, string strWindowName)
{
m_data = default(APPBARDATA);
m_data.cbSize = (uint)Marshal.SizeOf(m_data.GetType());
IntPtr intPtr = FindWindow(strClassName, strWindowName);
if (intPtr != IntPtr.Zero)
{
uint num = SHAppBarMessage(5u, ref m_data);
if (num != 1)
{
throw new Exception("Failed to communicate with the given AppBar");
}
return;
}
throw new Exception("Failed to find an AppBar that matched the given criteria");
}
public void GetSystemTaskBarPosition()
{
GetPosition("Shell_TrayWnd", null);
}
}
}
@@ -0,0 +1,14 @@
namespace Hardcodet.Wpf.TaskbarNotification.Interop
{
public enum BalloonFlags
{
None = 0,
Info = 1,
Warning = 2,
Error = 3,
User = 4,
NoSound = 16,
LargeIcon = 32,
RespectQuietTime = 128
}
}
@@ -0,0 +1,16 @@
using System;
namespace Hardcodet.Wpf.TaskbarNotification.Interop
{
[Flags]
public enum IconDataMembers
{
Message = 1,
Icon = 2,
Tip = 4,
State = 8,
Info = 0x10,
Realtime = 0x40,
UseLegacyToolTips = 0x80
}
}
@@ -0,0 +1,8 @@
namespace Hardcodet.Wpf.TaskbarNotification.Interop
{
public enum IconState
{
Visible = 0,
Hidden = 1
}
}
@@ -0,0 +1,15 @@
namespace Hardcodet.Wpf.TaskbarNotification.Interop
{
public enum MouseEvent
{
MouseMove = 0,
IconRightMouseDown = 1,
IconLeftMouseDown = 2,
IconRightMouseUp = 3,
IconLeftMouseUp = 4,
IconMiddleMouseDown = 5,
IconMiddleMouseUp = 6,
IconDoubleClick = 7,
BalloonToolTipClicked = 8
}
}
@@ -0,0 +1,11 @@
namespace Hardcodet.Wpf.TaskbarNotification.Interop
{
public enum NotifyCommand
{
Add = 0,
Modify = 1,
Delete = 2,
SetFocus = 3,
SetVersion = 4
}
}
@@ -0,0 +1,66 @@
using System;
using System.Runtime.InteropServices;
namespace Hardcodet.Wpf.TaskbarNotification.Interop
{
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct NotifyIconData
{
public uint cbSize;
public IntPtr WindowHandle;
public uint TaskbarIconId;
public IconDataMembers ValidMembers;
public uint CallbackMessageId;
public IntPtr IconHandle;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string ToolTipText;
public IconState IconState;
public IconState StateMask;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string BalloonText;
public uint VersionOrTimeout;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
public string BalloonTitle;
public BalloonFlags BalloonFlags;
public Guid TaskbarIconGuid;
public IntPtr CustomBalloonIconHandle;
public static NotifyIconData CreateDefault(IntPtr handle)
{
NotifyIconData notifyIconData = default(NotifyIconData);
if (Environment.OSVersion.Version.Major >= 6)
{
notifyIconData.cbSize = (uint)Marshal.SizeOf(notifyIconData);
}
else
{
notifyIconData.cbSize = 952u;
notifyIconData.VersionOrTimeout = 10u;
}
notifyIconData.WindowHandle = handle;
notifyIconData.TaskbarIconId = 0u;
notifyIconData.CallbackMessageId = 1024u;
notifyIconData.VersionOrTimeout = 0u;
notifyIconData.IconHandle = IntPtr.Zero;
notifyIconData.IconState = IconState.Hidden;
notifyIconData.StateMask = IconState.Hidden;
notifyIconData.ValidMembers = IconDataMembers.Message | IconDataMembers.Icon | IconDataMembers.Tip;
notifyIconData.ToolTipText = (notifyIconData.BalloonText = (notifyIconData.BalloonTitle = string.Empty));
return notifyIconData;
}
}
}
@@ -0,0 +1,9 @@
namespace Hardcodet.Wpf.TaskbarNotification.Interop
{
public enum NotifyIconVersion
{
Win95 = 0,
Win2000 = 3,
Vista = 4
}
}
@@ -0,0 +1,9 @@
namespace Hardcodet.Wpf.TaskbarNotification.Interop
{
public struct Point
{
public int X;
public int Y;
}
}
@@ -0,0 +1,29 @@
using System.Windows.Interop;
namespace Hardcodet.Wpf.TaskbarNotification.Interop
{
public static class SystemInfo
{
private static readonly System.Windows.Point DpiFactors;
public static double DpiFactorX => DpiFactors.X;
public static double DpiFactorY => DpiFactors.Y;
static SystemInfo()
{
using HwndSource hwndSource = new HwndSource(default(HwndSourceParameters));
HwndTarget compositionTarget = hwndSource.CompositionTarget;
if (compositionTarget != null)
{
_ = compositionTarget.TransformToDevice;
if (true)
{
DpiFactors = new System.Windows.Point(hwndSource.CompositionTarget.TransformToDevice.M11, hwndSource.CompositionTarget.TransformToDevice.M22);
return;
}
}
DpiFactors = new System.Windows.Point(1.0, 1.0);
}
}
}
@@ -0,0 +1,48 @@
using System.Drawing;
namespace Hardcodet.Wpf.TaskbarNotification.Interop
{
public static class TrayInfo
{
public static Point GetTrayLocation()
{
int num = 2;
AppBarInfo appBarInfo = new AppBarInfo();
appBarInfo.GetSystemTaskBarPosition();
Rectangle workArea = appBarInfo.WorkArea;
int x = 0;
int y = 0;
switch (appBarInfo.Edge)
{
case AppBarInfo.ScreenEdge.Left:
x = workArea.Right + num;
y = workArea.Bottom;
break;
case AppBarInfo.ScreenEdge.Bottom:
x = workArea.Right;
y = workArea.Bottom - workArea.Height - num;
break;
case AppBarInfo.ScreenEdge.Top:
x = workArea.Right;
y = workArea.Top + workArea.Height + num;
break;
case AppBarInfo.ScreenEdge.Right:
x = workArea.Right - workArea.Width - num;
y = workArea.Bottom;
break;
}
Point point = default(Point);
point.X = x;
point.Y = y;
return GetDeviceCoordinates(point);
}
public static Point GetDeviceCoordinates(Point point)
{
Point result = default(Point);
result.X = (int)((double)point.X / SystemInfo.DpiFactorX);
result.Y = (int)((double)point.Y / SystemInfo.DpiFactorY);
return result;
}
}
}
@@ -0,0 +1,40 @@
using System;
using System.Runtime.InteropServices;
namespace Hardcodet.Wpf.TaskbarNotification.Interop
{
internal static class WinApi
{
private const string User32 = "user32.dll";
[DllImport("shell32.Dll", CharSet = CharSet.Unicode)]
public static extern bool Shell_NotifyIcon(NotifyCommand cmd, [In] ref NotifyIconData data);
[DllImport("user32.dll", EntryPoint = "CreateWindowExW", SetLastError = true)]
public static extern IntPtr CreateWindowEx(int dwExStyle, [MarshalAs(UnmanagedType.LPWStr)] string lpClassName, [MarshalAs(UnmanagedType.LPWStr)] string lpWindowName, int dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lpParam);
[DllImport("user32.dll")]
public static extern IntPtr DefWindowProc(IntPtr hWnd, uint msg, IntPtr wparam, IntPtr lparam);
[DllImport("user32.dll", EntryPoint = "RegisterClassW", SetLastError = true)]
public static extern short RegisterClass(ref WindowClass lpWndClass);
[DllImport("user32.dll", EntryPoint = "RegisterWindowMessageW")]
public static extern uint RegisterWindowMessage([MarshalAs(UnmanagedType.LPWStr)] string lpString);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool DestroyWindow(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern int GetDoubleClickTime();
[DllImport("user32.dll", SetLastError = true)]
public static extern bool GetPhysicalCursorPos(ref Point lpPoint);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool GetCursorPos(ref Point lpPoint);
}
}
@@ -0,0 +1,30 @@
using System;
using System.Runtime.InteropServices;
namespace Hardcodet.Wpf.TaskbarNotification.Interop
{
public struct WindowClass
{
public uint style;
public WindowProcedureHandler 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,162 @@
using System;
using System.ComponentModel;
namespace Hardcodet.Wpf.TaskbarNotification.Interop
{
public class WindowMessageSink : IDisposable
{
public const int CallbackMessageId = 1024;
private uint taskbarRestartMessageId;
private bool isDoubleClick;
private WindowProcedureHandler messageHandler;
internal string WindowId { get; private set; }
internal IntPtr MessageWindowHandle { get; private set; }
public NotifyIconVersion Version { get; set; }
public bool IsDisposed { get; private set; }
public event Action<bool> ChangeToolTipStateRequest;
public event Action<MouseEvent> MouseEventReceived;
public event Action<bool> BalloonToolTipChanged;
public event Action TaskbarCreated;
public WindowMessageSink(NotifyIconVersion version)
{
Version = version;
CreateMessageWindow();
}
private WindowMessageSink()
{
}
internal static WindowMessageSink CreateEmpty()
{
return new WindowMessageSink
{
MessageWindowHandle = IntPtr.Zero,
Version = NotifyIconVersion.Vista
};
}
private void CreateMessageWindow()
{
WindowId = "WPFTaskbarIcon_" + Guid.NewGuid().ToString();
messageHandler = OnWindowMessageReceived;
WindowClass lpWndClass = default(WindowClass);
lpWndClass.style = 0u;
lpWndClass.lpfnWndProc = messageHandler;
lpWndClass.cbClsExtra = 0;
lpWndClass.cbWndExtra = 0;
lpWndClass.hInstance = IntPtr.Zero;
lpWndClass.hIcon = IntPtr.Zero;
lpWndClass.hCursor = IntPtr.Zero;
lpWndClass.hbrBackground = IntPtr.Zero;
lpWndClass.lpszMenuName = string.Empty;
lpWndClass.lpszClassName = WindowId;
WinApi.RegisterClass(ref lpWndClass);
taskbarRestartMessageId = WinApi.RegisterWindowMessage("TaskbarCreated");
MessageWindowHandle = WinApi.CreateWindowEx(0, WindowId, "", 0, 0, 0, 1, 1, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
if (MessageWindowHandle == IntPtr.Zero)
{
throw new Win32Exception("Message window handle was not a valid pointer");
}
}
private IntPtr OnWindowMessageReceived(IntPtr hWnd, uint messageId, IntPtr wParam, IntPtr lParam)
{
if (messageId == taskbarRestartMessageId)
{
this.TaskbarCreated?.Invoke();
}
ProcessWindowMessage(messageId, wParam, lParam);
return WinApi.DefWindowProc(hWnd, messageId, wParam, lParam);
}
private void ProcessWindowMessage(uint msg, IntPtr wParam, IntPtr lParam)
{
if (msg != 1024)
{
return;
}
switch (lParam.ToInt32())
{
case 512:
this.MouseEventReceived?.Invoke(MouseEvent.MouseMove);
break;
case 513:
this.MouseEventReceived?.Invoke(MouseEvent.IconLeftMouseDown);
break;
case 514:
if (!isDoubleClick)
{
this.MouseEventReceived?.Invoke(MouseEvent.IconLeftMouseUp);
}
isDoubleClick = false;
break;
case 515:
isDoubleClick = true;
this.MouseEventReceived?.Invoke(MouseEvent.IconDoubleClick);
break;
case 516:
this.MouseEventReceived?.Invoke(MouseEvent.IconRightMouseDown);
break;
case 517:
this.MouseEventReceived?.Invoke(MouseEvent.IconRightMouseUp);
break;
case 519:
this.MouseEventReceived?.Invoke(MouseEvent.IconMiddleMouseDown);
break;
case 520:
this.MouseEventReceived?.Invoke(MouseEvent.IconMiddleMouseUp);
break;
case 1026:
this.BalloonToolTipChanged?.Invoke(obj: true);
break;
case 1027:
case 1028:
this.BalloonToolTipChanged?.Invoke(obj: false);
break;
case 1029:
this.MouseEventReceived?.Invoke(MouseEvent.BalloonToolTipClicked);
break;
case 1030:
this.ChangeToolTipStateRequest?.Invoke(obj: true);
break;
case 1031:
this.ChangeToolTipStateRequest?.Invoke(obj: false);
break;
}
}
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
~WindowMessageSink()
{
Dispose(disposing: false);
}
private void Dispose(bool disposing)
{
if (!IsDisposed)
{
IsDisposed = true;
WinApi.DestroyWindow(MessageWindowHandle);
messageHandler = null;
}
}
}
}
@@ -0,0 +1,6 @@
using System;
namespace Hardcodet.Wpf.TaskbarNotification.Interop
{
public delegate IntPtr WindowProcedureHandler(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam);
}
@@ -0,0 +1,26 @@
namespace Hardcodet.Wpf.TaskbarNotification.Interop
{
public enum WindowsMessages : uint
{
WM_CONTEXTMENU = 123u,
WM_MOUSEMOVE = 512u,
WM_LBUTTONDOWN = 513u,
WM_LBUTTONUP = 514u,
WM_LBUTTONDBLCLK = 515u,
WM_RBUTTONDOWN = 516u,
WM_RBUTTONUP = 517u,
WM_RBUTTONDBLCLK = 518u,
WM_MBUTTONDOWN = 519u,
WM_MBUTTONUP = 520u,
WM_MBUTTONDBLCLK = 521u,
WM_USER = 1024u,
NIN_SELECT = 1024u,
NIN_KEYSELECT = 1025u,
NIN_BALLOONSHOW = 1026u,
NIN_BALLOONHIDE = 1027u,
NIN_BALLOONTIMEOUT = 1028u,
NIN_BALLOONUSERCLICK = 1029u,
NIN_POPUPOPEN = 1030u,
NIN_POPUPCLOSE = 1031u
}
}
@@ -0,0 +1,13 @@
namespace Hardcodet.Wpf.TaskbarNotification
{
public enum PopupActivationMode
{
LeftClick = 0,
RightClick = 1,
DoubleClick = 2,
LeftOrRightClick = 3,
LeftOrDoubleClick = 4,
MiddleClick = 5,
All = 6
}
}
@@ -0,0 +1,47 @@
using System;
using System.Windows;
namespace Hardcodet.Wpf.TaskbarNotification
{
internal static class RoutedEventHelper
{
internal static void RaiseEvent(DependencyObject target, RoutedEventArgs args)
{
if (target is UIElement uIElement)
{
uIElement.RaiseEvent(args);
}
else if (target is ContentElement contentElement)
{
contentElement.RaiseEvent(args);
}
}
internal static void AddHandler(DependencyObject element, RoutedEvent routedEvent, Delegate handler)
{
UIElement uIElement = element as UIElement;
if (element != null && uIElement != null)
{
uIElement.AddHandler(routedEvent, handler);
return;
}
ContentElement contentElement = element as ContentElement;
if (element != null)
{
contentElement?.AddHandler(routedEvent, handler);
}
}
internal static void RemoveHandler(DependencyObject element, RoutedEvent routedEvent, Delegate handler)
{
if (element is UIElement uIElement)
{
uIElement.RemoveHandler(routedEvent, handler);
}
else if (element is ContentElement contentElement)
{
contentElement.RemoveHandler(routedEvent, handler);
}
}
}
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,159 @@
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Resources;
using System.Windows.Threading;
using Hardcodet.Wpf.TaskbarNotification.Interop;
namespace Hardcodet.Wpf.TaskbarNotification
{
internal static class Util
{
public static readonly object SyncRoot;
private static readonly bool isDesignMode;
public static bool IsDesignMode => isDesignMode;
static Util()
{
SyncRoot = new object();
isDesignMode = (bool)DependencyPropertyDescriptor.FromProperty(DesignerProperties.IsInDesignModeProperty, typeof(FrameworkElement)).Metadata.DefaultValue;
}
public static Window CreateHelperWindow()
{
return new Window
{
Width = 0.0,
Height = 0.0,
ShowInTaskbar = false,
WindowStyle = WindowStyle.None,
AllowsTransparency = true,
Opacity = 0.0
};
}
public static bool WriteIconData(ref NotifyIconData data, NotifyCommand command)
{
return WriteIconData(ref data, command, data.ValidMembers);
}
public static bool WriteIconData(ref NotifyIconData data, NotifyCommand command, IconDataMembers flags)
{
if (IsDesignMode)
{
return true;
}
data.ValidMembers = flags;
lock (SyncRoot)
{
return WinApi.Shell_NotifyIcon(command, ref data);
}
}
public static BalloonFlags GetBalloonFlag(this BalloonIcon icon)
{
return icon switch
{
BalloonIcon.None => BalloonFlags.None,
BalloonIcon.Info => BalloonFlags.Info,
BalloonIcon.Warning => BalloonFlags.Warning,
BalloonIcon.Error => BalloonFlags.Error,
_ => throw new ArgumentOutOfRangeException("icon"),
};
}
public static Icon ToIcon(this ImageSource imageSource)
{
if (imageSource == null)
{
return null;
}
Uri uriResource = new Uri(imageSource.ToString());
StreamResourceInfo resourceStream = Application.GetResourceStream(uriResource);
if (resourceStream == null)
{
string format = "The supplied image source '{0}' could not be resolved.";
format = string.Format(format, imageSource);
throw new ArgumentException(format);
}
return new Icon(resourceStream.Stream);
}
public static bool Is<T>(this T value, params T[] candidates)
{
if (candidates == null)
{
return false;
}
foreach (T val in candidates)
{
if (value.Equals(val))
{
return true;
}
}
return false;
}
public static bool IsMatch(this MouseEvent me, PopupActivationMode activationMode)
{
return activationMode switch
{
PopupActivationMode.LeftClick => me == MouseEvent.IconLeftMouseUp,
PopupActivationMode.RightClick => me == MouseEvent.IconRightMouseUp,
PopupActivationMode.LeftOrRightClick => me.Is(MouseEvent.IconLeftMouseUp, MouseEvent.IconRightMouseUp),
PopupActivationMode.LeftOrDoubleClick => me.Is(MouseEvent.IconLeftMouseUp, MouseEvent.IconDoubleClick),
PopupActivationMode.DoubleClick => me.Is(MouseEvent.IconDoubleClick),
PopupActivationMode.MiddleClick => me == MouseEvent.IconMiddleMouseUp,
PopupActivationMode.All => me != MouseEvent.MouseMove,
_ => throw new ArgumentOutOfRangeException("activationMode"),
};
}
public static void ExecuteIfEnabled(this ICommand command, object commandParameter, IInputElement target)
{
if (command == null)
{
return;
}
if (command is RoutedCommand routedCommand)
{
if (routedCommand.CanExecute(commandParameter, target))
{
routedCommand.Execute(commandParameter, target);
}
}
else if (command.CanExecute(commandParameter))
{
command.Execute(commandParameter);
}
}
internal static Dispatcher GetDispatcher(this DispatcherObject source)
{
if (Application.Current != null)
{
return Application.Current.Dispatcher;
}
if (source.Dispatcher != null)
{
return source.Dispatcher;
}
return Dispatcher.CurrentDispatcher;
}
public static bool IsDataContextDataBound(this FrameworkElement element)
{
if (element == null)
{
throw new ArgumentNullException("element");
}
return element.GetBindingExpression(FrameworkElement.DataContextProperty) != null;
}
}
}