/// <summary>
        ///   Instantiates a new TrayIcon
        /// </summary>
        internal TrayIcon()
        {
            var contextMenu = new ContextMenu();

            // add separator if at least one task has been added to the context menu
            if (contextMenu.MenuItems.Count > 0)
            {
                contextMenu.MenuItems.Add("-");
            }

            contextMenu.MenuItems.AddRange(new[] {
                new MenuItem(Resources.AppMenu_Options,
                             (s, e) => {
                    try {
                        new OptionsWindow().Show();
                    } catch (ApplicationException) {
                        /* already open */
                    }
                }),
                new MenuItem(Resources.AppMenu_About,
                             (s, e) => {
                    try {
                        new AboutWindow().Show();
                    } catch (ApplicationException) {
                        /*already open */
                    }
                }),
                new MenuItem("-"),
#if DEBUG
                new MenuItem(@"Report active objects", delegate { ReportObjects(); }),
#endif
                new MenuItem(Resources.AppMenu_Exit, delegate { Exit(); })
            });

            NotifyIcon = new NotifyIcon {
                ContextMenu = contextMenu
            };
            NotifyIcon.MouseClick += (s, e) => {
                int index = -1;

                switch (e.Button)
                {
                case MouseButtons.Left:
                    index = 0;
                    break;

                case MouseButtons.Middle:
                    index = 1;
                    break;

                case MouseButtons.XButton1:
                    index = 2;
                    break;

                case MouseButtons.XButton2:
                    index = 3;
                    break;
                }

                if (index >= 0 &&
                    index < NotifyIcon.ContextMenu.MenuItems.Count &&
                    NotifyIcon.ContextMenu.MenuItems[index].Enabled &&
                    NotifyIcon.ContextMenu.MenuItems[index].Tag != null &&
                    (bool)NotifyIcon.ContextMenu.MenuItems[index].Tag)
                {
                    NotifyIcon.ContextMenu.MenuItems[index].PerformClick();
                }
            };

            // display the tray icon
            Show();

            // get the platform-dependent indicator style variant
            if (Environment.OSVersion.Version.Major > 6)
            {
                this.iconRenderer = new FluentIndicatorRenderer(GetIconHandle());
            }
            else if (
                Environment.OSVersion.Version.Minor > 2)
            {
                this.iconRenderer = new AeroIndicatorRenderer(GetIconHandle());
            }
            else
            {
                this.iconRenderer = new AeroIndicatorRenderer(GetIconHandle());
            }

            // set initial icon
            SetIndicator(IndicatorStatus.Idle);
        }
Beispiel #2
0
        /// <summary>
        ///   Instantiates a new TrayIcon
        /// </summary>
        internal TrayIcon()
        {
            var contextMenu = new ContextMenu();

            contextMenu.MenuItems.AddRange(Application.Options.Tasks.Select((t, i) =>
                                                                            new MenuItem(t.Name, (s, e) => TaskHelper.StartTask(t))
            {
                DefaultItem = i == 0,
                Tag         = true // the tag is used to distinguish action menu items from the rest of stock items
            })
                                           .ToArray());

            // add separator if at least one task has been added to the context menu
            if (contextMenu.MenuItems.Count > 0)
            {
                contextMenu.MenuItems.Add("-");
            }

            contextMenu.MenuItems.AddRange(new[] {
                new MenuItem(Resources.AppMenu_Options,
                             (s, e) => {
                    try { new OptionsWindow().Show(); } catch (ApplicationException) {
                        /* already open */
                    }
                }),
                new MenuItem(Resources.AppMenu_About,
                             (s, e) => {
                    try { new AboutWindow().Show(); } catch (ApplicationException) {
                        /*already open */
                    }
                }),
                new MenuItem("-"),
                new MenuItem(Resources.AppMenu_Exit, (s, e) => Exit())
            });

            NotifyIcon = new NotifyIcon {
                ContextMenu = contextMenu
            };
            NotifyIcon.MouseClick += (s, e) => {
                int index = -1;

                switch (e.Button)
                {
                case MouseButtons.Left:
                    index = 0;
                    break;

                case MouseButtons.Middle:
                    index = 1;
                    break;

                case MouseButtons.XButton1:
                    index = 2;
                    break;

                case MouseButtons.XButton2:
                    index = 3;
                    break;
                }

                if (index == 0 && Application.ActionManager.IsBusy)
                {
                    // there are ongoing actions - display progress dialog
                    Application.ActionManager.DisplayActionDialog();
                    return;
                }

                if (index >= 0 &&
                    index < NotifyIcon.ContextMenu.MenuItems.Count &&
                    NotifyIcon.ContextMenu.MenuItems[index].Enabled &&
                    (bool)NotifyIcon.ContextMenu.MenuItems[index].Tag)
                {
                    NotifyIcon.ContextMenu.MenuItems[index].PerformClick();
                }
            };

            // display the tray icon
            Show();

            // get the platform-dependent indicator style variant
            if (Environment.OSVersion.Version.Major > 6
                )
            {
                this.iconRenderer = new FluentIndicatorRenderer(GetIconHandle());
            }
            else if (
                Environment.OSVersion.Version.Minor > 2
                )
            {
                this.iconRenderer = new AeroIndicatorRenderer(GetIconHandle());
            }
            else
            {
                this.iconRenderer = new AeroIndicatorRenderer(GetIconHandle());
            }

            // set initial icon
            SetIndicator(IndicatorStatus.Idle);
        }