151 lines
6.2 KiB
C#
151 lines
6.2 KiB
C#
using System;
|
|
using System.Runtime.InteropServices;
|
|
|
|
namespace ZNTechClone
|
|
{
|
|
public static class Native
|
|
{
|
|
public const int GWL_EXSTYLE = -20;
|
|
public const int WS_EX_TRANSPARENT = 0x00000020;
|
|
public const int WS_EX_TOOLWINDOW = 0x00000080;
|
|
public const int WS_EX_LAYERED = 0x00080000;
|
|
public const int WS_EX_NOACTIVATE = 0x08000000;
|
|
public const uint WDA_NONE = 0x00000000;
|
|
public const uint WDA_EXCLUDEFROMCAPTURE = 0x00000011;
|
|
|
|
[DllImport("user32.dll", SetLastError = true)]
|
|
public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
|
|
[DllImport("user32.dll", SetLastError = true)]
|
|
public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
|
|
[DllImport("user32.dll", SetLastError = true)]
|
|
public static extern bool SetWindowDisplayAffinity(IntPtr hWnd, uint dwAffinity);
|
|
[DllImport("user32.dll")]
|
|
public static extern short GetAsyncKeyState(int vKey);
|
|
[DllImport("user32.dll")]
|
|
public static extern IntPtr GetForegroundWindow();
|
|
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
|
|
public static extern int GetWindowText(IntPtr hWnd, System.Text.StringBuilder text, int count);
|
|
[DllImport("user32.dll")]
|
|
public static extern int GetWindowThreadProcessId(IntPtr hWnd, out int pid);
|
|
|
|
public static void MakeClickThrough(IntPtr hwnd)
|
|
{
|
|
int ex = GetWindowLong(hwnd, GWL_EXSTYLE);
|
|
SetWindowLong(hwnd, GWL_EXSTYLE, ex | WS_EX_TRANSPARENT | WS_EX_TOOLWINDOW | WS_EX_LAYERED | WS_EX_NOACTIVATE);
|
|
}
|
|
|
|
public static bool HideFromCapture(IntPtr hwnd, bool hide)
|
|
{
|
|
try { return SetWindowDisplayAffinity(hwnd, hide ? WDA_EXCLUDEFROMCAPTURE : WDA_NONE); }
|
|
catch { return false; }
|
|
}
|
|
|
|
public static bool IsKeyDown(int vk) => (GetAsyncKeyState(vk) & 0x8000) != 0;
|
|
|
|
public static string ForegroundTitle()
|
|
{
|
|
var sb = new System.Text.StringBuilder(512);
|
|
GetWindowText(GetForegroundWindow(), sb, sb.Capacity);
|
|
return sb.ToString();
|
|
}
|
|
|
|
// ---- SendInput ----
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
struct INPUT { public uint type; public InputUnion U; }
|
|
[StructLayout(LayoutKind.Explicit)]
|
|
struct InputUnion { [FieldOffset(0)] public MOUSEINPUT mi; [FieldOffset(0)] public KEYBDINPUT ki; [FieldOffset(0)] public HARDWAREINPUT hi; }
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
struct MOUSEINPUT { public int dx, dy; public uint mouseData, dwFlags, time; public IntPtr dwExtraInfo; }
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
struct KEYBDINPUT { public ushort wVk, wScan; public uint dwFlags, time; public IntPtr dwExtraInfo; }
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
struct HARDWAREINPUT { public uint uMsg; public ushort wParamL, wParamH; }
|
|
|
|
const uint INPUT_KEYBOARD = 1, INPUT_MOUSE = 0;
|
|
const uint KEYEVENTF_KEYUP = 0x0002, KEYEVENTF_SCANCODE = 0x0008;
|
|
const uint MOUSEEVENTF_LEFTDOWN = 0x0002, MOUSEEVENTF_LEFTUP = 0x0004,
|
|
MOUSEEVENTF_RIGHTDOWN = 0x0008, MOUSEEVENTF_RIGHTUP = 0x0010;
|
|
|
|
[DllImport("user32.dll", SetLastError = true)]
|
|
static extern uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);
|
|
|
|
public static void KeyDown(int vk)
|
|
{
|
|
var inp = new INPUT[1];
|
|
inp[0].type = INPUT_KEYBOARD;
|
|
inp[0].U.ki = new KEYBDINPUT { wVk = (ushort)vk, wScan = 0, dwFlags = 0 };
|
|
SendInput(1, inp, Marshal.SizeOf<INPUT>());
|
|
}
|
|
|
|
public static void KeyUp(int vk)
|
|
{
|
|
var inp = new INPUT[1];
|
|
inp[0].type = INPUT_KEYBOARD;
|
|
inp[0].U.ki = new KEYBDINPUT { wVk = (ushort)vk, wScan = 0, dwFlags = KEYEVENTF_KEYUP };
|
|
SendInput(1, inp, Marshal.SizeOf<INPUT>());
|
|
}
|
|
|
|
public static void ScanDown(ushort scan)
|
|
{
|
|
var inp = new INPUT[1];
|
|
inp[0].type = INPUT_KEYBOARD;
|
|
inp[0].U.ki = new KEYBDINPUT { wVk = 0, wScan = scan, dwFlags = KEYEVENTF_SCANCODE };
|
|
SendInput(1, inp, Marshal.SizeOf<INPUT>());
|
|
}
|
|
|
|
public static void ScanUp(ushort scan)
|
|
{
|
|
var inp = new INPUT[1];
|
|
inp[0].type = INPUT_KEYBOARD;
|
|
inp[0].U.ki = new KEYBDINPUT { wVk = 0, wScan = scan, dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP };
|
|
SendInput(1, inp, Marshal.SizeOf<INPUT>());
|
|
}
|
|
|
|
public static void MouseButton(bool left, bool down)
|
|
{
|
|
var inp = new INPUT[1];
|
|
inp[0].type = INPUT_MOUSE;
|
|
inp[0].U.mi = new MOUSEINPUT
|
|
{
|
|
dwFlags = left ? (down ? MOUSEEVENTF_LEFTDOWN : MOUSEEVENTF_LEFTUP)
|
|
: (down ? MOUSEEVENTF_RIGHTDOWN : MOUSEEVENTF_RIGHTUP)
|
|
};
|
|
SendInput(1, inp, Marshal.SizeOf<INPUT>());
|
|
}
|
|
|
|
public static ushort ScanCode(int vk)
|
|
{
|
|
// 常用键扫描码(与 VK 一致的部分键位)
|
|
return vk switch
|
|
{
|
|
0x20 => 0x39, // space
|
|
0x11 => 0x1D, // ctrl
|
|
0x10 => 0x2A, // shift
|
|
0x77 => 0x3F, // F9
|
|
0x76 => 0x40, // F10
|
|
0x41 => 0x1E, 0x44 => 0x20, 0x53 => 0x1F, 0x57 => 0x11,
|
|
0x51 => 0x10, 0x45 => 0x12, 0x52 => 0x13,
|
|
_ => 0
|
|
};
|
|
}
|
|
}
|
|
|
|
public static class VkNames
|
|
{
|
|
public static readonly (int vk, string name)[] All =
|
|
{
|
|
(0x20,"Space"),(0x11,"Ctrl"),(0x10,"Shift"),(0x41,"A"),(0x44,"D"),(0x53,"S"),(0x57,"W"),
|
|
(0x51,"Q"),(0x45,"E"),(0x52,"R"),(0x46,"F"),(0x56,"V"),(0x43,"C"),(0x58,"X"),(0x5A,"Z"),
|
|
(0x76,"F10"),(0x77,"F9"),(0x78,"F11"),(0x79,"F12"),(0x70,"F1"),(0x71,"F2"),(0x72,"F3"),
|
|
(0x01,"鼠标左键"),(0x02,"鼠标右键"),(0x04,"鼠标中键"),(0x05,"侧键 4"),(0x06,"侧键 5"),
|
|
(0x09,"Tab"),(0x12,"Alt"),(0x1B,"Esc")
|
|
};
|
|
|
|
public static string Name(int vk)
|
|
{
|
|
foreach (var (v, n) in All) if (v == vk) return n;
|
|
return "VK" + vk;
|
|
}
|
|
}
|
|
}
|