From 23930a2a15d5ef4e13c1a0023a1fed33129cf089 Mon Sep 17 00:00:00 2001 From: Terry MacDonald Date: Mon, 26 Oct 2020 14:30:00 +1300 Subject: [PATCH] Added initial ability to minimise to notification area Used some awesome help from Hans Passant to build logic to allow minimise to notification area as well as be able to change profiles etc from the notification area. https://stackoverflow.com/questions/1730731/how-to-start-winform-app-minimized-to-tray --- HeliosPlus.Shared/ProfileItem.cs | 17 ++- HeliosPlus.Shared/ProfileRepository.cs | 4 + HeliosPlus/HeliosPlus.csproj | 3 + HeliosPlus/ShortcutItem.cs | 13 +- HeliosPlus/ShortcutRepository.cs | 3 + HeliosPlus/UIForms/MainForm.Designer.cs | 143 ++++++++++++-------- HeliosPlus/UIForms/MainForm.cs | 90 +++++++++++++ HeliosPlus/UIForms/MainForm.resx | 171 +++++++++++++++--------- 8 files changed, 319 insertions(+), 125 deletions(-) diff --git a/HeliosPlus.Shared/ProfileItem.cs b/HeliosPlus.Shared/ProfileItem.cs index 2f7f450..0b41e3d 100644 --- a/HeliosPlus.Shared/ProfileItem.cs +++ b/HeliosPlus.Shared/ProfileItem.cs @@ -20,7 +20,7 @@ using NvAPIWrapper.Display; namespace HeliosPlus.Shared { - public class ProfileItem + public class ProfileItem : IComparable { private static List _allSavedProfiles = new List(); private ProfileIcon _profileIcon; @@ -388,10 +388,21 @@ namespace HeliosPlus.Shared } return uncheckedFilename; } - + + public int CompareTo(object obj) + { + if (obj == null) return 1; + + ProfileItem otherProfile = obj as ProfileItem; + if (otherProfile != null) + return this.Name.CompareTo(otherProfile.Name); + else + throw new ArgumentException("Object to CompareTo is not a Shortcut"); + } + } - // Custom comparer for the Profile class + // Custom Equality comparer for the Profile class // Allows us to use 'Contains' class ProfileComparer : IEqualityComparer { diff --git a/HeliosPlus.Shared/ProfileRepository.cs b/HeliosPlus.Shared/ProfileRepository.cs index 4bdd4e1..5d9da63 100644 --- a/HeliosPlus.Shared/ProfileRepository.cs +++ b/HeliosPlus.Shared/ProfileRepository.cs @@ -468,6 +468,10 @@ namespace HeliosPlus.Shared _currentProfile = loadedProfile; } + + // Sort the profiles alphabetically + _allProfiles.Sort(); + } } else { diff --git a/HeliosPlus/HeliosPlus.csproj b/HeliosPlus/HeliosPlus.csproj index 6ef9012..20697ae 100644 --- a/HeliosPlus/HeliosPlus.csproj +++ b/HeliosPlus/HeliosPlus.csproj @@ -60,6 +60,9 @@ + + false + diff --git a/HeliosPlus/ShortcutItem.cs b/HeliosPlus/ShortcutItem.cs index f75503f..ccaead9 100644 --- a/HeliosPlus/ShortcutItem.cs +++ b/HeliosPlus/ShortcutItem.cs @@ -72,7 +72,7 @@ namespace HeliosPlus } - public class ShortcutItem + public class ShortcutItem : IComparable { //private static List _allSavedShortcuts = new List(); @@ -1495,6 +1495,17 @@ namespace HeliosPlus } } + public int CompareTo(object obj) + { + if (obj == null) return 1; + + ShortcutItem otherShortcut = obj as ShortcutItem; + if (otherShortcut != null) + return this.Name.CompareTo(otherShortcut.Name); + else + throw new ArgumentException("Object to CompareTo is not a Shortcut"); + } + } /*internal class IconActions diff --git a/HeliosPlus/ShortcutRepository.cs b/HeliosPlus/ShortcutRepository.cs index ef90bae..10e26a3 100644 --- a/HeliosPlus/ShortcutRepository.cs +++ b/HeliosPlus/ShortcutRepository.cs @@ -399,6 +399,9 @@ namespace HeliosPlus } } } + + // Sort the shortcuts alphabetically + _allShortcuts.Sort(); } } _shortcutsLoaded = true; diff --git a/HeliosPlus/UIForms/MainForm.Designer.cs b/HeliosPlus/UIForms/MainForm.Designer.cs index 9d6f99f..29de4ff 100644 --- a/HeliosPlus/UIForms/MainForm.Designer.cs +++ b/HeliosPlus/UIForms/MainForm.Designer.cs @@ -37,16 +37,19 @@ this.btn_setup_game_shortcuts = new System.Windows.Forms.Button(); this.btn_exit = new System.Windows.Forms.Button(); this.pb_game_shortcut = new System.Windows.Forms.PictureBox(); - this.mainNotifyIcon = new System.Windows.Forms.NotifyIcon(this.components); + this.notifyIcon = new System.Windows.Forms.NotifyIcon(this.components); this.mainContextMenuStrip = new System.Windows.Forms.ContextMenuStrip(this.components); - this.toolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem(); - this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator(); - this.changeDisplayProfileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); - this.profilesToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); - this.runShortcutToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.toolStripMenuItemHeading = new System.Windows.Forms.ToolStripMenuItem(); + this.toolStripSeparator = new System.Windows.Forms.ToolStripSeparator(); this.openApplicationWindowToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); - this.toolStripSeparator2 = new System.Windows.Forms.ToolStripSeparator(); - this.toolStripMenuItem2 = new System.Windows.Forms.ToolStripMenuItem(); + this.profileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.profilesToolStripMenuItemHeading = new System.Windows.Forms.ToolStripMenuItem(); + this.profileToolStripSeparator = new System.Windows.Forms.ToolStripSeparator(); + this.shortcutToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.shortcutsToolStripMenuItemHeading = new System.Windows.Forms.ToolStripMenuItem(); + this.shortcutToolStripSeparator = new System.Windows.Forms.ToolStripSeparator(); + this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator(); + this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit(); this.splitContainer1.Panel1.SuspendLayout(); this.splitContainer1.Panel2.SuspendLayout(); @@ -127,65 +130,86 @@ this.pb_game_shortcut.TabStop = false; this.pb_game_shortcut.Click += new System.EventHandler(this.pb_game_shortcut_Click); // - // mainNotifyIcon + // notifyIcon // - resources.ApplyResources(this.mainNotifyIcon, "mainNotifyIcon"); - this.mainNotifyIcon.ContextMenuStrip = this.mainContextMenuStrip; + resources.ApplyResources(this.notifyIcon, "notifyIcon"); + this.notifyIcon.ContextMenuStrip = this.mainContextMenuStrip; // // mainContextMenuStrip // this.mainContextMenuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { - this.toolStripMenuItem1, - this.toolStripSeparator1, + this.toolStripMenuItemHeading, + this.toolStripSeparator, this.openApplicationWindowToolStripMenuItem, - this.changeDisplayProfileToolStripMenuItem, - this.runShortcutToolStripMenuItem}); + this.profileToolStripMenuItem, + this.shortcutToolStripMenuItem, + this.toolStripSeparator1, + this.exitToolStripMenuItem}); this.mainContextMenuStrip.Name = "mainContextMenuStrip"; resources.ApplyResources(this.mainContextMenuStrip, "mainContextMenuStrip"); // - // toolStripMenuItem1 + // toolStripMenuItemHeading // - resources.ApplyResources(this.toolStripMenuItem1, "toolStripMenuItem1"); - this.toolStripMenuItem1.Name = "toolStripMenuItem1"; + resources.ApplyResources(this.toolStripMenuItemHeading, "toolStripMenuItemHeading"); + this.toolStripMenuItemHeading.Name = "toolStripMenuItemHeading"; + // + // toolStripSeparator + // + this.toolStripSeparator.Name = "toolStripSeparator"; + resources.ApplyResources(this.toolStripSeparator, "toolStripSeparator"); + // + // openApplicationWindowToolStripMenuItem + // + resources.ApplyResources(this.openApplicationWindowToolStripMenuItem, "openApplicationWindowToolStripMenuItem"); + this.openApplicationWindowToolStripMenuItem.Name = "openApplicationWindowToolStripMenuItem"; + this.openApplicationWindowToolStripMenuItem.Click += new System.EventHandler(this.openApplicationWindowToolStripMenuItem_Click); + // + // profileToolStripMenuItem + // + this.profileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.profilesToolStripMenuItemHeading, + this.profileToolStripSeparator}); + this.profileToolStripMenuItem.Name = "profileToolStripMenuItem"; + resources.ApplyResources(this.profileToolStripMenuItem, "profileToolStripMenuItem"); + // + // profilesToolStripMenuItemHeading + // + resources.ApplyResources(this.profilesToolStripMenuItemHeading, "profilesToolStripMenuItemHeading"); + this.profilesToolStripMenuItemHeading.Name = "profilesToolStripMenuItemHeading"; + // + // profileToolStripSeparator + // + this.profileToolStripSeparator.Name = "profileToolStripSeparator"; + resources.ApplyResources(this.profileToolStripSeparator, "profileToolStripSeparator"); + // + // shortcutToolStripMenuItem + // + this.shortcutToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.shortcutsToolStripMenuItemHeading, + this.shortcutToolStripSeparator}); + this.shortcutToolStripMenuItem.Name = "shortcutToolStripMenuItem"; + resources.ApplyResources(this.shortcutToolStripMenuItem, "shortcutToolStripMenuItem"); + // + // shortcutsToolStripMenuItemHeading + // + resources.ApplyResources(this.shortcutsToolStripMenuItemHeading, "shortcutsToolStripMenuItemHeading"); + this.shortcutsToolStripMenuItemHeading.Name = "shortcutsToolStripMenuItemHeading"; + // + // shortcutToolStripSeparator + // + this.shortcutToolStripSeparator.Name = "shortcutToolStripSeparator"; + resources.ApplyResources(this.shortcutToolStripSeparator, "shortcutToolStripSeparator"); // // toolStripSeparator1 // this.toolStripSeparator1.Name = "toolStripSeparator1"; resources.ApplyResources(this.toolStripSeparator1, "toolStripSeparator1"); // - // changeDisplayProfileToolStripMenuItem + // exitToolStripMenuItem // - this.changeDisplayProfileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { - this.profilesToolStripMenuItem, - this.toolStripSeparator2, - this.toolStripMenuItem2}); - this.changeDisplayProfileToolStripMenuItem.Name = "changeDisplayProfileToolStripMenuItem"; - resources.ApplyResources(this.changeDisplayProfileToolStripMenuItem, "changeDisplayProfileToolStripMenuItem"); - // - // profilesToolStripMenuItem - // - this.profilesToolStripMenuItem.Name = "profilesToolStripMenuItem"; - resources.ApplyResources(this.profilesToolStripMenuItem, "profilesToolStripMenuItem"); - // - // runShortcutToolStripMenuItem - // - this.runShortcutToolStripMenuItem.Name = "runShortcutToolStripMenuItem"; - resources.ApplyResources(this.runShortcutToolStripMenuItem, "runShortcutToolStripMenuItem"); - // - // openApplicationWindowToolStripMenuItem - // - resources.ApplyResources(this.openApplicationWindowToolStripMenuItem, "openApplicationWindowToolStripMenuItem"); - this.openApplicationWindowToolStripMenuItem.Name = "openApplicationWindowToolStripMenuItem"; - // - // toolStripSeparator2 - // - this.toolStripSeparator2.Name = "toolStripSeparator2"; - resources.ApplyResources(this.toolStripSeparator2, "toolStripSeparator2"); - // - // toolStripMenuItem2 - // - this.toolStripMenuItem2.Name = "toolStripMenuItem2"; - resources.ApplyResources(this.toolStripMenuItem2, "toolStripMenuItem2"); + this.exitToolStripMenuItem.Name = "exitToolStripMenuItem"; + resources.ApplyResources(this.exitToolStripMenuItem, "exitToolStripMenuItem"); + this.exitToolStripMenuItem.Click += new System.EventHandler(this.exitToolStripMenuItem_Click); // // MainForm // @@ -218,15 +242,18 @@ private System.Windows.Forms.Button btn_setup_display_profiles; private System.Windows.Forms.Button btn_setup_game_shortcuts; private System.Windows.Forms.Label lbl_version; - private System.Windows.Forms.NotifyIcon mainNotifyIcon; + private System.Windows.Forms.NotifyIcon notifyIcon; private System.Windows.Forms.ContextMenuStrip mainContextMenuStrip; - private System.Windows.Forms.ToolStripMenuItem toolStripMenuItem1; - private System.Windows.Forms.ToolStripSeparator toolStripSeparator1; - private System.Windows.Forms.ToolStripMenuItem changeDisplayProfileToolStripMenuItem; - private System.Windows.Forms.ToolStripMenuItem profilesToolStripMenuItem; - private System.Windows.Forms.ToolStripMenuItem runShortcutToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem toolStripMenuItemHeading; + private System.Windows.Forms.ToolStripSeparator toolStripSeparator; + private System.Windows.Forms.ToolStripMenuItem profileToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem profilesToolStripMenuItemHeading; + private System.Windows.Forms.ToolStripMenuItem shortcutToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem openApplicationWindowToolStripMenuItem; - private System.Windows.Forms.ToolStripSeparator toolStripSeparator2; - private System.Windows.Forms.ToolStripMenuItem toolStripMenuItem2; + private System.Windows.Forms.ToolStripSeparator profileToolStripSeparator; + private System.Windows.Forms.ToolStripMenuItem shortcutsToolStripMenuItemHeading; + private System.Windows.Forms.ToolStripSeparator shortcutToolStripSeparator; + private System.Windows.Forms.ToolStripSeparator toolStripSeparator1; + private System.Windows.Forms.ToolStripMenuItem exitToolStripMenuItem; } } \ No newline at end of file diff --git a/HeliosPlus/UIForms/MainForm.cs b/HeliosPlus/UIForms/MainForm.cs index c12af52..549f549 100644 --- a/HeliosPlus/UIForms/MainForm.cs +++ b/HeliosPlus/UIForms/MainForm.cs @@ -10,18 +10,48 @@ using System.Windows.Forms; using HeliosPlus.GameLibraries; using System.Threading; using System.Reflection; +using HeliosPlus.Shared; namespace HeliosPlus.UIForms { public partial class MainForm : Form { + + private bool allowVisible; // ContextMenu's Show command used + private bool allowClose; // ContextMenu's Exit command used + public MainForm() { InitializeComponent(); btn_setup_display_profiles.Parent = splitContainer1.Panel1; btn_setup_game_shortcuts.Parent = splitContainer1.Panel2; lbl_version.Text = string.Format(lbl_version.Text, Assembly.GetExecutingAssembly().GetName().Version); + notifyIcon.Visible = true; + // Make the form show + allowVisible = true; + // Close the application when the form is closed + allowClose = true; + RefreshNotifyIconMenus(); + } + protected override void SetVisibleCore(bool value) + { + if (!allowVisible) + { + value = false; + if (!this.IsHandleCreated) CreateHandle(); + } + base.SetVisibleCore(value); + } + + protected override void OnFormClosing(FormClosingEventArgs e) + { + if (!allowClose) + { + this.Hide(); + e.Cancel = true; + } + base.OnFormClosing(e); } private void btn_exit_Click(object sender, EventArgs e) @@ -59,5 +89,65 @@ namespace HeliosPlus.UIForms //SteamGame.GetAllInstalledGames(); } + private void RefreshNotifyIconMenus() + { + // Clear all the profiles + profileToolStripMenuItem.DropDownItems.Clear(); + // Prepare the heading shortcuts + ToolStripMenuItem heading = new ToolStripMenuItem(); + heading.Text = "Display Profiles"; + Font headingFont = new Font(heading.Font, FontStyle.Italic); + heading.Font = headingFont; + heading.Enabled = false; + profileToolStripMenuItem.DropDownItems.Add(heading); + ToolStripSeparator separator = new ToolStripSeparator(); + profileToolStripMenuItem.DropDownItems.Add(separator); + + // Add the current slist of profiles into the NotifyIcon context menu + foreach (ProfileItem profile in ProfileRepository.AllProfiles) + { + profileToolStripMenuItem.DropDownItems.Add(profile.Name,profile.ProfileBitmap, runProfileToolStripMenuItem_Click); + } + + // Clear all the shortcuts + shortcutToolStripMenuItem.DropDownItems.Clear(); + // Prepare the heading shortcuts + heading = new ToolStripMenuItem(); + heading.Text = "Game Shortcuts"; + heading.Font = headingFont; + heading.Enabled = false; + shortcutToolStripMenuItem.DropDownItems.Add(heading); + separator = new ToolStripSeparator(); + shortcutToolStripMenuItem.DropDownItems.Add(separator); + // Add the current list of profiles into the NotifyIcon context menu + foreach (ShortcutItem shortcut in ShortcutRepository.AllShortcuts) + { + shortcutToolStripMenuItem.DropDownItems.Add(shortcut.Name,shortcut.ShortcutBitmap, runShortcutToolStripMenuItem_Click); + } + + } + + private void runProfileToolStripMenuItem_Click(object sender, EventArgs e) + { + this.Show(); + } + + private void runShortcutToolStripMenuItem_Click(object sender, EventArgs e) + { + this.Show(); + } + + private void openApplicationWindowToolStripMenuItem_Click(object sender, EventArgs e) + { + allowVisible = true; + Show(); + BringToFront(); + } + + private void exitToolStripMenuItem_Click(object sender, EventArgs e) + { + allowClose = true; + Application.Exit(); + } } } diff --git a/HeliosPlus/UIForms/MainForm.resx b/HeliosPlus/UIForms/MainForm.resx index 90c2b25..0eb3c37 100644 --- a/HeliosPlus/UIForms/MainForm.resx +++ b/HeliosPlus/UIForms/MainForm.resx @@ -10768,7 +10768,7 @@ Microsoft Sans Serif, 21.75pt - 212, 194 + 212, 180 360, 50 @@ -10798,7 +10798,7 @@ Flat - 698, 354 + 698, 347 75, 23 @@ -63142,28 +63142,31 @@ 1 - + 17, 17 - + Use this application to automatically change your display settings when you run a game. - + HeliosPlus 152, 17 - + False - + + Segoe UI, 9pt, style=Italic + + 218, 22 - + HeliosPlus - + 215, 6 @@ -63175,35 +63178,59 @@ Open Application Window - - 180, 22 + + False - + + Segoe UI, 9pt, style=Italic + + + 155, 22 + + Display Profiles - - 177, 6 + + 152, 6 - - 180, 22 - - - DIsplay Profile 1 - - + 218, 22 - - Change Display Profile To + + Change Display Profile - + + False + + + Segoe UI, 9pt, style=Italic + + + 156, 22 + + + Game Shortcuts + + + 153, 6 + + 218, 22 - - Run Shortcut + + Run Game Shortcut + + + 215, 6 + + + 218, 22 + + + Close HeliosPlus - 219, 120 + 219, 126 Text @@ -63214,7 +63241,7 @@ System.Windows.Forms.ContextMenuStrip, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - + AAABAAkAEBAAAAEAIABoBAAAlgAAABgYAAABACAAiAkAAP4EAAAgIAAAAQAgAKgQAACGDgAAMDAAAAEA IACoJQAALh8AAEBAAAABACAAKEIAANZEAABgYAAAAQAgAKiUAAD+hgAAgIAAAAEAIAAoCAEAphsBAMDA @@ -71025,10 +71052,10 @@ rjGJe6619efaHz2S/5D4v/OFla+gZqVXAAAAAElFTkSuQmCC - + HeliosPlus - + True @@ -78866,58 +78893,76 @@ HeliosPlus - - mainNotifyIcon + + notifyIcon - + System.Windows.Forms.NotifyIcon, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - toolStripMenuItem1 + + toolStripMenuItemHeading - + System.Windows.Forms.ToolStripMenuItem, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - toolStripSeparator1 + + toolStripSeparator - + System.Windows.Forms.ToolStripSeparator, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - changeDisplayProfileToolStripMenuItem - - - System.Windows.Forms.ToolStripMenuItem, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - profilesToolStripMenuItem - - - System.Windows.Forms.ToolStripMenuItem, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - runShortcutToolStripMenuItem - - - System.Windows.Forms.ToolStripMenuItem, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - openApplicationWindowToolStripMenuItem System.Windows.Forms.ToolStripMenuItem, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - toolStripSeparator2 + + profileToolStripMenuItem - + + System.Windows.Forms.ToolStripMenuItem, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + profilesToolStripMenuItemHeading + + + System.Windows.Forms.ToolStripMenuItem, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + profileToolStripSeparator + + System.Windows.Forms.ToolStripSeparator, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - toolStripMenuItem2 + + shortcutToolStripMenuItem - + + System.Windows.Forms.ToolStripMenuItem, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + shortcutsToolStripMenuItemHeading + + + System.Windows.Forms.ToolStripMenuItem, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + shortcutToolStripSeparator + + + System.Windows.Forms.ToolStripSeparator, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + toolStripSeparator1 + + + System.Windows.Forms.ToolStripSeparator, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + exitToolStripMenuItem + + System.Windows.Forms.ToolStripMenuItem, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089