Example #1
0
        private void UpdateIcon(bool forceDestroy = false)
        {
            if (DesignerProperties.GetIsInDesignMode(this)) return;

            var iconVisibility = IconVisibility;
            bool showIconInTray = !forceDestroy &&
                                  (iconVisibility == NotifyIconVisibility.Visible ||
                                   (iconVisibility == NotifyIconVisibility.UseControlVisibility && IsVisible));

            lock (_syncObj)
            {
                IntPtr iconHandle = IntPtr.Zero;

                try
                {
                    _allWindowsPermission.Demand();

                    if (showIconInTray && _hwndSource == null)
                    {
                        _hwndSource = new NotifyIconHwndSource(this);
                    }

                    if (_hwndSource != null)
                    {
                        _hwndSource.LockReference(showIconInTray);

                        var pnid = new NativeMethods.NOTIFYICONDATA
                        {
                            uCallbackMessage = (int)NativeMethods.WindowMessage.TrayMouseMessage,
                            uFlags = NativeMethods.NotifyIconFlags.Message | NativeMethods.NotifyIconFlags.ToolTip,
                            hWnd = _hwndSource.Handle,
                            uID = _id,
                            szTip = Text
                        };
                        if (Icon != null)
                        {
                            iconHandle = NativeMethods.GetHIcon(Icon);

                            pnid.uFlags |= NativeMethods.NotifyIconFlags.Icon;
                            pnid.hIcon = iconHandle;
                        }

                        if (showIconInTray && iconHandle != IntPtr.Zero)
                        {
                            if (!_iconCreated)
                            {
                                NativeMethods.Shell_NotifyIcon(0, pnid);
                                _iconCreated = true;
                            }
                            else
                            {
                                NativeMethods.Shell_NotifyIcon(1, pnid);
                            }
                        }
                        else if (_iconCreated)
                        {
                            NativeMethods.Shell_NotifyIcon(2, pnid);
                            _iconCreated = false;
                        }
                    }
                }
                finally
                {
                    if (iconHandle != IntPtr.Zero)
                    {
                        NativeMethods.DestroyIcon(iconHandle);
                    }
                }
            }
        }
Example #2
0
        public void ShowBalloonTip(int timeout, string tipTitle, string tipText, NotifyBalloonIcon tipIcon)
        {
            if (timeout < 0)
            {
                throw new ArgumentOutOfRangeException("timeout", timeout, SR.NotifyIcon_NoNegativeTimeout);
            }
            ArgumentValidator.NotNullOrEmptyString(tipText, "tipText");
            ArgumentValidator.EnumValueIsDefined(typeof(NotifyBalloonIcon), tipIcon, "tipIcon");

            if (_iconCreated)
            {
                _allWindowsPermission.Demand();

                var pnid = new NativeMethods.NOTIFYICONDATA
                {
                    hWnd = _hwndSource.Handle,
                    uID = _id,
                    uFlags = NativeMethods.NotifyIconFlags.Balloon,
                    uTimeoutOrVersion = timeout,
                    szInfoTitle = tipTitle,
                    szInfo = tipText,
                    dwInfoFlags = (int)tipIcon
                };
                NativeMethods.Shell_NotifyIcon(1, pnid);
            }
        }