-
Notifications
You must be signed in to change notification settings - Fork 0
/
SystemProcessHookForm.cs
41 lines (37 loc) · 1.26 KB
/
SystemProcessHookForm.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
using System.Diagnostics;
using System.Windows.Forms;
namespace SpotifySongTracker {
class SystemProcessHookForm : Form {
private readonly int msgNotify;
public delegate void EventHandler(object sender, Process Process);
public event EventHandler WindowCreated;
protected virtual void OnWindowCreated(Process process) {
var handler = WindowCreated;
if (handler != null) {
handler(this, process);
}
}
public SystemProcessHookForm() {
// Hook on to the shell
msgNotify = Interop.RegisterWindowMessage("SHELLHOOK");
Interop.RegisterShellHookWindow(this.Handle);
}
protected override void WndProc(ref Message m) {
if (m.Msg == msgNotify) {
// Receive shell messages
var hwnd = m.LParam;
switch ((Interop.ShellEvents)m.WParam.ToInt32()) {
case Interop.ShellEvents.HSHELL_WINDOWCREATED:
Interop.GetWindowThreadProcessId(hwnd, out uint processId);
OnWindowCreated(Process.GetProcessById((int)processId));
break;
}
}
base.WndProc(ref m);
}
protected override void Dispose(bool disposing) {
try { Interop.DeregisterShellHookWindow(this.Handle); } catch { }
base.Dispose(disposing);
}
}
}