From 62993f6382388c6d7e634c6159247570e464bfb7 Mon Sep 17 00:00:00 2001 From: Xorboo Date: Sun, 4 Jun 2023 23:09:03 +0200 Subject: [PATCH] Add autorun option --- HdrSwitcher/AutorunManager.cs | 27 ++++++++++++++++ HdrSwitcher/FormMain.Designer.cs | 53 +++++++++++++++++++++++++------- HdrSwitcher/FormMain.cs | 7 +++++ HdrSwitcher/HdrSwitcher.csproj | 19 ++++++------ HdrSwitcher/MonitorTracker.cs | 2 +- 5 files changed, 86 insertions(+), 22 deletions(-) create mode 100644 HdrSwitcher/AutorunManager.cs diff --git a/HdrSwitcher/AutorunManager.cs b/HdrSwitcher/AutorunManager.cs new file mode 100644 index 0000000..6c5d4bc --- /dev/null +++ b/HdrSwitcher/AutorunManager.cs @@ -0,0 +1,27 @@ +using Microsoft.Win32; + +namespace HdrSwitcher +{ + internal static class AutorunManager + { + const string AutorunRegistryPath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"; + const string AppRegistryName = "Hdr Switcher"; + + public static bool AutorunEnabled + { + get + { + var rk = Registry.CurrentUser.OpenSubKey(AutorunRegistryPath, false); + return rk?.GetValue(AppRegistryName) != null; + } + set + { + var rk = Registry.CurrentUser.OpenSubKey(AutorunRegistryPath, true); + if (value) + rk?.SetValue(AppRegistryName, Application.ExecutablePath); + else + rk?.DeleteValue(AppRegistryName, false); + } + } + } +} diff --git a/HdrSwitcher/FormMain.Designer.cs b/HdrSwitcher/FormMain.Designer.cs index b2f8787..ac6fb3b 100644 --- a/HdrSwitcher/FormMain.Designer.cs +++ b/HdrSwitcher/FormMain.Designer.cs @@ -34,9 +34,12 @@ private void InitializeComponent() notifyIcon = new NotifyIcon(components); contextMenuStrip = new ContextMenuStrip(components); monitorToolStripMenuItem = new ToolStripMenuItem(); - toolStripMenuItem2 = new ToolStripSeparator(); + settingsToolStripMenuItem = new ToolStripMenuItem(); + autorunToolStripMenuItem = new ToolStripMenuItem(); + separatorToolStripMenuItem = new ToolStripSeparator(); exitToolStripMenuItem = new ToolStripMenuItem(); checkTimer = new System.Windows.Forms.Timer(components); + label1 = new Label(); contextMenuStrip.SuspendLayout(); SuspendLayout(); // @@ -51,9 +54,9 @@ private void InitializeComponent() // contextMenuStrip // contextMenuStrip.ImageScalingSize = new Size(20, 20); - contextMenuStrip.Items.AddRange(new ToolStripItem[] { monitorToolStripMenuItem, toolStripMenuItem2, exitToolStripMenuItem }); + contextMenuStrip.Items.AddRange(new ToolStripItem[] { monitorToolStripMenuItem, settingsToolStripMenuItem, separatorToolStripMenuItem, exitToolStripMenuItem }); contextMenuStrip.Name = "contextMenuStrip"; - contextMenuStrip.Size = new Size(132, 58); + contextMenuStrip.Size = new Size(132, 82); contextMenuStrip.Opening += contextMenuStrip_Opening; // // monitorToolStripMenuItem @@ -62,10 +65,24 @@ private void InitializeComponent() monitorToolStripMenuItem.Size = new Size(131, 24); monitorToolStripMenuItem.Text = "Monitor"; // - // toolStripMenuItem2 + // settingsToolStripMenuItem // - toolStripMenuItem2.Name = "toolStripMenuItem2"; - toolStripMenuItem2.Size = new Size(128, 6); + settingsToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { autorunToolStripMenuItem }); + settingsToolStripMenuItem.Name = "settingsToolStripMenuItem"; + settingsToolStripMenuItem.Size = new Size(131, 24); + settingsToolStripMenuItem.Text = "Settings"; + // + // autorunToolStripMenuItem + // + autorunToolStripMenuItem.Name = "autorunToolStripMenuItem"; + autorunToolStripMenuItem.Size = new Size(224, 26); + autorunToolStripMenuItem.Text = "Run on startup"; + autorunToolStripMenuItem.Click += autorunToolStripMenuItem_Click; + // + // separatorToolStripMenuItem + // + separatorToolStripMenuItem.Name = "separatorToolStripMenuItem"; + separatorToolStripMenuItem.Size = new Size(128, 6); // // exitToolStripMenuItem // @@ -80,14 +97,25 @@ private void InitializeComponent() checkTimer.Interval = 5000; checkTimer.Tick += checkTimer_Tick; // + // label1 + // + label1.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right; + label1.Font = new Font("Segoe UI", 24F, FontStyle.Bold, GraphicsUnit.Point); + label1.Location = new Point(12, 9); + label1.Name = "label1"; + label1.Size = new Size(449, 184); + label1.TabIndex = 1; + label1.Text = "You weren't supposed to be able to get here you know"; + label1.TextAlign = ContentAlignment.MiddleCenter; + // // FormMain // - AutoScaleDimensions = new SizeF(8F, 20F); - AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(800, 450); + AutoScaleMode = AutoScaleMode.None; + ClientSize = new Size(473, 202); + Controls.Add(label1); Icon = (Icon)resources.GetObject("$this.Icon"); Name = "FormMain"; - Text = "Form1"; + Text = "HDR Switcher by Xorboo"; contextMenuStrip.ResumeLayout(false); ResumeLayout(false); } @@ -97,7 +125,10 @@ private void InitializeComponent() private NotifyIcon notifyIcon; private ContextMenuStrip contextMenuStrip; private ToolStripMenuItem monitorToolStripMenuItem; - private ToolStripSeparator toolStripMenuItem2; + private ToolStripSeparator separatorToolStripMenuItem; private ToolStripMenuItem exitToolStripMenuItem; private System.Windows.Forms.Timer checkTimer; + private ToolStripMenuItem settingsToolStripMenuItem; + private ToolStripMenuItem autorunToolStripMenuItem; + private Label label1; } \ No newline at end of file diff --git a/HdrSwitcher/FormMain.cs b/HdrSwitcher/FormMain.cs index fb3ff11..b014aca 100644 --- a/HdrSwitcher/FormMain.cs +++ b/HdrSwitcher/FormMain.cs @@ -17,6 +17,8 @@ public FormMain() _tracker = new MonitorTracker(Properties.Settings.Default.MonitorIndex); UpdateMonitorState(); + + autorunToolStripMenuItem.Checked = AutorunManager.AutorunEnabled; } @@ -69,6 +71,11 @@ private void notifyIcon_MouseClick(object sender, MouseEventArgs e) UpdateMonitorState(); } + private void autorunToolStripMenuItem_Click(object sender, EventArgs e) + { + AutorunManager.AutorunEnabled = !AutorunManager.AutorunEnabled; + } + private void checkTimer_Tick(object sender, EventArgs e) { UpdateMonitorState(); diff --git a/HdrSwitcher/HdrSwitcher.csproj b/HdrSwitcher/HdrSwitcher.csproj index 05aa25c..84fbf92 100644 --- a/HdrSwitcher/HdrSwitcher.csproj +++ b/HdrSwitcher/HdrSwitcher.csproj @@ -14,7 +14,15 @@ - + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + @@ -46,15 +54,6 @@ SettingsSingleFileGenerator Settings.Designer.cs - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - \ No newline at end of file diff --git a/HdrSwitcher/MonitorTracker.cs b/HdrSwitcher/MonitorTracker.cs index aef5829..2c4279d 100644 --- a/HdrSwitcher/MonitorTracker.cs +++ b/HdrSwitcher/MonitorTracker.cs @@ -2,7 +2,7 @@ namespace HdrSwitcher { - public class MonitorTracker + internal class MonitorTracker { public enum MonitorState {