From 258588150061bd0771ac505f42555751e9a168ae Mon Sep 17 00:00:00 2001 From: Marcos Date: Fri, 22 Sep 2023 23:35:53 -0300 Subject: [PATCH] Added notification counter badge to app taskbar icon --- .../Skiff Desktop/MainWindow.xaml.cs | 30 ++++++++++++++++++ .../Properties/Resources.Designer.cs | 10 ++++++ .../Skiff Desktop/Properties/Resources.resx | 3 ++ skiffWindowsApp/Skiff Desktop/badgebg.png | Bin 0 -> 1525 bytes 4 files changed, 43 insertions(+) create mode 100644 skiffWindowsApp/Skiff Desktop/badgebg.png diff --git a/skiffWindowsApp/Skiff Desktop/MainWindow.xaml.cs b/skiffWindowsApp/Skiff Desktop/MainWindow.xaml.cs index c204ac0..db3c49c 100644 --- a/skiffWindowsApp/Skiff Desktop/MainWindow.xaml.cs +++ b/skiffWindowsApp/Skiff Desktop/MainWindow.xaml.cs @@ -10,6 +10,10 @@ using CustomNotificationsExample.CustomMessage; using System.Net.Http; using System.Net.Http.Headers; +using System.Drawing; +using System.Windows.Media; +using System.Windows.Interop; +using System.Windows.Media.Imaging; namespace Skiff_Desktop { @@ -57,6 +61,7 @@ public MainWindow() StateChanged += OnWindowStateChanged; SizeChanged += OnWindowSizeChanged; RestoreWindow(); + TaskbarItemInfo = new(); } internal void RestoreWindow() @@ -209,6 +214,31 @@ internal void UpdateUnreadCount(int newTotal) { UnreadCount = newTotal; UnreadCounterChanged?.Invoke(); + + TaskbarItemInfo.Overlay = null; + if (UnreadCount > 0) + TaskbarItemInfo.Overlay = GetCounterBadge(); + } + + private ImageSource GetCounterBadge() + { + using Bitmap bitmap = Properties.Resources.badgebg; + using Graphics graphics = Graphics.FromImage(bitmap); + + using StringFormat format = new() + { + LineAlignment = StringAlignment.Center, + Alignment = StringAlignment.Center, + }; + var point = new PointF(15f, 17f); + var font = new Font(System.Drawing.FontFamily.GenericSansSerif, emSize: 5f, System.Drawing.FontStyle.Bold); + string counterStr = Math.Clamp(UnreadCount, 0, 99).ToString(); + graphics.DrawString(counterStr, font, System.Drawing.Brushes.White, point, format); + + return Imaging.CreateBitmapSourceFromHIcon( + bitmap.GetHicon(), + Int32Rect.Empty, + BitmapSizeOptions.FromEmptyOptions()); } } diff --git a/skiffWindowsApp/Skiff Desktop/Properties/Resources.Designer.cs b/skiffWindowsApp/Skiff Desktop/Properties/Resources.Designer.cs index a955297..3fdde1c 100644 --- a/skiffWindowsApp/Skiff Desktop/Properties/Resources.Designer.cs +++ b/skiffWindowsApp/Skiff Desktop/Properties/Resources.Designer.cs @@ -60,6 +60,16 @@ internal Resources() { } } + /// + /// Looks up a localized resource of type System.Drawing.Bitmap. + /// + internal static System.Drawing.Bitmap badgebg { + get { + object obj = ResourceManager.GetObject("badgebg", resourceCulture); + return ((System.Drawing.Bitmap)(obj)); + } + } + /// /// Looks up a localized resource of type System.Drawing.Bitmap. /// diff --git a/skiffWindowsApp/Skiff Desktop/Properties/Resources.resx b/skiffWindowsApp/Skiff Desktop/Properties/Resources.resx index bf22922..7705b23 100644 --- a/skiffWindowsApp/Skiff Desktop/Properties/Resources.resx +++ b/skiffWindowsApp/Skiff Desktop/Properties/Resources.resx @@ -118,6 +118,9 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + ..\badgebg.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a + ..\logo.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a diff --git a/skiffWindowsApp/Skiff Desktop/badgebg.png b/skiffWindowsApp/Skiff Desktop/badgebg.png new file mode 100644 index 0000000000000000000000000000000000000000..e13f9c98fead3a979a560f908e75c6810835ab36 GIT binary patch literal 1525 zcmVEX>4Tx04R}tkv&MmKpe$iQ>7{u2MdabWT;LSL`58>ibb$c+6t{Ym|Xe=O&XFE z7e~Rh;NZt%)xpJCR|i)?5c~jfb#YR3krMxx6k5c1aNL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