From 0f03a26a4c959e0831adce667563ca8418497a91 Mon Sep 17 00:00:00 2001 From: Ray Date: Mon, 18 May 2020 20:45:25 +0100 Subject: [PATCH] Added: global hot keys + notify icon --- MainForm.cs | 37 ++++++++++++++++++++++++++++ Program.cs | 20 ++++++++++----- Windows/Forms/AForm.cs | 53 +++++++++++++++++++++++++++++----------- Windows/Forms/AForm.resx | 3 +++ 4 files changed, 93 insertions(+), 20 deletions(-) diff --git a/MainForm.cs b/MainForm.cs index f54189a..8f65215 100644 --- a/MainForm.cs +++ b/MainForm.cs @@ -7,6 +7,7 @@ using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.IO; +using System.Runtime.InteropServices; using System.Threading.Tasks; using System.Windows.Forms; @@ -14,6 +15,18 @@ namespace AppLauncher { public partial class MainForm : AForm { + [DllImport("user32.dll")] + protected static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk); + + [DllImport("user32.dll")] + protected static extern bool UnregisterHotKey(IntPtr hWnd, int id); + + protected const int MOD_ALT = 0x1; + protected const int MOD_CONTROL = 0x2; + protected const int MOD_SHIFT = 0x4; + protected const int MOD_WIN = 0x8; + protected const int WM_HOTKEY = 0x312; + protected int collapsedWidth = 40; protected int expandedWidth = 800; @@ -26,6 +39,13 @@ namespace AppLauncher //this.Visible = false; } + protected override void OnLoad(EventArgs e) + { + base.OnLoad(e); + + RegisterHotKey((IntPtr)Handle, 1, (MOD_CONTROL | MOD_ALT), (int)Keys.F10); + } + protected override void OnShown(EventArgs e) { this.Visible = false; @@ -46,6 +66,8 @@ namespace AppLauncher { base.OnClosing(e); + UnregisterHotKey((IntPtr)Handle, 1); + if (string.IsNullOrWhiteSpace(sessionFilename)) { // do nothing @@ -72,6 +94,21 @@ namespace AppLauncher } } + protected override void WndProc(ref Message m) + { + base.WndProc(ref m); + + if (m.Msg != WM_HOTKEY) return; + + switch (m.WParam.ToInt32()) + { + case 1: + this.Visible = !this.Visible; + break; + default: break; + } + } + public async Task ToggleSize() { if (this.Width > collapsedWidth) diff --git a/Program.cs b/Program.cs index 06a2aa3..5f28734 100644 --- a/Program.cs +++ b/Program.cs @@ -1,22 +1,30 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; using System.Windows.Forms; namespace AppLauncher { static class Program { + const string applicationID = "f31c9f65-4047-41e4-ac85-dbe11d12ee46"; + /// /// The main entry point for the application. /// [STAThread] static void Main() { - Application.EnableVisualStyles(); - Application.SetCompatibleTextRenderingDefault(false); - Application.Run(new MainForm()); + System.Threading.Mutex mutex = new System.Threading.Mutex(false, applicationID); + if (mutex.WaitOne(TimeSpan.Zero)) + { + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + Application.Run(new MainForm()); + } + + mutex.ReleaseMutex(); + mutex.Close(); + mutex.Dispose(); + mutex = null; } } } diff --git a/Windows/Forms/AForm.cs b/Windows/Forms/AForm.cs index b394436..d8de44a 100644 --- a/Windows/Forms/AForm.cs +++ b/Windows/Forms/AForm.cs @@ -22,6 +22,8 @@ namespace AppLauncher.Windows.Forms protected internal RyzStudio.Windows.Forms.TImageBox imageBox1; private NotifyIcon notifyIcon1; private Panel panel1; + private ContextMenuStrip contextMenuStrip1; + private ToolStripMenuItem exitToolStripMenuItem; private IContainer components; public AForm() : base() @@ -192,13 +194,16 @@ namespace AppLauncher.Windows.Forms this.imageBox1 = new RyzStudio.Windows.Forms.TImageBox(); this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components); this.panel1 = new System.Windows.Forms.Panel(); + this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components); + this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); ((System.ComponentModel.ISupportInitialize)(this.imageBox3)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.imageBox2)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.imageBox1)).BeginInit(); + this.contextMenuStrip1.SuspendLayout(); this.SuspendLayout(); - // + // // imageBox3 - // + // this.imageBox3.BackColor = System.Drawing.Color.Transparent; this.imageBox3.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center; this.imageBox3.ErrorImage = null; @@ -215,9 +220,9 @@ namespace AppLauncher.Windows.Forms this.imageBox3.TabIndex = 146; this.imageBox3.TabStop = false; this.imageBox3.MouseClick += new System.Windows.Forms.MouseEventHandler(this.imageBox3_MouseClick); - // + // // imageBox2 - // + // this.imageBox2.BackColor = System.Drawing.Color.Transparent; this.imageBox2.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center; this.imageBox2.ErrorImage = null; @@ -234,9 +239,9 @@ namespace AppLauncher.Windows.Forms this.imageBox2.TabIndex = 147; this.imageBox2.TabStop = false; this.imageBox2.MouseClick += new System.Windows.Forms.MouseEventHandler(this.imageBox2_MouseClick); - // + // // imageBox1 - // + // this.imageBox1.BackColor = System.Drawing.Color.Transparent; this.imageBox1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center; this.imageBox1.ErrorImage = null; @@ -253,14 +258,17 @@ namespace AppLauncher.Windows.Forms this.imageBox1.TabIndex = 148; this.imageBox1.TabStop = false; this.imageBox1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.imageBox1_MouseClick); - // + // // notifyIcon1 - // + // + this.notifyIcon1.ContextMenuStrip = this.contextMenuStrip1; this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon"))); + this.notifyIcon1.Visible = true; this.notifyIcon1.Click += new System.EventHandler(this.notifyIcon1_Click); - // + this.notifyIcon1.DoubleClick += new System.EventHandler(this.notifyIcon1_Click); + // // panel1 - // + // this.panel1.BackColor = System.Drawing.Color.Transparent; this.panel1.Cursor = System.Windows.Forms.Cursors.SizeNS; this.panel1.Location = new System.Drawing.Point(109, 114); @@ -271,9 +279,23 @@ namespace AppLauncher.Windows.Forms this.panel1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseDown); this.panel1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseMove); this.panel1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseUp); - // + // + // contextMenuStrip1 + // + this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.exitToolStripMenuItem}); + this.contextMenuStrip1.Name = "contextMenuStrip1"; + this.contextMenuStrip1.Size = new System.Drawing.Size(94, 26); + // + // exitToolStripMenuItem + // + this.exitToolStripMenuItem.Name = "exitToolStripMenuItem"; + this.exitToolStripMenuItem.Size = new System.Drawing.Size(93, 22); + this.exitToolStripMenuItem.Text = "E&xit"; + this.exitToolStripMenuItem.Click += new System.EventHandler(this.exitToolStripMenuItem_Click); + // // AForm - // + // this.ClientSize = new System.Drawing.Size(421, 321); this.Controls.Add(this.panel1); this.Controls.Add(this.imageBox1); @@ -284,6 +306,7 @@ namespace AppLauncher.Windows.Forms ((System.ComponentModel.ISupportInitialize)(this.imageBox3)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.imageBox2)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.imageBox1)).EndInit(); + this.contextMenuStrip1.ResumeLayout(false); this.ResumeLayout(false); } @@ -294,7 +317,7 @@ namespace AppLauncher.Windows.Forms { //this.WindowState = FormWindowState.Minimized; this.Visible = false; - notifyIcon1.Visible = true; + //notifyIcon1.Visible = true; } } @@ -323,7 +346,7 @@ namespace AppLauncher.Windows.Forms private void notifyIcon1_Click(object sender, EventArgs e) { this.Visible = !this.Visible; - notifyIcon1.Visible = !this.Visible; + //notifyIcon1.Visible = !this.Visible; } private void panel1_MouseDown(object sender, MouseEventArgs e) @@ -348,5 +371,7 @@ namespace AppLauncher.Windows.Forms } } + private void exitToolStripMenuItem_Click(object sender, EventArgs e) => this.Close(); + } } \ No newline at end of file diff --git a/Windows/Forms/AForm.resx b/Windows/Forms/AForm.resx index aec58fb..b723165 100644 --- a/Windows/Forms/AForm.resx +++ b/Windows/Forms/AForm.resx @@ -120,6 +120,9 @@ 17, 17 + + 130, 17 +