-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathSetTransparency.cs
98 lines (80 loc) · 2.66 KB
/
SetTransparency.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
using System;
using System.Runtime.InteropServices;
using System.Diagnostics;
using Windows;
namespace GlassIt
{
public static class SetTransParency
{
public static bool SetTransparency(int pid, byte alpha)
{
Process mainproc = Process.GetProcessById(pid);
foreach (Process proc in Process.GetProcessesByName(mainproc.ProcessName))
{
if (proc.StartInfo.FileName != mainproc.StartInfo.FileName)
{
continue;
}
IntPtr hMainWnd = proc.MainWindowHandle;
if (hMainWnd == IntPtr.Zero)
{
continue;
}
uint tid = User32.GetWindowThreadProcessId(hMainWnd, out pid);
bool result = User32.EnumThreadWindows(tid, delegate(IntPtr hWnd, IntPtr lParam) {
if (!User32.IsWindowVisible(hWnd))
{
return true;
}
WS windowLong = User32.GetWindowLong(hWnd, GWL.EXSTYLE);
User32.SetWindowLong(hWnd, GWL.EXSTYLE, windowLong | WS.EX_LAYERED);
return User32.SetLayeredWindowAttributes(hWnd, 0, alpha, LWA.ALPHA);
}, IntPtr.Zero);
if (!result)
{
return false;
}
}
return true;
}
}
}
namespace Windows
{
internal static class User32
{
public delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
[DllImport("user32.dll")]
public static extern bool EnumThreadWindows(uint dwThreadId, EnumWindowsProc lpEnumFunc, IntPtr lParam);
[DllImport("user32.dll")]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);
[DllImport("user32.dll")]
public static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern WS GetWindowLong(IntPtr hWnd, GWL nIndex);
[DllImport("user32.dll")]
public static extern int SetWindowLong(IntPtr hWnd, GWL nIndex, WS dwNewLong);
[DllImport("user32.dll")]
public static extern bool SetLayeredWindowAttributes(IntPtr hWnd, uint crKey, byte bAlpha, LWA dwFlags);
}
internal enum GWL: int
{
EXSTYLE = -20,
HINSTANCE = -6,
HWNDPARENT = -8,
ID = -12,
STYLE = -16,
USERDATA = -21,
WNDPROC = -4,
}
[Flags]
internal enum WS: int
{
EX_LAYERED = 0x80000,
}
internal enum LWA: int
{
COLORKEY = 1,
ALPHA = 2,
}
}