193 lines
6.5 KiB
C#
193 lines
6.5 KiB
C#
using System.Runtime.InteropServices;
|
|
using System.Drawing;
|
|
|
|
namespace anjianjingling.Native
|
|
{
|
|
/// <summary>
|
|
/// Win32 API 统一封装:SendInput、窗口、热键、消息。
|
|
/// </summary>
|
|
internal static class Win32
|
|
{
|
|
#region SendInput 结构与常量
|
|
|
|
public const uint INPUT_MOUSE = 0;
|
|
public const uint INPUT_KEYBOARD = 1;
|
|
|
|
public const uint MOUSEEVENTF_MOVE = 0x0001;
|
|
public const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
|
|
public const uint MOUSEEVENTF_LEFTUP = 0x0004;
|
|
public const uint MOUSEEVENTF_RIGHTDOWN = 0x0008;
|
|
public const uint MOUSEEVENTF_RIGHTUP = 0x0010;
|
|
public const uint MOUSEEVENTF_MIDDLEDOWN = 0x0020;
|
|
public const uint MOUSEEVENTF_MIDDLEUP = 0x0040;
|
|
public const uint MOUSEEVENTF_WHEEL = 0x0800;
|
|
public const uint MOUSEEVENTF_ABSOLUTE = 0x8000;
|
|
public const uint MOUSEEVENTF_VIRTUALDESK = 0x4000;
|
|
public const int WHEEL_DELTA = 120;
|
|
|
|
public const uint KEYEVENTF_EXTENDEDKEY = 0x0001;
|
|
public const uint KEYEVENTF_KEYUP = 0x0002;
|
|
public const uint KEYEVENTF_UNICODE = 0x0004;
|
|
public const uint KEYEVENTF_SCANCODE = 0x0008;
|
|
|
|
public const int SM_CXVIRTUALSCREEN = 78;
|
|
public const int SM_CYVIRTUALSCREEN = 79;
|
|
public const int SM_XVIRTUALSCREEN = 76;
|
|
public const int SM_YVIRTUALSCREEN = 77;
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
public struct MOUSEINPUT
|
|
{
|
|
public int dx;
|
|
public int dy;
|
|
public uint mouseData;
|
|
public uint dwFlags;
|
|
public uint time;
|
|
public IntPtr dwExtraInfo;
|
|
}
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
public struct KEYBDINPUT
|
|
{
|
|
public ushort wVk;
|
|
public ushort wScan;
|
|
public uint dwFlags;
|
|
public uint time;
|
|
public IntPtr dwExtraInfo;
|
|
}
|
|
|
|
[StructLayout(LayoutKind.Explicit)]
|
|
public struct INPUTUNION
|
|
{
|
|
[FieldOffset(0)] public MOUSEINPUT mi;
|
|
[FieldOffset(0)] public KEYBDINPUT ki;
|
|
}
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
public struct INPUT
|
|
{
|
|
public uint type;
|
|
public INPUTUNION u;
|
|
}
|
|
|
|
[DllImport("user32.dll", SetLastError = true)]
|
|
public static extern uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);
|
|
|
|
[DllImport("user32.dll")]
|
|
public static extern bool GetCursorPos(out Point lpPoint);
|
|
|
|
[DllImport("user32.dll")]
|
|
public static extern bool SetCursorPos(int x, int y);
|
|
|
|
[DllImport("user32.dll")]
|
|
public static extern int GetSystemMetrics(int nIndex);
|
|
|
|
[DllImport("user32.dll")]
|
|
public static extern uint MapVirtualKey(uint uCode, uint uMapType);
|
|
public const uint MAPVK_VK_TO_VSC = 0;
|
|
|
|
#endregion
|
|
|
|
#region 热键
|
|
|
|
public const int WM_HOTKEY = 0x0312;
|
|
public const uint MOD_ALT = 0x0001;
|
|
public const uint MOD_CONTROL = 0x0002;
|
|
public const uint MOD_SHIFT = 0x0004;
|
|
public const uint MOD_NOREPEAT = 0x4000;
|
|
|
|
[DllImport("user32.dll", SetLastError = true)]
|
|
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);
|
|
|
|
#endregion
|
|
|
|
#region 窗口
|
|
|
|
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.Unicode)]
|
|
public static extern int GetWindowText(IntPtr hWnd, System.Text.StringBuilder lpString, int nMaxCount);
|
|
|
|
[DllImport("user32.dll")]
|
|
public static extern int GetWindowTextLength(IntPtr hWnd);
|
|
|
|
[DllImport("user32.dll")]
|
|
public static extern bool IsWindowVisible(IntPtr hWnd);
|
|
|
|
[DllImport("user32.dll")]
|
|
public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
|
|
|
|
[DllImport("user32.dll")]
|
|
public static extern bool GetClientRect(IntPtr hWnd, out RECT lpRect);
|
|
|
|
[DllImport("user32.dll")]
|
|
public static extern bool SetForegroundWindow(IntPtr hWnd);
|
|
|
|
[DllImport("user32.dll")]
|
|
public static extern IntPtr GetForegroundWindow();
|
|
|
|
[DllImport("user32.dll")]
|
|
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
|
|
|
|
[DllImport("user32.dll")]
|
|
public static extern bool IsIconic(IntPtr hWnd);
|
|
|
|
[DllImport("user32.dll")]
|
|
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
|
|
|
|
[DllImport("user32.dll")]
|
|
public static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach);
|
|
|
|
[DllImport("kernel32.dll")]
|
|
public static extern uint GetCurrentThreadId();
|
|
|
|
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
|
|
public static extern IntPtr FindWindow(string? lpClassName, string lpWindowName);
|
|
|
|
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
|
|
public static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
|
|
|
|
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
|
|
public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
|
|
|
|
[DllImport("user32.dll")]
|
|
public static extern bool ScreenToClient(IntPtr hWnd, ref Point lpPoint);
|
|
|
|
[DllImport("user32.dll")]
|
|
public static extern bool PrintWindow(IntPtr hWnd, IntPtr hdcBlt, uint nFlags);
|
|
public const uint PW_RENDERFULLCONTENT = 2;
|
|
|
|
public const int SW_RESTORE = 9;
|
|
public const int SW_SHOW = 5;
|
|
|
|
// 窗口消息
|
|
public const uint WM_LBUTTONDOWN = 0x0201;
|
|
public const uint WM_LBUTTONUP = 0x0202;
|
|
public const uint WM_RBUTTONDOWN = 0x0204;
|
|
public const uint WM_RBUTTONUP = 0x0205;
|
|
public const uint WM_KEYDOWN = 0x0100;
|
|
public const uint WM_KEYUP = 0x0101;
|
|
public const uint WM_CHAR = 0x0102;
|
|
public const IntPtr MK_LBUTTON = 0x0001;
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
public struct RECT
|
|
{
|
|
public int Left, Top, Right, Bottom;
|
|
public int Width => Right - Left;
|
|
public int Height => Bottom - Top;
|
|
}
|
|
|
|
public static IntPtr MakeLParam(int low, int high)
|
|
=> (IntPtr)((high << 16) | (low & 0xFFFF));
|
|
|
|
#endregion
|
|
}
|
|
}
|