Added: global hot keys + notify icon

This commit is contained in:
Ray 2020-05-18 20:45:25 +01:00
parent 045b55dbb3
commit 0f03a26a4c
4 changed files with 93 additions and 20 deletions

View File

@ -7,6 +7,7 @@ using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Drawing; using System.Drawing;
using System.IO; using System.IO;
using System.Runtime.InteropServices;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
@ -14,6 +15,18 @@ namespace AppLauncher
{ {
public partial class MainForm : AForm 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 collapsedWidth = 40;
protected int expandedWidth = 800; protected int expandedWidth = 800;
@ -26,6 +39,13 @@ namespace AppLauncher
//this.Visible = false; //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) protected override void OnShown(EventArgs e)
{ {
this.Visible = false; this.Visible = false;
@ -46,6 +66,8 @@ namespace AppLauncher
{ {
base.OnClosing(e); base.OnClosing(e);
UnregisterHotKey((IntPtr)Handle, 1);
if (string.IsNullOrWhiteSpace(sessionFilename)) if (string.IsNullOrWhiteSpace(sessionFilename))
{ {
// do nothing // 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() public async Task ToggleSize()
{ {
if (this.Width > collapsedWidth) if (this.Width > collapsedWidth)

View File

@ -1,22 +1,30 @@
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
namespace AppLauncher namespace AppLauncher
{ {
static class Program static class Program
{ {
const string applicationID = "f31c9f65-4047-41e4-ac85-dbe11d12ee46";
/// <summary> /// <summary>
/// The main entry point for the application. /// The main entry point for the application.
/// </summary> /// </summary>
[STAThread] [STAThread]
static void Main() static void Main()
{
System.Threading.Mutex mutex = new System.Threading.Mutex(false, applicationID);
if (mutex.WaitOne(TimeSpan.Zero))
{ {
Application.EnableVisualStyles(); Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false); Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm()); Application.Run(new MainForm());
} }
mutex.ReleaseMutex();
mutex.Close();
mutex.Dispose();
mutex = null;
}
} }
} }

View File

@ -22,6 +22,8 @@ namespace AppLauncher.Windows.Forms
protected internal RyzStudio.Windows.Forms.TImageBox imageBox1; protected internal RyzStudio.Windows.Forms.TImageBox imageBox1;
private NotifyIcon notifyIcon1; private NotifyIcon notifyIcon1;
private Panel panel1; private Panel panel1;
private ContextMenuStrip contextMenuStrip1;
private ToolStripMenuItem exitToolStripMenuItem;
private IContainer components; private IContainer components;
public AForm() : base() public AForm() : base()
@ -192,9 +194,12 @@ namespace AppLauncher.Windows.Forms
this.imageBox1 = new RyzStudio.Windows.Forms.TImageBox(); this.imageBox1 = new RyzStudio.Windows.Forms.TImageBox();
this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components); this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
this.panel1 = new System.Windows.Forms.Panel(); 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.imageBox3)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.imageBox2)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.imageBox2)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.imageBox1)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.imageBox1)).BeginInit();
this.contextMenuStrip1.SuspendLayout();
this.SuspendLayout(); this.SuspendLayout();
// //
// imageBox3 // imageBox3
@ -256,8 +261,11 @@ namespace AppLauncher.Windows.Forms
// //
// notifyIcon1 // notifyIcon1
// //
this.notifyIcon1.ContextMenuStrip = this.contextMenuStrip1;
this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon"))); 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.Click += new System.EventHandler(this.notifyIcon1_Click);
this.notifyIcon1.DoubleClick += new System.EventHandler(this.notifyIcon1_Click);
// //
// panel1 // panel1
// //
@ -272,6 +280,20 @@ namespace AppLauncher.Windows.Forms
this.panel1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseMove); this.panel1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseMove);
this.panel1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseUp); 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 // AForm
// //
this.ClientSize = new System.Drawing.Size(421, 321); this.ClientSize = new System.Drawing.Size(421, 321);
@ -284,6 +306,7 @@ namespace AppLauncher.Windows.Forms
((System.ComponentModel.ISupportInitialize)(this.imageBox3)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.imageBox3)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.imageBox2)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.imageBox2)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.imageBox1)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.imageBox1)).EndInit();
this.contextMenuStrip1.ResumeLayout(false);
this.ResumeLayout(false); this.ResumeLayout(false);
} }
@ -294,7 +317,7 @@ namespace AppLauncher.Windows.Forms
{ {
//this.WindowState = FormWindowState.Minimized; //this.WindowState = FormWindowState.Minimized;
this.Visible = false; 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) private void notifyIcon1_Click(object sender, EventArgs e)
{ {
this.Visible = !this.Visible; this.Visible = !this.Visible;
notifyIcon1.Visible = !this.Visible; //notifyIcon1.Visible = !this.Visible;
} }
private void panel1_MouseDown(object sender, MouseEventArgs e) 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();
} }
} }

View File

@ -120,6 +120,9 @@
<metadata name="notifyIcon1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"> <metadata name="notifyIcon1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value> <value>17, 17</value>
</metadata> </metadata>
<metadata name="contextMenuStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>130, 17</value>
</metadata>
<assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> <assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<data name="notifyIcon1.Icon" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64"> <data name="notifyIcon1.Icon" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value> <value>