From f5234bcec3e2a3afc9f1db55c7348654ccb0a9fa Mon Sep 17 00:00:00 2001 From: Markus Hofknecht Date: Sat, 23 Oct 2021 11:59:39 +0200 Subject: [PATCH] [BUG] Microsoft Store Autostart not working (#131), version 1.0.23.0 --- Packaging/Package.appxmanifest | 2 +- Properties/AssemblyInfo.cs | 4 +- SystemTrayMenu.csproj | 5 +- UserInterface/SettingsForm.Designer.cs | 34 ++++-- UserInterface/SettingsForm.cs | 143 ++++++++++++------------- 5 files changed, 102 insertions(+), 86 deletions(-) diff --git a/Packaging/Package.appxmanifest b/Packaging/Package.appxmanifest index 179f706..44d6932 100644 --- a/Packaging/Package.appxmanifest +++ b/Packaging/Package.appxmanifest @@ -10,7 +10,7 @@ + Version="1.0.23.0" /> SystemTrayMenu diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs index f3c9616..9fc8e14 100644 --- a/Properties/AssemblyInfo.cs +++ b/Properties/AssemblyInfo.cs @@ -39,5 +39,5 @@ using System.Runtime.InteropServices; // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.22.2")] -[assembly: AssemblyFileVersion("1.0.22.2")] +[assembly: AssemblyVersion("1.0.23.0")] +[assembly: AssemblyFileVersion("1.0.23.0")] diff --git a/SystemTrayMenu.csproj b/SystemTrayMenu.csproj index f5f7a8c..8263a59 100644 --- a/SystemTrayMenu.csproj +++ b/SystemTrayMenu.csproj @@ -286,7 +286,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - + all runtime; build; native; contentfiles; analyzers; buildtransitive @@ -297,6 +297,9 @@ + + Designer + SettingsSingleFileGenerator Settings.Designer.cs diff --git a/UserInterface/SettingsForm.Designer.cs b/UserInterface/SettingsForm.Designer.cs index ce4ca94..43047a8 100644 --- a/UserInterface/SettingsForm.Designer.cs +++ b/UserInterface/SettingsForm.Designer.cs @@ -264,6 +264,7 @@ namespace SystemTrayMenu.UserInterface this.buttonOk = new System.Windows.Forms.Button(); this.buttonCancel = new System.Windows.Forms.Button(); this.colorDialog = new System.Windows.Forms.ColorDialog(); + this.buttonAddTaskManagerStartupTask = new System.Windows.Forms.Button(); this.tableLayoutPanelMain.SuspendLayout(); this.tabControl.SuspendLayout(); this.tabPageGeneral.SuspendLayout(); @@ -666,7 +667,7 @@ namespace SystemTrayMenu.UserInterface this.groupBoxAutostart.MinimumSize = new System.Drawing.Size(400, 0); this.groupBoxAutostart.Name = "groupBoxAutostart"; this.groupBoxAutostart.Padding = new System.Windows.Forms.Padding(3, 6, 3, 6); - this.groupBoxAutostart.Size = new System.Drawing.Size(400, 53); + this.groupBoxAutostart.Size = new System.Drawing.Size(400, 59); this.groupBoxAutostart.TabIndex = 0; this.groupBoxAutostart.TabStop = false; this.groupBoxAutostart.Text = "groupBoxAutostart"; @@ -675,15 +676,18 @@ namespace SystemTrayMenu.UserInterface // this.tableLayoutPanelAutostart.AutoSize = true; this.tableLayoutPanelAutostart.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; - this.tableLayoutPanelAutostart.ColumnCount = 1; - this.tableLayoutPanelAutostart.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F)); + this.tableLayoutPanelAutostart.ColumnCount = 3; + this.tableLayoutPanelAutostart.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); + this.tableLayoutPanelAutostart.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); + this.tableLayoutPanelAutostart.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F)); + this.tableLayoutPanelAutostart.Controls.Add(this.buttonAddTaskManagerStartupTask, 0, 0); this.tableLayoutPanelAutostart.Controls.Add(this.checkBoxAutostart, 0, 0); this.tableLayoutPanelAutostart.Dock = System.Windows.Forms.DockStyle.Fill; this.tableLayoutPanelAutostart.Location = new System.Drawing.Point(3, 22); this.tableLayoutPanelAutostart.Name = "tableLayoutPanelAutostart"; this.tableLayoutPanelAutostart.RowCount = 1; - this.tableLayoutPanelAutostart.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F)); - this.tableLayoutPanelAutostart.Size = new System.Drawing.Size(394, 25); + this.tableLayoutPanelAutostart.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F)); + this.tableLayoutPanelAutostart.Size = new System.Drawing.Size(394, 31); this.tableLayoutPanelAutostart.TabIndex = 0; // // checkBoxAutostart @@ -692,7 +696,7 @@ namespace SystemTrayMenu.UserInterface this.checkBoxAutostart.Dock = System.Windows.Forms.DockStyle.Fill; this.checkBoxAutostart.Location = new System.Drawing.Point(3, 3); this.checkBoxAutostart.Name = "checkBoxAutostart"; - this.checkBoxAutostart.Size = new System.Drawing.Size(388, 19); + this.checkBoxAutostart.Size = new System.Drawing.Size(126, 25); this.checkBoxAutostart.TabIndex = 0; this.checkBoxAutostart.Text = "checkBoxAutostart"; this.checkBoxAutostart.UseVisualStyleBackColor = true; @@ -702,7 +706,7 @@ namespace SystemTrayMenu.UserInterface this.groupBoxHotkey.AutoSize = true; this.groupBoxHotkey.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; this.groupBoxHotkey.Controls.Add(this.tableLayoutPanelHotkey); - this.groupBoxHotkey.Location = new System.Drawing.Point(3, 289); + this.groupBoxHotkey.Location = new System.Drawing.Point(3, 295); this.groupBoxHotkey.MaximumSize = new System.Drawing.Size(400, 0); this.groupBoxHotkey.MinimumSize = new System.Drawing.Size(400, 0); this.groupBoxHotkey.Name = "groupBoxHotkey"; @@ -755,7 +759,7 @@ namespace SystemTrayMenu.UserInterface this.groupBoxLanguage.AutoSize = true; this.groupBoxLanguage.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; this.groupBoxLanguage.Controls.Add(this.tableLayoutPanelLanguage); - this.groupBoxLanguage.Location = new System.Drawing.Point(3, 354); + this.groupBoxLanguage.Location = new System.Drawing.Point(3, 360); this.groupBoxLanguage.MaximumSize = new System.Drawing.Size(400, 0); this.groupBoxLanguage.MinimumSize = new System.Drawing.Size(400, 0); this.groupBoxLanguage.Name = "groupBoxLanguage"; @@ -3649,6 +3653,19 @@ namespace SystemTrayMenu.UserInterface this.buttonCancel.UseVisualStyleBackColor = true; this.buttonCancel.Click += new System.EventHandler(this.ButtonCancel_Click); // + // buttonAddTaskManagerStartupTask + // + this.buttonAddTaskManagerStartupTask.AutoSize = true; + this.buttonAddTaskManagerStartupTask.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; + this.buttonAddTaskManagerStartupTask.Location = new System.Drawing.Point(135, 3); + this.buttonAddTaskManagerStartupTask.MinimumSize = new System.Drawing.Size(75, 23); + this.buttonAddTaskManagerStartupTask.Name = "buttonAddTaskManagerStartupTask"; + this.buttonAddTaskManagerStartupTask.Size = new System.Drawing.Size(204, 25); + this.buttonAddTaskManagerStartupTask.TabIndex = 1; + this.buttonAddTaskManagerStartupTask.Text = "buttonAddTaskManagerStartupTask"; + this.buttonAddTaskManagerStartupTask.UseVisualStyleBackColor = true; + this.buttonAddTaskManagerStartupTask.Click += new System.EventHandler(this.ButtonAddTaskManagerStartupTask_Click); + // // SettingsForm // this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F); @@ -4104,5 +4121,6 @@ namespace SystemTrayMenu.UserInterface private System.Windows.Forms.TextBox textBoxColorIconsDarkMode; private System.Windows.Forms.CheckBox checkBoxPossibilityToSelectFolderByWindowsContextMenu; private System.Windows.Forms.Button buttonOpenFolder; + private System.Windows.Forms.Button buttonAddTaskManagerStartupTask; } } \ No newline at end of file diff --git a/UserInterface/SettingsForm.cs b/UserInterface/SettingsForm.cs index 5816953..74fa659 100644 --- a/UserInterface/SettingsForm.cs +++ b/UserInterface/SettingsForm.cs @@ -119,6 +119,7 @@ namespace SystemTrayMenu.UserInterface buttonOpenAssemblyLocation.Text = Translator.GetText("Open the assembly location"); groupBoxAutostart.Text = Translator.GetText("Autostart"); checkBoxAutostart.Text = Translator.GetText("Launch on startup"); + buttonAddTaskManagerStartupTask.Text = Translator.GetText("Launch on startup"); groupBoxHotkey.Text = Translator.GetText("Hotkey"); buttonHotkeyDefault.Text = Translator.GetText("Default"); groupBoxLanguage.Text = Translator.GetText("Language"); @@ -203,14 +204,19 @@ namespace SystemTrayMenu.UserInterface InitializeAutostart(); void InitializeAutostart() { - checkBoxAutostart.Checked = - Settings.Default.IsAutostartActivated; + bool useStartupTask = false; #if RELEASEPACKAGE - if (Settings.Default.IsAutostartActivated) - { - checkBoxAutostart.Enabled = false; - } + useStartupTask = true; #endif + if (useStartupTask) + { + checkBoxAutostart.Visible = false; + } + else + { + buttonAddTaskManagerStartupTask.Visible = false; + checkBoxAutostart.Checked = Settings.Default.IsAutostartActivated; + } } InitializeHotkey(); @@ -568,84 +574,21 @@ namespace SystemTrayMenu.UserInterface private void ButtonOk_Click(object sender, EventArgs e) { - Settings.Default.UseIconFromRootFolder = - checkBoxUseIconFromRootFolder.Checked; - - _ = SaveAutostartAsync().Wait(8000); - async Task SaveAutostartAsync() - { - bool useStartupTask = false; -#if RELEASEPACKAGE - useStartupTask = true; + Settings.Default.UseIconFromRootFolder = checkBoxUseIconFromRootFolder.Checked; +#if !RELEASEPACKAGE + SaveAutostartRegistryEntry(); #endif - if (checkBoxAutostart.Checked) - { - if (useStartupTask) - { - // Pass the task ID you specified in the appxmanifest file - StartupTask startupTask = await StartupTask.GetAsync("MyStartupId"); - Log.Info($"Autostart {startupTask.State}."); - - switch (startupTask.State) - { - case StartupTaskState.Enabled: - break; - case StartupTaskState.Disabled: - // Task is disabled but can be enabled. - StartupTaskState newState = await startupTask.RequestEnableAsync(); - break; - case StartupTaskState.DisabledByUser: - Log.Info($"Please enable autostart via Task Manager."); - break; - case StartupTaskState.DisabledByPolicy: - Log.Info($"Autostart disabled by policy. Please add autostart manually, see issue #131"); - break; - case StartupTaskState.EnabledByPolicy: - default: - break; - } - } - else - { - RegistryKey key = Registry.CurrentUser.OpenSubKey( - @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true); - key.SetValue( - Assembly.GetExecutingAssembly().GetName().Name, - System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName); - } - - Settings.Default.IsAutostartActivated = true; - } - else - { - if (useStartupTask) - { - // when added by StartupTask, then only possible to disable autostart via Task Manager. - } - else - { - RegistryKey key = Registry.CurrentUser.OpenSubKey( - @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true); - key.DeleteValue("SystemTrayMenu", false); - } - - Settings.Default.IsAutostartActivated = false; - } - } - SaveHotkey(); void SaveHotkey() { - Settings.Default.HotKey = - new KeysConverter().ConvertToInvariantString( + Settings.Default.HotKey = new KeysConverter().ConvertToInvariantString( textBoxHotkey.Hotkey | textBoxHotkey.HotkeyModifiers); } SaveLanguage(); void SaveLanguage() { - Settings.Default.CurrentCultureInfoName = - comboBoxLanguage.SelectedValue.ToString(); + Settings.Default.CurrentCultureInfoName = comboBoxLanguage.SelectedValue.ToString(); Translator.Initialize(); } @@ -689,6 +632,58 @@ namespace SystemTrayMenu.UserInterface Close(); } + private void SaveAutostartRegistryEntry() + { + if (checkBoxAutostart.Checked) + { + RegistryKey key = Registry.CurrentUser.OpenSubKey( + @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true); + key.SetValue( + Assembly.GetExecutingAssembly().GetName().Name, + System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName); + + Settings.Default.IsAutostartActivated = true; + } + else + { + RegistryKey key = Registry.CurrentUser.OpenSubKey( + @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true); + key.DeleteValue("SystemTrayMenu", false); + + Settings.Default.IsAutostartActivated = false; + } + } + + private void ButtonAddTaskManagerStartupTask_Click(object sender, EventArgs e) + { + _ = AddStartUpAsync(); + static async Task AddStartUpAsync() + { + // Pass the task ID you specified in the appxmanifest file + StartupTask startupTask = await StartupTask.GetAsync("MyStartupId"); + Log.Info($"Autostart {startupTask.State}."); + + switch (startupTask.State) + { + case StartupTaskState.Enabled: + break; + case StartupTaskState.Disabled: + // Task is disabled but can be enabled. + StartupTaskState newState = await startupTask.RequestEnableAsync(); + break; + case StartupTaskState.DisabledByUser: + Log.Info($"Please enable autostart via Task Manager."); + break; + case StartupTaskState.DisabledByPolicy: + Log.Info($"Autostart disabled by policy. Please add autostart manually, see issue #131"); + break; + case StartupTaskState.EnabledByPolicy: + default: + break; + } + } + } + private void ButtonHotkeyDefault_Click(object sender, EventArgs e) { textBoxHotkey.SetHotkey("Ctrl+LWin");