Repository navigation
Expand file tree
/
Copy pathDimTest.cs
More file actions
60 lines (51 loc) · 3.25 KB
/
Copy pathDimTest.cs
File metadata and controls
60 lines (51 loc) · 3.25 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
58
59
60
// Drives the running MagicVolume dimmer overlay from outside and checks the
// safety-critical properties: that it is click-through, never takes focus, and
// that raising the level hides it again (so a black screen is always escapable).
using System;
using System.Runtime.InteropServices;
using System.Threading;
class DimTest {
const int GWL_EXSTYLE = -20;
const int WS_EX_LAYERED = 0x00080000, WS_EX_TRANSPARENT = 0x00000020;
const int WS_EX_TOPMOST = 0x00000008, WS_EX_NOACTIVATE = 0x08000000;
const uint WM_APP_SETLEVEL = 0x8001;
const uint LWA_ALPHA = 0x2;
[StructLayout(LayoutKind.Sequential)] struct RECT { public int L, T, R, B; }
[DllImport("user32.dll", CharSet = CharSet.Unicode)] static extern IntPtr FindWindowW(string cls, string title);
[DllImport("user32.dll")] static extern int GetWindowLongW(IntPtr h, int i);
[DllImport("user32.dll")] static extern bool GetWindowRect(IntPtr h, out RECT r);
[DllImport("user32.dll")] static extern bool IsWindowVisible(IntPtr h);
[DllImport("user32.dll")] static extern bool PostMessageW(IntPtr h, uint m, IntPtr w, IntPtr l);
[DllImport("user32.dll")] static extern bool GetLayeredWindowAttributes(IntPtr h, out uint key, out byte alpha, out uint flags);
[DllImport("user32.dll")] static extern int GetSystemMetrics(int i);
static void Main() {
IntPtr h = FindWindowW("MagicVolumeDimmer", null);
if (h == IntPtr.Zero) { Console.WriteLine("FAIL: dimmer window not found - is MagicVolume running?"); return; }
Console.WriteLine("dimmer window found: 0x" + h.ToInt64().ToString("X"));
int ex = GetWindowLongW(h, GWL_EXSTYLE);
Console.WriteLine(" click-through (WS_EX_TRANSPARENT): " + ((ex & WS_EX_TRANSPARENT) != 0));
Console.WriteLine(" never activates (WS_EX_NOACTIVATE): " + ((ex & WS_EX_NOACTIVATE) != 0));
Console.WriteLine(" layered (WS_EX_LAYERED): " + ((ex & WS_EX_LAYERED) != 0));
Console.WriteLine(" topmost (WS_EX_TOPMOST): " + ((ex & WS_EX_TOPMOST) != 0));
RECT r; GetWindowRect(h, out r);
Console.WriteLine(string.Format(" covers {0}x{1} at ({2},{3}); virtual screen is {4}x{5}",
r.R - r.L, r.B - r.T, r.L, r.T, GetSystemMetrics(78), GetSystemMetrics(79)));
Console.WriteLine(" visible at rest: " + IsWindowVisible(h) + " (should be False at 100%)");
Console.WriteLine();
Console.WriteLine("dimming to 50% for a moment...");
PostMessageW(h, WM_APP_SETLEVEL, new IntPtr(50), IntPtr.Zero);
Thread.Sleep(600);
uint key, flags; byte alpha;
bool got = GetLayeredWindowAttributes(h, out key, out alpha, out flags);
Console.WriteLine(" visible: " + IsWindowVisible(h) + " alpha: " + (got ? alpha.ToString() : "?") + "/255 (expect ~127)");
Console.WriteLine("restoring to 100%...");
PostMessageW(h, WM_APP_SETLEVEL, new IntPtr(100), IntPtr.Zero);
Thread.Sleep(600);
bool hidden = !IsWindowVisible(h);
Console.WriteLine(" hidden again: " + hidden);
Console.WriteLine();
Console.WriteLine(hidden && (ex & WS_EX_TRANSPARENT) != 0
? "RESULT: dimmer works and is safely escapable."
: "RESULT: PROBLEM - check the flags above.");
}
}