示例#1
0
        /// <summary>
        /// Show balloon notification
        /// </summary>
        /// <param name="caption">Caption of the balloon. Limited to 64 chars</param>
        /// <param name="text">Text of the balloon. Limited to 256 chars</param>
        /// <param name="balloonIcon">Icon to show in balloon. Recommended to be at least 32x32</param>
        /// <param name="backupIcon">Icon type when running on WindowsXP which is unable to use custom icons here</param>
        /// <param name="noSound">Set to true to suppress default system sound</param>
        /// <param name="realTime">Set to true if the notification is relevant only now. If Windows cannot show
        /// the notification now - it will be discarded</param>
        /// <exception cref="ArgumentOutOfRangeException">Shown if <see cref="TrimLongText"/> is false and <paramref name="caption"/> or <paramref name="text"/> overflows the limits</exception>
        public void ShowBalloonTip(string caption, string text, Icon balloonIcon, NotifyIconIcons backupIcon, bool noSound = false, bool realTime = false)
        {
            if (_ownerForm == null)
            {
                return;
            }

            if (caption.Length >= 64)
            {
                if (TrimLongText)
                {
                    caption = caption.Substring(0, 61) + "...";
                }
                else
                {
                    throw new ArgumentOutOfRangeException("caption", caption.Length, "Caption should be less than 64 chars");
                }
            }
            if (text.Length >= 256)
            {
                if (TrimLongText)
                {
                    text = text.Substring(0, 253) + "...";
                }
                else
                {
                    throw new ArgumentOutOfRangeException("text", text.Length, "Text should be less than 64 chars");
                }
            }

            var data = new NOTIFYICONDATA4();

            InitNotifyIconData(ref data);
            data.szInfoTitle = caption;
            data.szInfo      = text;
            data.uFlags      = NotifyIconFlags.Info;
            if (realTime)
            {
                data.uFlags |= NotifyIconFlags.RealTime;
            }
            data.dwInfoFlags  = NotifyIconInfoFlags.IconUser;
            data.hBalloonIcon = balloonIcon.Handle;

            data.dwInfoFlags |= NotifyIconInfoFlags.RespectQuietTime;
            if (UseLargeIcons)
            {
                data.dwInfoFlags |= NotifyIconInfoFlags.LargeIcon;
            }
            if (noSound)
            {
                data.dwInfoFlags |= NotifyIconInfoFlags.NoSound;
            }

            if (!Shell_NotifyIcon(NotifyIconMessages.Modify, data))
            {
                throw new Win32Exception("Shell_NotifyIcon failed");
            }
        }
示例#2
0
        private void ShowBalloonTipLegacy(string caption, string text, NotifyIconIcons balloonIcon, bool noSound = false, bool realTime = false)
        {
            var data = new NOTIFYICONDATA();

            InitNotifyIconData(ref data);
            data.szInfoTitle = caption;
            data.szInfo      = text;
            data.uFlags      = NotifyIconFlags.Info;
            if (realTime)
            {
                data.uFlags |= NotifyIconFlags.RealTime;
            }
            switch (balloonIcon)
            {
            case NotifyIconIcons.None:
                data.dwInfoFlags = NotifyIconInfoFlags.IconNone;
                break;

            case NotifyIconIcons.Error:
                data.dwInfoFlags = NotifyIconInfoFlags.IconError;
                break;

            case NotifyIconIcons.Info:
                data.dwInfoFlags = NotifyIconInfoFlags.IconInfo;
                break;

            case NotifyIconIcons.Warning:
                data.dwInfoFlags = NotifyIconInfoFlags.IconWarning;
                break;

            case NotifyIconIcons.User:
                data.dwInfoFlags = NotifyIconInfoFlags.IconUser;
                break;
            }

            data.dwInfoFlags |= NotifyIconInfoFlags.RespectQuietTime;
            if (UseLargeIcons)
            {
                data.dwInfoFlags |= NotifyIconInfoFlags.LargeIcon;
            }
            if (noSound)
            {
                data.dwInfoFlags |= NotifyIconInfoFlags.NoSound;
            }

            if (!Shell_NotifyIcon(NotifyIconMessages.Modify, data))
            {
                throw new Win32Exception("Shell_NotifyIcon failed");
            }
        }