-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNative.cs
More file actions
229 lines (174 loc) · 9.13 KB
/
Copy pathNative.cs
File metadata and controls
229 lines (174 loc) · 9.13 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
namespace WindowTinter
{
/// <summary>
/// 全部 Win32 P/Invoke 声明集中在此。
/// </summary>
internal static class Native
{
// ---- 窗口扩展样式 ----
public const int GWL_EXSTYLE = -20;
public const int WS_EX_LAYERED = 0x80000;
public const int WS_EX_TOPMOST = 0x8;
public const int WS_EX_TRANSPARENT = 0x20;
// ---- SetWindowPos ----
public static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
public const uint SWP_NOACTIVATE = 0x0010;
public const uint SWP_SHOWWINDOW = 0x0040;
public const uint SWP_HIDEWINDOW = 0x0080;
// ---- 分层窗口属性 ----
public const int LWA_ALPHA = 0x2;
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left, Top, Right, Bottom;
public int Width => Right - Left;
public int Height => Bottom - Top;
}
// ---- user32:窗口枚举 / 文本 / 进程 ----
public delegate bool EnumWindowsProc(IntPtr hwnd, IntPtr lParam);
[DllImport("user32.dll")]
public static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll")]
public static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
[DllImport("user32.dll")]
public static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern bool IsIconic(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern bool IsWindow(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
// ---- user32:几何 / 样式 ----
[DllImport("user32.dll")]
public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
[DllImport("user32.dll")]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
[DllImport("user32.dll", EntryPoint = "GetWindowLongPtrW")]
public static extern IntPtr GetWindowLongPtr(IntPtr hWnd, int nIndex);
[DllImport("user32.dll", EntryPoint = "SetWindowLongPtrW")]
public static extern IntPtr SetWindowLongPtr(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
[DllImport("user32.dll")]
public static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags);
// ---- user32:UpdateLayeredWindow(逐像素 alpha 合成)----
public const uint ULW_ALPHA = 0x00000002;
public const byte AC_SRC_ALPHA = 1;
[StructLayout(LayoutKind.Sequential)]
public struct BLENDFUNCTION
{
public byte BlendOp;
public byte BlendFlags;
public byte SourceConstantAlpha;
public byte AlphaFormat;
}
[DllImport("user32.dll")]
public static extern bool UpdateLayeredWindow(
IntPtr hWnd, IntPtr hdcDst,
ref Point pptDst, ref Size psize,
IntPtr hdcSrc, ref Point pptSrc,
uint crKey, ref BLENDFUNCTION pblend, uint dwFlags);
// ---- gdi32:DC / 位图管理 ----
[DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleDC(IntPtr hdc);
[DllImport("gdi32.dll")]
public static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj);
[DllImport("gdi32.dll")]
public static extern bool DeleteDC(IntPtr hdc);
[DllImport("user32.dll")]
public static extern IntPtr GetDC(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
// ---- user32:窗口 DC(拾取器高亮边框用)----
[DllImport("user32.dll")]
public static extern IntPtr GetWindowDC(IntPtr hWnd);
// ---- gdi32:PatBlt(反转绘制,拾取器高亮边框)----
public const uint DSTINVERT = 0x00550009;
[DllImport("gdi32.dll")]
public static extern bool PatBlt(IntPtr hdc, int nXLeft, int nYLeft, int nWidth, int nHeight, uint dwRop);
[DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr hObject);
// ---- gdi32:区域创建(圆角矩形裁剪用)----
[DllImport("gdi32.dll")]
public static extern IntPtr CreateRoundRectRgn(int x1, int y1, int x2, int y2, int cx, int cy);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateRectRgn(int x1, int y1, int x2, int y2);
// ---- user32:设置窗口区域 ----
[DllImport("user32.dll")]
public static extern int SetWindowRgn(IntPtr hWnd, IntPtr hRgn, bool bRedraw);
// ---- dwmapi:标题栏深色模式 (Windows 10 2004+) ----
public const int DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
[DllImport("dwmapi.dll")]
public static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int pvAttr, int cbAttr);
// ---- dwmapi:扩展框架边界(排除 DWM 阴影后的真实可见矩形)----
public const int DWMWA_EXTENDED_FRAME_BOUNDS = 9;
[DllImport("dwmapi.dll")]
public static extern int DwmGetWindowAttribute(IntPtr hwnd, int dwAttribute, out RECT pvAttribute, int cbAttribute);
/// <summary>取真实可见矩形(排除 DWM 阴影),失败时回退 GetWindowRect。</summary>
public static RECT GetVisibleWindowRect(IntPtr hwnd)
{
var r = default(RECT);
// DwmGetWindowAttribute 仅在 Windows Vista+ 可用,我们的 target 是 net6-windows 没问题
int hr = DwmGetWindowAttribute(hwnd, DWMWA_EXTENDED_FRAME_BOUNDS, out r, Marshal.SizeOf<RECT>());
if (hr != 0 && !GetWindowRect(hwnd, out r))
r = default; // 双重回退均失败,返回全零(调用方有 w<=0 防护)
return r;
}
// ---- user32:拾取窗口 ----
[DllImport("user32.dll")]
public static extern IntPtr GetAncestor(IntPtr hwnd, uint gaFlags);
public const uint GA_ROOT = 2;
// ---- user32:WinEvent 钩子(事件驱动更新,替代高频轮询)----
public const uint WINEVENT_OUTOFCONTEXT = 0x0000;
public const uint WINEVENT_SKIPOWNPROCESS = 0x0002;
public const uint EVENT_OBJECT_LOCATIONCHANGE = 0x800B;
public const uint EVENT_OBJECT_HIDE = 0x8004;
public const uint EVENT_OBJECT_SHOW = 0x8006;
public const uint EVENT_OBJECT_DESTROY = 0x8001;
public const uint EVENT_SYSTEM_FOREGROUND = 0x0003;
public delegate void WinEventProc(
IntPtr hWinEventHook, uint eventType, IntPtr hwnd,
int idObject, int idChild, uint dwEventThread, uint dwmsEventTime);
[DllImport("user32.dll")]
public static extern IntPtr SetWinEventHook(
uint eventMin, uint eventMax, IntPtr hmodWinEventProc,
WinEventProc pfnWinEventProc, uint idProcess, uint idThread, uint dwFlags);
[DllImport("user32.dll")]
public static extern bool UnhookWinEvent(IntPtr hWinEventHook);
// ---- uxtheme:深色滚动条 ----
[DllImport("uxtheme.dll", CharSet = CharSet.Unicode)]
public static extern int SetWindowTheme(IntPtr hwnd, string pszSubAppName, string pszSubIdList);
// ---- user32:重绘 ----
[DllImport("user32.dll")]
public static extern bool InvalidateRect(IntPtr hWnd, IntPtr lpRect, bool bErase);
[DllImport("user32.dll")]
public static extern bool RedrawWindow(IntPtr hWnd, IntPtr lprcUpdate, IntPtr hrgnUpdate, uint flags);
public const uint RDW_INVALIDATE = 0x0001;
public const uint RDW_ERASE = 0x0004;
public const uint RDW_FRAME = 0x0400;
public const uint RDW_ALLCHILDREN = 0x0080;
// ---- 提权检测:目标窗口以管理员运行、本程序非提权时无法修改其透明度 ----
public const uint PROCESS_QUERY_INFORMATION = 0x0400;
public const uint TOKEN_QUERY = 0x0008;
[StructLayout(LayoutKind.Sequential)]
public struct TOKEN_ELEVATION
{
public uint TokenIsElevated;
}
[DllImport("advapi32.dll")]
public static extern bool OpenProcessToken(IntPtr processHandle, uint desiredAccess, out IntPtr tokenHandle);
[DllImport("advapi32.dll")]
public static extern bool GetTokenInformation(IntPtr tokenHandle, int tokenInformationClass,
out TOKEN_ELEVATION tokenInformation, uint tokenInformationLength, out uint returnLength);
[DllImport("kernel32.dll")]
public static extern IntPtr OpenProcess(uint desiredAccess, bool inheritHandle, uint processId);
[DllImport("kernel32.dll")]
public static extern bool CloseHandle(IntPtr hObject);
}
}