我有WPF桌面应用程序正在使用以下方式推送一些通知:
NotifyIcon.ShowBalloonTip(,,,ToolTipIcon.None)
。
问题是:
Windows 10使用新的“带图像和文本的Windows 10矩形”样式进行气球通知(我不知道它的确切调用方式)。
如果我use ToolTipIcon.None
参数,它会将我的应用程序图标设置为NotifyIcon.Icon
属性并在此气球通知中显示。此图标模糊/拉伸(如图标太小,拉伸到此气球图像所需的尺寸)。
我的ico文件包含多种尺寸:16*16, 32*32, 128*128, 256*256
等。我已经尝试设置只有一个128*128
尺寸的图标文件,但它不起作用。
它应该有效吗?
谢谢。
答案 0 :(得分:2)
下面的类应该在Windows 10的气球提示中显示一个平滑的大图标。它并没有被抛光,但它应该证明这个概念。很大一部分代码是直接从反编译的Microsoft NotifyIcon类中复制的。
原始NotifyIcon类与此之间的关键更改是:
NOTIFYICONDATA.hBalloonIcon
成员的添加(和使用)。nOTIFYICONDATA.dwInfoFlags = NIIF_LARGE_ICON | NIIF_USER
,而不是NIIF_NONE
。您可以阅读NOTIFYICONDATA结构here的具体内容,但要点是:
hBalloonIcon
,专门用于在系统托盘外部使用。NIIF_USER
说要使用hBalloonIcon
中的图标作为气球图标。NIIF_LARGE_ICON
说气球的图标应该很大。NotifyIconLarge类:
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace BalloonNotification
{
public class NotifyIconLarge : IDisposable
{
[DllImport("shell32.dll", CharSet = CharSet.Auto)]
public static extern int Shell_NotifyIcon(int message, NOTIFYICONDATA pnid);
[DllImport("Comctl32.dll", CharSet = CharSet.Unicode)]
private static extern IntPtr LoadIconWithScaleDown(IntPtr hinst, string pszName, int cx, int cy, out IntPtr phico);
[DllImport("user32.dll", SetLastError = true)]
static extern bool DestroyIcon(IntPtr hIcon);
private const int NIIF_LARGE_ICON = 0x00000020;
private const int NIIF_USER = 0x00000004;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class NOTIFYICONDATA
{
public int cbSize = Marshal.SizeOf(typeof(NOTIFYICONDATA));
public IntPtr hWnd;
public int uID;
public int uFlags;
public int uCallbackMessage;
public IntPtr hIcon;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string szTip;
public int dwState;
public int dwStateMask;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string szInfo;
public int uTimeoutOrVersion;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
public string szInfoTitle;
public int dwInfoFlags;
Guid guidItem;
public IntPtr hBalloonIcon;
}
private IntPtr _windowHandle;
private IntPtr _hIcon;
private bool _added;
private int _id = 1;
private string _tipText;
public NotifyIconLarge(IntPtr windowHandle, string iconFile, int iconSize, string tipText)
{
_windowHandle = windowHandle;
_tipText = tipText;
IntPtr result = LoadIconWithScaleDown(IntPtr.Zero, iconFile, iconSize, iconSize, out _hIcon);
UpdateIcon(true);
}
private void UpdateIcon(bool showIconInTray)
{
NOTIFYICONDATA nOTIFYICONDATA = new NOTIFYICONDATA();
nOTIFYICONDATA.uCallbackMessage = 2048;
nOTIFYICONDATA.uFlags = 1;
nOTIFYICONDATA.hWnd = _windowHandle;
nOTIFYICONDATA.uID = _id;
nOTIFYICONDATA.hIcon = IntPtr.Zero;
nOTIFYICONDATA.szTip = null;
if (_hIcon != IntPtr.Zero)
{
nOTIFYICONDATA.uFlags |= 2;
nOTIFYICONDATA.hIcon = _hIcon;
}
nOTIFYICONDATA.uFlags |= 4;
nOTIFYICONDATA.szTip = _tipText;
nOTIFYICONDATA.hBalloonIcon = _hIcon;
if (showIconInTray && _hIcon != IntPtr.Zero)
{
if (!_added)
{
Shell_NotifyIcon(0, nOTIFYICONDATA);
_added = true;
}
else
{
Shell_NotifyIcon(1, nOTIFYICONDATA);
}
}
else
{
if (_added)
{
Shell_NotifyIcon(2, nOTIFYICONDATA);
_added = false;
}
}
}
public void ShowBalloonTip(int timeout, string tipTitle, string tipText, ToolTipIcon tipIcon)
{
NOTIFYICONDATA nOTIFYICONDATA = new NOTIFYICONDATA();
nOTIFYICONDATA.hWnd = _windowHandle;
nOTIFYICONDATA.uID = _id;
nOTIFYICONDATA.uFlags = 16;
nOTIFYICONDATA.uTimeoutOrVersion = timeout;
nOTIFYICONDATA.szInfoTitle = tipTitle;
nOTIFYICONDATA.szInfo = tipText;
switch (tipIcon)
{
case ToolTipIcon.None:
nOTIFYICONDATA.dwInfoFlags = NIIF_LARGE_ICON | NIIF_USER;
break;
case ToolTipIcon.Info:
nOTIFYICONDATA.dwInfoFlags = 1;
break;
case ToolTipIcon.Warning:
nOTIFYICONDATA.dwInfoFlags = 2;
break;
case ToolTipIcon.Error:
nOTIFYICONDATA.dwInfoFlags = 3;
break;
}
int ret = Shell_NotifyIcon(1, nOTIFYICONDATA);
}
public void RemoveFromTray()
{
UpdateIcon(false);
if (_hIcon != IntPtr.Zero)
DestroyIcon(_hIcon);
}
~NotifyIconLarge()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
public void Dispose(bool disposing)
{
RemoveFromTray();
}
}
}
在某处声明:
private NotifyIconLarge _nil;
然后像这样使用它:
string fileName = @"C:\path_to_some_icon.ico";
_nil = new NotifyIconLarge(Handle, fileName, 64, "Icon Tip");
_nil.ShowBalloonTip(10000, "Balloon Title", "Balloon Text", ToolTipIcon.None);
完成后,取下托盘图标:
_nil.RemoveFromTray();