Skip to content

Commit

Permalink
Fixed #75: Pick the correct size of the icon from a .ico file. (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
devpelux authored May 21, 2022
1 parent b3d484a commit 3672b4e
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 2 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,9 @@ The sample below shows some of the properties of the control. For a more compreh
</Window>
```

## 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.

20 changes: 20 additions & 0 deletions src/NotifyIconWpf/Interop/Size.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Runtime.InteropServices;

namespace Hardcodet.Wpf.TaskbarNotification.Interop
{
/// <summary>
/// Win API struct representing a size with width and height.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct Size
{
/// <summary>
/// Width.
/// </summary>
public int Width;
/// <summary>
/// Height.
/// </summary>
public int Height;
}
}
45 changes: 45 additions & 0 deletions src/NotifyIconWpf/Interop/SystemInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,50 @@ public static Point ScaleWithDpi(this Point point)
Y = (int)(point.Y / DpiFactorY)
};
}

/// <summary>
/// Scale the supplied size to the current DPI settings
/// </summary>
/// <param name="size"></param>
/// <returns>Size</returns>
[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;

/// <summary>
/// Gets a value indicating the recommended size, in pixels, of a small icon
/// </summary>
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
}
}
9 changes: 8 additions & 1 deletion src/NotifyIconWpf/Interop/WinApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,12 @@ public static Point GetCursorPosition(NotifyIconVersion version)

return TrayInfo.GetDeviceCoordinates(cursorPosition);
}


/// <summary>
/// Gets the specified system metric.
/// </summary>
[DllImport(User32)]
internal static extern int GetSystemMetrics(int nIndex);
}
}
}
3 changes: 2 additions & 1 deletion src/NotifyIconWpf/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3672b4e

Please sign in to comment.