如何显示气球提示像Windows 10气球提示没有拉伸图标

前端之家收集整理的这篇文章主要介绍了如何显示气球提示像Windows 10气球提示没有拉伸图标前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有 WPF桌面应用程序正在推送一些通知使用:

NotifyIcon.ShowBalloonTip(,ToolTipIcon.None).

问题是:

Windows 10使用新的“带图像和文本的Windows 10矩形”样式进行气球通知(我不知道它是如何调用的).

如果我使用ToolTipIcon.None参数,它会获取我的应用程序图标,该图标设置为NotifyIcon.Icon属性并在此气球通知显示.并且这个图标被模糊/拉伸(就像拍摄太小的图标并拉伸到这个气球图像所需的尺寸).

我的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,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();
原文链接:https://www.f2er.com/windows/364907.html

猜你在找的Windows相关文章