This repository was archived by the owner on Sep 19, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathNativeMethods.cs
More file actions
141 lines (108 loc) · 5.33 KB
/
Copy pathNativeMethods.cs
File metadata and controls
141 lines (108 loc) · 5.33 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
// WindowCenter — 快捷键居中 Windows 窗口
// Copyright (C) 2026 WindowCenter contributors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
using System.Runtime.InteropServices;
namespace WindowCenter;
/// <summary>Windows API P/Invoke 声明</summary>
internal static partial class NativeMethods
{
// ─── 常量 ───────────────────────────────────────
public const int MOD_ALT = 0x0001;
public const int MOD_CONTROL = 0x0002;
public const int MOD_NOREPEAT = 0x4000;
public const int MOD_SHIFT = 0x0004;
public const int MOD_WIN = 0x0008;
public const int WM_HOTKEY = 0x0312;
public const int WM_USER = 0x0400;
public const int WM_KEYDOWN = 0x0100;
public const int WH_KEYBOARD_LL = 13;
public const int VK_LCONTROL = 0xA2;
public const int VK_RCONTROL = 0xA3;
public const int VK_LMENU = 0xA4;
public const int VK_RMENU = 0xA5;
public const int VK_LSHIFT = 0xA0;
public const int VK_RSHIFT = 0xA1;
public const int VK_LWIN = 0x5B;
public const int VK_RWIN = 0x5C;
public const uint SWP_NOSIZE = 0x0001;
public const uint SWP_NOZORDER = 0x0004;
public const uint SWP_NOACTIVATE = 0x0010;
public const uint SWP_SHOWWINDOW = 0x0040;
public const int SW_RESTORE = 9;
public const int SW_SHOWMINIMIZED = 2;
public const int SW_MAXIMIZE = 3;
public const uint MONITOR_DEFAULTTONEAREST = 2;
public const int GWL_STYLE = -16;
public const long WS_MAXIMIZE = 0x01000000;
public const long WS_MINIMIZE = 0x20000000;
// ─── 结构体 ─────────────────────────────────────
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left, Top, Right, Bottom;
public int Width => Right - Left;
public int Height => Bottom - Top;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct MONITORINFO
{
public int cbSize;
public RECT rcMonitor; // 显示器完整区域
public RECT rcWork; // 工作区域(排除任务栏)
public uint dwFlags;
}
// ─── user32.dll ────────────────────────────────
[LibraryImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static partial bool RegisterHotKey(nint hWnd, int id, uint fsModifiers, uint vk);
[LibraryImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static partial bool UnregisterHotKey(nint hWnd, int id);
[LibraryImport("user32.dll")]
public static partial nint GetForegroundWindow();
[LibraryImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static partial bool GetWindowRect(nint hWnd, out RECT lpRect);
[LibraryImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static partial bool SetWindowPos(nint hWnd, nint hWndInsertAfter,
int X, int Y, int cx, int cy, uint uFlags);
[LibraryImport("user32.dll")]
public static partial nint MonitorFromWindow(nint hWnd, uint dwFlags);
[LibraryImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static partial bool GetMonitorInfoW(nint hMonitor, ref MONITORINFO lpmi);
[LibraryImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static partial bool ShowWindow(nint hWnd, int nCmdShow);
[LibraryImport("user32.dll")]
public static partial long GetWindowLongPtrW(nint hWnd, int nIndex);
// ─── kernel32.dll ─────────────────────────────
[LibraryImport("kernel32.dll", SetLastError = true)]
public static partial nint GetConsoleWindow();
[LibraryImport("kernel32.dll", SetLastError = true)]
public static partial nint GetModuleHandleW(nint lpModuleName);
// ─── 键盘钩子 ─────────────────────────────────
public delegate nint HookProc(int nCode, nint wParam, nint lParam);
[LibraryImport("user32.dll", SetLastError = true)]
public static partial nint SetWindowsHookExW(int idHook, HookProc lpfn, nint hMod, uint dwThreadId);
[LibraryImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static partial bool UnhookWindowsHookEx(nint hhk);
[LibraryImport("user32.dll")]
public static partial nint CallNextHookEx(nint hhk, int nCode, nint wParam, nint lParam);
[LibraryImport("user32.dll")]
public static partial short GetAsyncKeyState(int vKey);
}