我有WPF桌面应用程序,它使用以下命令推送一些通知:NotifyIcon.ShowBalloonTip(,,,ToolTipIcon.None)
。
问题是:
Windows 10对气球通知使用新的“带有图像和文本的Windows 10矩形”样式(我不知道它的确切叫法)。
如果我使用use ToolTipIcon.None
参数,它将获取设置为NotifyIcon.Icon
属性的我的应用程序图标,并将其显示在此Balloon通知中。并且此图标是模糊/拉伸(stretch)的(就像拍摄的图标太小,并且拉伸(stretch)到此气球图像所需的尺寸一样)。
我的ico文件包含几种大小:16*16, 32*32, 128*128, 256*256
等。我已经尝试过将图标文件设置为只有一种128*128
大小,但是没有用。
它应该工作吗?
谢谢你。
最佳答案
下一课在Windows 10的气球提示中应显示一个平滑的大图标。它决不是完美的,但应证明这一概念。很大一部分代码是直接从反编译的Microsoft NotifyIcon类复制的。
原始NotifyIcon类与以下类之间的关键更改是:
NOTIFYICONDATA.hBalloonIcon
成员的添加(和使用)。 nOTIFYICONDATA.dwInfoFlags = NIIF_LARGE_ICON | NIIF_USER
的NIIF_NONE
的设置。 您可以阅读NOTIFYICONDATA结构here的详细信息,但要点是:
在Vista中添加了
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();
关于windows - 如何在不拉伸(stretch)的情况下显示类似于Windows 10气球提示的气球提示,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34760898/