c#气球提示文本:从通知中删除项目文本

时间:2018-06-12 17:10:19

标签: c# notifications

我在C#中使用气球提示文本,一切都按预期工作。但是,每当我的通知出现时,都会有与项目名称相关的子文本。

如何删除此文字?

通知图片:

enter image description here

notifyIcon1.Icon = new Icon(SystemIcons.Application, 40, 40);
notifyIcon1.Visible = true;
notifyIcon1.Text = "Application Installation";
notifyIcon1.BalloonTipText = "The App installaion has started.";
notifyIcon1.BalloonTipIcon = ToolTipIcon.Info;
notifyIcon1.BalloonTipTitle = "Test App Installation";
notifyIcon1.ShowBalloonTip(10000);

1 个答案:

答案 0 :(得分:0)

哦,所以我想我至少有一个开始。如下图所示,我设法删除了运行它的exectuable文件的名称。

Example

我在这里找到了一个示例how to show Balloon tip like Windows 10 Balloon tip without stretching icon以及 cokeman19 中的示例,您可以在其中添加自己的自定义图标。虽然当你不想在底部使用exectuable名称时,这似乎不起作用。

如果你想做一些改变,我建议你看一下https://msdn.microsoft.com/en-us/library/windows/desktop/bb773352(v=vs.85).aspx,因为它提供了一些很好的提示。 这是我做了一些改动的代码,所以我们得到了我们想要的东西。

    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 NIF_LARGE_ICON = 0x00000040;

        [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 tipText)
        {
            _windowHandle = windowHandle;
            _tipText = tipText;
            IntPtr result = LoadIconWithScaleDown(IntPtr.Zero, "", 0, 0, 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 |= 1;
                nOTIFYICONDATA.hIcon = _hIcon;
            }
            nOTIFYICONDATA.uFlags |= 4;
            nOTIFYICONDATA.szTip = _tipText;
            nOTIFYICONDATA.hBalloonIcon = _hIcon;
            if (showIconInTray)
            {
                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 = NIF_LARGE_ICON;
                    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();
        }
    }

首先,你要在某处声明NotifyIconLarge类。

      private NotifyIconLarge _nil;

然后要创建通知弹出窗口,请使用以下代码:

    _nil = new NotifyIconLarge(Handle, "Icon Tip");
    _nil.ShowBalloonTip(10000, "Balloon Title", "Balloon Text", ToolTipIcon.None);

完成托盘后,将其移除:

    _nil.RemoveFromTray();

对于大多数代码

,归功于 cokeman19