-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTrayApp.cs
111 lines (99 loc) · 3.62 KB
/
TrayApp.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using System;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using WindowsTermialTray.Keys;
namespace WindowsTermialTray
{
public class TrayApp : IDisposable
{
// Invisible window to listen for hotkey and firing a hook
private readonly KeyboardHook _hook = new KeyboardHook();
// Hotkeys vars
private ModifierKeys _mk;
private System.Windows.Forms.Keys _k;
// Process vars
private Process _appProcess = null;
private readonly string _processName;
private readonly string _exec;
// Window vars
private const int SW_HIDE = 0;
private const int SW_MAXIMIZE = 3;
private const int SW_SHOW = 5;
private const int SW_MINIMIZE = 6;
private bool hidden = false;
[DllImport("User32")]
private static extern int ShowWindow(int hwnd, int nCmdShow);
[DllImport("User32")]
static extern bool SetForegroundWindow(IntPtr hWnd);
/// <summary>
/// Create new instance of an app, that will be hided to tray
/// </summary>
/// <param name="processName">Proper ProcessName.</param>
/// <param name="exec">Executable to run if app doesn't exist yet as an process.</param>
/// <param name="mk">Modifier key to be pressed to fire an event.</param>
/// <param name="k">Key with modifier that fires the event.</param>
public TrayApp(string processName, string exec, ModifierKeys mk, System.Windows.Forms.Keys k)
{
_processName = processName;
_exec = exec;
_mk = mk;
_k = k;
_hook.KeyPressed += HotKeyPressed;
_hook.RegisterHotKey(_mk, _k);
}
public void HotKeyPressed(object sender, KeyPressedEventArgs e)
{
if (_appProcess == null || _appProcess.HasExited)
{
RunApp();
return;
}
if (hidden)
{
ShowWindow(_appProcess.MainWindowHandle.ToInt32(), SW_MAXIMIZE);
SetForegroundWindow(_appProcess.MainWindowHandle);
}
else
{
ShowWindow(_appProcess.MainWindowHandle.ToInt32(), SW_MINIMIZE);
ShowWindow(_appProcess.MainWindowHandle.ToInt32(), SW_HIDE);
}
hidden = !hidden;
}
private void RunApp()
{
_appProcess = Process.GetProcesses().FirstOrDefault(x => x.ProcessName.Equals(_processName) && !x.HasExited);
if (_appProcess != null && _appProcess.MainWindowHandle.ToInt32() == 0)
{
_appProcess.Kill();
_appProcess = null;
}
if (_appProcess == null)
{
Process process = new Process();
process.StartInfo.FileName = _exec;
process.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
process.Start();
while (_appProcess == null)
{
_appProcess = Process.GetProcesses().FirstOrDefault(x => x.ProcessName.Equals(_processName) && !x.HasExited);
Thread.Sleep(100);
}
};
}
public string GetKeys()
{
return $"{_processName}: {_mk.ToString()} + {_k.ToString()}\n";
}
public void Dispose()
{
if (_appProcess != null)
{
ShowWindow(_appProcess.MainWindowHandle.ToInt32(), SW_SHOW);
}
_hook.Dispose();
}
}
}