-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindowsAPI.cs
More file actions
135 lines (110 loc) · 4.58 KB
/
Copy pathWindowsAPI.cs
File metadata and controls
135 lines (110 loc) · 4.58 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
using System;
using System.Runtime.InteropServices;
using System.Text;
namespace WindowTopMost
{
public static class WindowsAPI
{
// 窗口置顶相关常量
public const int HWND_TOPMOST = -1;
public const int HWND_NOTOPMOST = -2;
public const uint SWP_NOMOVE = 0x0002;
public const uint SWP_NOSIZE = 0x0001;
public const uint SWP_SHOWWINDOW = 0x0040;
public const uint SWP_NOACTIVATE = 0x0010;
// 热键相关常量
public const int WM_HOTKEY = 0x0312;
public const int MOD_ALT = 0x0001;
public const int MOD_CONTROL = 0x0002;
public const int MOD_SHIFT = 0x0004;
public const int MOD_WIN = 0x0008;
// 获取窗口信息相关常量
public const int GWL_EXSTYLE = -20;
public const int WS_EX_TOPMOST = 0x00000008;
public const int WS_EX_LAYERED = 0x00080000;
// 透明度相关常量
public const int GWL_EXSTYLE_LAYERED = -20;
public const int LWA_ALPHA = 0x00000002;
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
public static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
[DllImport("user32.dll")]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
[DllImport("user32.dll")]
public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
[DllImport("user32.dll")]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll")]
public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
public static extern bool IsWindow(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll")]
public static extern bool SetLayeredWindowAttributes(IntPtr hWnd, uint crKey, byte bAlpha, uint dwFlags);
/// <summary>
/// 获取窗口标题
/// </summary>
public static string GetWindowTitle(IntPtr hWnd)
{
StringBuilder sb = new StringBuilder(256);
GetWindowText(hWnd, sb, sb.Capacity);
return sb.ToString();
}
/// <summary>
/// 检查窗口是否置顶
/// </summary>
public static bool IsWindowTopMost(IntPtr hWnd)
{
int exStyle = GetWindowLong(hWnd, GWL_EXSTYLE);
return (exStyle & WS_EX_TOPMOST) != 0;
}
/// <summary>
/// 设置窗口置顶状态
/// </summary>
public static bool SetWindowTopMost(IntPtr hWnd, bool topMost)
{
if (!IsWindow(hWnd)) return false;
int insertAfter = topMost ? HWND_TOPMOST : HWND_NOTOPMOST;
return SetWindowPos(hWnd, insertAfter, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
}
/// <summary>
/// 设置窗口透明度
/// </summary>
/// <param name="hWnd">窗口句柄</param>
/// <param name="opacity">透明度 (0-255, 0为完全透明,255为完全不透明)</param>
/// <returns>设置是否成功</returns>
public static bool SetWindowOpacity(IntPtr hWnd, byte opacity)
{
if (!IsWindow(hWnd)) return false;
// 获取当前窗口样式
int exStyle = GetWindowLong(hWnd, GWL_EXSTYLE);
// 添加分层窗口样式
if ((exStyle & WS_EX_LAYERED) == 0)
{
SetWindowLong(hWnd, GWL_EXSTYLE, exStyle | WS_EX_LAYERED);
}
// 设置透明度
return SetLayeredWindowAttributes(hWnd, 0, opacity, LWA_ALPHA);
}
/// <summary>
/// 移除窗口透明度效果
/// </summary>
/// <param name="hWnd">窗口句柄</param>
/// <returns>移除是否成功</returns>
public static bool RemoveWindowOpacity(IntPtr hWnd)
{
if (!IsWindow(hWnd)) return false;
// 获取当前窗口样式
int exStyle = GetWindowLong(hWnd, GWL_EXSTYLE);
// 移除分层窗口样式
if ((exStyle & WS_EX_LAYERED) != 0)
{
SetWindowLong(hWnd, GWL_EXSTYLE, exStyle & ~WS_EX_LAYERED);
return true;
}
return false;
}
}
}