예제 #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 UpdateIcon()
        {
            if (_ownerForm == null)
            {
                return;
            }
            if (DesignMode)
            {
                return;
            }

            var data = new NOTIFYICONDATA4();

            InitNotifyIconData(ref data);

            if (!_iconAdded)
            {
                data.uTimeoutOrVersion = 4;

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

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

            data.hIcon            = _icon == null ? IntPtr.Zero : _icon.Handle;
            data.szTip            = _hintText;
            data.uCallbackMessage = CALLBACK_MESSAGE;
            data.uFlags           = NotifyIconFlags.Icon | NotifyIconFlags.Message | NotifyIconFlags.Tip;
            if (_showDefaultTips)
            {
                data.uFlags |= NotifyIconFlags.ShowTip;
            }

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

            _iconAdded = true;
        }
예제 #3
0
        private void RemoveIcon()
        {
            if (_ownerForm == null)
            {
                return;
            }
            if (!_iconAdded)
            {
                return;
            }

            var data = new NOTIFYICONDATA4();

            InitNotifyIconData(ref data);

            if (!Shell_NotifyIcon(NotifyIconMessages.Delete, data))
            {
                throw new Win32Exception("Shell_NotifyIcon failed");
            }
            _iconAdded = false;
        }
예제 #4
0
 private void InitNotifyIconData(ref NOTIFYICONDATA4 data)
 {
     data.hWnd     = _ownerForm.Handle;
     data.uID      = _id;
     data.guidItem = Guid;
 }
예제 #5
0
 private static extern bool Shell_NotifyIcon(NotifyIconMessages message, NOTIFYICONDATA4 pnid);