-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
57 lines (49 loc) · 1.59 KB
/
Copy pathProgram.cs
File metadata and controls
57 lines (49 loc) · 1.59 KB
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
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using static ActiveWindowTracker.Win32;
var (titlePrev, pidPrev) = GetActiveWindowTitle();
Log("Started listening to active window changes");
while (true)
{
var (title, pid) = GetActiveWindowTitle();
if (pid != pidPrev)
{
Log($"Active program changed to PID {pid}: {title}");
}
else if (title != titlePrev)
{
Log($"Active program title changed to: {title}");
}
titlePrev = title;
pidPrev = pid;
await Task.Delay(TimeSpan.FromMilliseconds(100));
}
static void Log(string text)
{
Console.WriteLine($"[{DateTime.Now:u}] {text}");
}
// adapted from https://stackoverflow.com/a/115905/1780502, https://stackoverflow.com/a/18184700/1780502
static (string, uint) GetActiveWindowTitle()
{
const int nChars = 256;
StringBuilder buff = new(nChars);
var handle = GetForegroundWindow();
var processId = uint.MaxValue;
GetWindowText(handle, buff, nChars);
GetWindowThreadProcessId(handle, out processId);
return (buff.ToString(), processId);
}
namespace ActiveWindowTracker
{
public static class Win32
{
[DllImport("user32.dll", SetLastError = true)]
public static extern void GetWindowThreadProcessId(IntPtr hWnd, out uint processId);
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
}
}