From 3672b4e3c1e530596cd7c70b11e5a2002e9750e1 Mon Sep 17 00:00:00 2001 From: Salvatore Peluso Date: Sat, 21 May 2022 07:59:41 +0200 Subject: [PATCH] Fixed #75: Pick the correct size of the icon from a .ico file. (#76) --- README.md | 6 ++++ src/NotifyIconWpf/Interop/Size.cs | 20 +++++++++++ src/NotifyIconWpf/Interop/SystemInfo.cs | 45 +++++++++++++++++++++++++ src/NotifyIconWpf/Interop/WinApi.cs | 9 ++++- src/NotifyIconWpf/Util.cs | 3 +- 5 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 src/NotifyIconWpf/Interop/Size.cs diff --git a/README.md b/README.md index 052d050..c8e2e87 100644 --- a/README.md +++ b/README.md @@ -50,3 +50,9 @@ The sample below shows some of the properties of the control. For a more compreh ``` +## Contributors and Thanks + +Hi, I'm Philipp! This little library was originally written by me, but is currently mostly maintained by [Jan Karger](https://github.com/punker76) and [Robin Krom](https://github.com/Lakritzator). Big, big kudos to the two of you, and everybody else who [contributed](https://github.com/hardcodet/wpf-notifyicon/graphs/contributors) to this library. You rock! + +Make sure to check out Robin's great [Greenshot](https://getgreenshot.org/) tool (that I use on a daily basis), and Jan's [MahApps](https://github.com/MahApps) UI framework. + diff --git a/src/NotifyIconWpf/Interop/Size.cs b/src/NotifyIconWpf/Interop/Size.cs new file mode 100644 index 0000000..6b20444 --- /dev/null +++ b/src/NotifyIconWpf/Interop/Size.cs @@ -0,0 +1,20 @@ +using System.Runtime.InteropServices; + +namespace Hardcodet.Wpf.TaskbarNotification.Interop +{ + /// + /// Win API struct representing a size with width and height. + /// + [StructLayout(LayoutKind.Sequential)] + public struct Size + { + /// + /// Width. + /// + public int Width; + /// + /// Height. + /// + public int Height; + } +} \ No newline at end of file diff --git a/src/NotifyIconWpf/Interop/SystemInfo.cs b/src/NotifyIconWpf/Interop/SystemInfo.cs index 6edeead..39c8f3d 100644 --- a/src/NotifyIconWpf/Interop/SystemInfo.cs +++ b/src/NotifyIconWpf/Interop/SystemInfo.cs @@ -81,5 +81,50 @@ public static Point ScaleWithDpi(this Point point) Y = (int)(point.Y / DpiFactorY) }; } + + /// + /// Scale the supplied size to the current DPI settings + /// + /// + /// Size + [Pure] + public static Size ScaleWithDpi(this Size size) + { + return new Size + { + Height = (int)(size.Height / DpiFactorY), + Width = (int)(size.Width / DpiFactorX) + }; + } + + #region SmallIconSize + + private static Size? _smallIconSize = null; + + private const int CXSMICON = 49; + private const int CYSMICON = 50; + + /// + /// Gets a value indicating the recommended size, in pixels, of a small icon + /// + public static Size SmallIconSize + { + get + { + if (!_smallIconSize.HasValue) + { + Size smallIconSize = new Size + { + Height = WinApi.GetSystemMetrics(CYSMICON), + Width = WinApi.GetSystemMetrics(CXSMICON) + }; + _smallIconSize = smallIconSize.ScaleWithDpi(); + } + + return _smallIconSize.Value; + } + } + + #endregion } } \ No newline at end of file diff --git a/src/NotifyIconWpf/Interop/WinApi.cs b/src/NotifyIconWpf/Interop/WinApi.cs index c4ce297..6009f94 100644 --- a/src/NotifyIconWpf/Interop/WinApi.cs +++ b/src/NotifyIconWpf/Interop/WinApi.cs @@ -110,5 +110,12 @@ public static Point GetCursorPosition(NotifyIconVersion version) return TrayInfo.GetDeviceCoordinates(cursorPosition); } + + + /// + /// Gets the specified system metric. + /// + [DllImport(User32)] + internal static extern int GetSystemMetrics(int nIndex); } -} \ No newline at end of file +} diff --git a/src/NotifyIconWpf/Util.cs b/src/NotifyIconWpf/Util.cs index 99e61c3..a480f67 100644 --- a/src/NotifyIconWpf/Util.cs +++ b/src/NotifyIconWpf/Util.cs @@ -179,7 +179,8 @@ public static Icon ToIcon(this ImageSource imageSource) throw new ArgumentException(msg); } - return new Icon(streamInfo.Stream); + Interop.Size iconSize = SystemInfo.SmallIconSize; + return new Icon(streamInfo.Stream, new System.Drawing.Size(iconSize.Width, iconSize.Height)); } #endregion