Skip to content

Commit

Permalink
trayicon iconelement
Browse files Browse the repository at this point in the history
  • Loading branch information
NotYoojun committed Oct 30, 2023
1 parent bd147da commit 6cd10a3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
8 changes: 8 additions & 0 deletions source/iNKORE.UI.WPF/Helpers/ImageHelper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Expand All @@ -22,11 +23,18 @@ public static class ImageHelper
bmp.DecodePixelWidth = decodeWidth;
bmp.CacheOption = BitmapCacheOption.OnLoad;
bmp.UriSource = new Uri(uri);

//var fs = new FileStream(uri, FileMode.Open, FileAccess.Read);

//bmp.StreamSource = fs;
bmp.EndInit(); //结束初始化

if (bmp.CanFreeze)
bmp.Freeze();

//fs.Close();
//fs.Dispose();

return bmp;
}
}
Expand Down
2 changes: 1 addition & 1 deletion source/iNKORE.UI.WPF/TrayIcon/Interop/WindowMessageSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ private void ProcessWindowMessage(uint msg, IntPtr wParam, IntPtr lParam)
}

var message = (WindowsMessages)lParam.ToInt32();
Debug.WriteLine("Got message " + message);
//Debug.WriteLine("Got message " + message);
switch (message)
{
case WindowsMessages.WM_CONTEXTMENU:
Expand Down
32 changes: 26 additions & 6 deletions source/iNKORE.UI.WPF/TrayIcon/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@
using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Resources;
using System.Windows.Threading;
using iNKORE.UI.WPF.TrayIcon.Interop;
Expand Down Expand Up @@ -62,7 +64,7 @@ static Util()
isDesignMode =
(bool)
DependencyPropertyDescriptor.FromProperty(DesignerProperties.IsInDesignModeProperty,
typeof (FrameworkElement))
typeof(FrameworkElement))
.Metadata.DefaultValue;
}

Expand Down Expand Up @@ -156,7 +158,7 @@ public static BalloonFlags GetBalloonFlag(this BalloonIcon icon)

#endregion

#region ImageSource to Icon
#region ToIcon Extensions

/// <summary>
/// Reads a given image resource into a WinForms icon.
Expand All @@ -169,6 +171,8 @@ public static Icon ToIcon(this ImageSource imageSource)
{
if (imageSource == null) return null;

if (imageSource is BitmapSource bmp) return bmp.ToIcon();

Uri uri = new Uri(imageSource.ToString());
StreamResourceInfo streamInfo = Application.GetResourceStream(uri);

Expand All @@ -179,8 +183,26 @@ public static Icon ToIcon(this ImageSource imageSource)
throw new ArgumentException(msg);
}

Interop.Size iconSize = SystemInfo.SmallIconSize;
return new Icon(streamInfo.Stream, new System.Drawing.Size(iconSize.Width, iconSize.Height));
return new Icon(streamInfo.Stream);
}

/// <summary>
/// Converts a System.Windows.Media.Imaging.BitmapSource to
/// a System.Drawing.Icon.
/// </summary>
/// <param name="bitmap">The System.Windows.Media.Imaging bitmap.</param>
/// <returns>System.Drawing.Icon.</returns>
public static Icon ToIcon(this BitmapSource bitmap)
{
var enc = new PngBitmapEncoder();
enc.Frames.Add(BitmapFrame.Create(bitmap));

using var stream = new MemoryStream();
enc.Save(stream);

using var bmp = new Bitmap(stream);
using var icon = Icon.FromHandle(bmp.GetHicon());
return icon;
}

#endregion
Expand Down Expand Up @@ -240,8 +262,6 @@ public static bool IsMatch(this MouseEvent me, PopupActivationMode activationMod
case PopupActivationMode.All:
//return true for everything except mouse movements
return me != MouseEvent.MouseMove;
case PopupActivationMode.None:
return false;
default:
throw new ArgumentOutOfRangeException("activationMode");
}
Expand Down

0 comments on commit 6cd10a3

Please sign in to comment.