-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathWindowHelper.cs
More file actions
198 lines (170 loc) · 6.66 KB
/
WindowHelper.cs
File metadata and controls
198 lines (170 loc) · 6.66 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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using System;
using System.Runtime.InteropServices;
using static ManagedShell.Interop.NativeMethods;
namespace ManagedShell.Common.Helpers
{
public static class WindowHelper
{
public const string TrayWndClass = "Shell_TrayWnd";
public static void ShowWindowBottomMost(IntPtr handle)
{
SetWindowPos(
handle,
(IntPtr)WindowZOrder.HWND_BOTTOM,
0,
0,
0,
0,
(int)SetWindowPosFlags.SWP_NOSIZE | (int)SetWindowPosFlags.SWP_NOMOVE | (int)SetWindowPosFlags.SWP_NOACTIVATE/* | SWP_NOZORDER | SWP_NOOWNERZORDER*/);
}
public static void ShowWindowTopMost(IntPtr handle)
{
SetWindowPos(
handle,
(IntPtr)WindowZOrder.HWND_TOPMOST,
0,
0,
0,
0,
(int)SetWindowPosFlags.SWP_NOSIZE | (int)SetWindowPosFlags.SWP_NOMOVE | (int)SetWindowPosFlags.SWP_SHOWWINDOW/* | (int)SetWindowPosFlags.SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOOWNERZORDER*/);
}
public static void ShowWindowDesktop(IntPtr hwnd)
{
IntPtr desktopHwnd = GetLowestDesktopParentHwnd();
if (desktopHwnd != IntPtr.Zero)
{
IntPtr nextHwnd = GetWindow(desktopHwnd, GetWindow_Cmd.GW_HWNDPREV);
SetWindowPos(
hwnd,
nextHwnd,
0,
0,
0,
0,
(int)SetWindowPosFlags.SWP_NOSIZE | (int)SetWindowPosFlags.SWP_NOMOVE | (int)SetWindowPosFlags.SWP_NOACTIVATE);
}
else
{
ShowWindowBottomMost(hwnd);
}
}
public static IntPtr GetLowestDesktopParentHwnd()
{
IntPtr progmanHwnd = FindWindow("Progman", "Program Manager");
IntPtr desktopHwnd = FindWindowEx(progmanHwnd, IntPtr.Zero, "SHELLDLL_DefView", null);
if (desktopHwnd == IntPtr.Zero)
{
IntPtr workerHwnd = IntPtr.Zero;
IntPtr shellIconsHwnd;
do
{
workerHwnd = FindWindowEx(IntPtr.Zero, workerHwnd, "WorkerW", null);
shellIconsHwnd = FindWindowEx(workerHwnd, IntPtr.Zero, "SHELLDLL_DefView", null);
} while (shellIconsHwnd == IntPtr.Zero && workerHwnd != IntPtr.Zero);
desktopHwnd = workerHwnd;
}
else
{
desktopHwnd = progmanHwnd;
}
return desktopHwnd;
}
public static IntPtr GetLowestDesktopChildHwnd()
{
IntPtr progmanHwnd = FindWindow("Progman", "Program Manager");
IntPtr desktopHwnd = FindWindowEx(progmanHwnd, IntPtr.Zero, "SHELLDLL_DefView", null);
if (desktopHwnd == IntPtr.Zero)
{
IntPtr workerHwnd = IntPtr.Zero;
IntPtr shellIconsHwnd;
do
{
workerHwnd = FindWindowEx(IntPtr.Zero, workerHwnd, "WorkerW", null);
shellIconsHwnd = FindWindowEx(workerHwnd, IntPtr.Zero, "SHELLDLL_DefView", null);
} while (shellIconsHwnd == IntPtr.Zero && workerHwnd != IntPtr.Zero);
desktopHwnd = shellIconsHwnd;
}
return desktopHwnd;
}
public static void HideWindowFromTasks(IntPtr hWnd)
{
SetWindowLong(hWnd, GWL_EXSTYLE, (GetWindowLong(hWnd, GWL_EXSTYLE) & ~(int)ExtendedWindowStyles.WS_EX_APPWINDOW) | (int)ExtendedWindowStyles.WS_EX_TOOLWINDOW);
ExcludeWindowFromPeek(hWnd);
}
public static void ExcludeWindowFromPeek(IntPtr hWnd)
{
int status = 1;
DwmSetWindowAttribute(hWnd,
DWMWINDOWATTRIBUTE.DWMWA_EXCLUDED_FROM_PEEK,
ref status,
sizeof(int));
}
public static void PeekWindow(bool show, IntPtr targetHwnd, IntPtr callingHwnd)
{
uint enable = 0;
if (show) enable = 1;
if (EnvironmentHelper.IsWindows81OrBetter)
{
DwmActivateLivePreview(enable, targetHwnd, callingHwnd, AeroPeekType.Window, IntPtr.Zero);
}
else
{
DwmActivateLivePreview(enable, targetHwnd, callingHwnd, AeroPeekType.Window);
}
}
public static void SetWindowBlur(IntPtr hWnd, bool enable)
{
if (EnvironmentHelper.IsWindows10OrBetter)
{
// https://github.com/riverar/sample-win32-acrylicblur
// License: MIT
var accent = new AccentPolicy();
var accentStructSize = Marshal.SizeOf(accent);
if (enable)
{
if (EnvironmentHelper.IsWindows10RS4OrBetter)
{
accent.AccentState = AccentState.ACCENT_ENABLE_ACRYLICBLURBEHIND;
accent.GradientColor = (0 << 24) | (0xFFFFFF /* BGR */ & 0xFFFFFF);
}
else
{
accent.AccentState = AccentState.ACCENT_ENABLE_BLURBEHIND;
}
}
else
{
accent.AccentState = AccentState.ACCENT_DISABLED;
}
var accentPtr = Marshal.AllocHGlobal(accentStructSize);
Marshal.StructureToPtr(accent, accentPtr, false);
var data = new WindowCompositionAttributeData();
data.Attribute = WindowCompositionAttribute.WCA_ACCENT_POLICY;
data.SizeOfData = accentStructSize;
data.Data = accentPtr;
SetWindowCompositionAttribute(hWnd, ref data);
Marshal.FreeHGlobal(accentPtr);
}
}
public static bool SetDarkModePreference(PreferredAppMode mode)
{
if (EnvironmentHelper.IsWindows10DarkModeSupported)
{
return SetPreferredAppMode(mode);
}
return false;
}
public static IntPtr FindWindowsTray(IntPtr hwndIgnore)
{
IntPtr taskbarHwnd = FindWindow(TrayWndClass, "");
if (hwndIgnore != IntPtr.Zero)
{
while (taskbarHwnd == hwndIgnore)
{
taskbarHwnd = FindWindowEx(IntPtr.Zero, taskbarHwnd, TrayWndClass, "");
}
}
return taskbarHwnd;
}
}
}