-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNative.cs
74 lines (61 loc) · 2.42 KB
/
Native.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
namespace WindowOpacity;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
public static class Native {
private const int GwlExStyle = -20;
private const int WsExLayered = 0x80000;
private const int LwaAlpha = 2;
/// <summary>
/// Find processes matching given names.
/// </summary>
/// <param name="processNames"></param>
/// <returns></returns>
public static Process[] FindProcesses(string[] processNames) {
var processes = Process.GetProcesses().Where(p => {
if (p.MainWindowHandle == IntPtr.Zero) {
return false;
}
foreach (var name in processNames) {
if (Regex.IsMatch(p.ProcessName, name)) {
return true;
}
}
return false;
});
return processes.ToArray();
}
/// <summary>
/// Make window transparent.
/// </summary>
/// <param name="windowHandleList"></param>
/// <param name="opacity"></param>
public static void SetWindowsOpacity(Process[] windowHandleList, byte opacity) {
if (windowHandleList.Length == 0) {
Console.WriteLine("No processes found.");
return;
}
if (opacity < 40 || opacity > 255) {
Console.WriteLine("Opacity should be 40-255.");
return;
}
foreach (var process in windowHandleList) {
var hwnd = process.MainWindowHandle;
var windowLong = GetWindowLong(hwnd, GwlExStyle);
_ = SetWindowLong(hwnd, GwlExStyle, windowLong | WsExLayered);
var ok = SetLayeredWindowAttributes(hwnd, 0, opacity, LwaAlpha);
if (ok) {
Console.WriteLine($"SetLayeredWindowAttributes {process.ProcessName} succeeded.");
}
else {
Console.WriteLine($"SetLayeredWindowAttributes {process.ProcessName} failed.");
}
}
}
[DllImport("user32.dll", EntryPoint = "GetWindowLongA", SetLastError = true)]
private static extern int GetWindowLong(IntPtr hwnd, int nIndex);
[DllImport("user32.dll", EntryPoint = "SetWindowLongA", SetLastError = true)]
private static extern int SetWindowLong(IntPtr hwnd, int nIndex, int dwNewLong);
[DllImport("user32.dll")]
private static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags);
}