Magpie/CursorHook/NativeMethods.cs
2021-03-17 21:18:39 +08:00

164 lines
5.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
namespace Magpie.CursorHook {
// Win32 API
public static class NativeMethods {
[DllImport("user32.dll")]
public static extern IntPtr SetCursor(IntPtr hCursor);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr LoadCursor(IntPtr hInstance, IntPtr lpCursorName);
public readonly static IntPtr IDC_ARROW = new IntPtr(32512);
public readonly static IntPtr IDC_HAND = new IntPtr(32649);
public readonly static IntPtr IDC_APPSTARTING = new IntPtr(32650);
public readonly static int GCLP_HCURSOR = -12;
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
private static extern IntPtr GetClassLongPtr(IntPtr hWnd, int nIndex);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
private static extern int GetClassLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
private static extern IntPtr SetClassLongPtr(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
private static extern int SetClassLong(IntPtr hWnd, int nIndex, int dwNewLong);
public static IntPtr GetClassLongAuto(IntPtr hWnd, int nIndex) {
if (EasyHook.NativeAPI.Is64Bit) {
return GetClassLongPtr(hWnd, nIndex);
} else {
return new IntPtr(GetClassLong(hWnd, nIndex));
}
}
public static IntPtr SetClassLongAuto(IntPtr hWnd, int nIndex, IntPtr dwNewLong) {
if (EasyHook.NativeAPI.Is64Bit) {
return SetClassLongPtr(hWnd, nIndex, dwNewLong);
} else {
return new IntPtr(SetClassLong(hWnd, nIndex, dwNewLong.ToInt32()));
}
}
public delegate bool EnumWindowsProc(IntPtr hwnd, int lParam);
[DllImport("user32.dll")]
public static extern bool EnumChildWindows(IntPtr hWndParent, EnumWindowsProc lpEnumFunc, int lParam);
[DllImport("user32.dll")]
public static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, int lParam);
[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 bool SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
public readonly static uint MAGPIE_WM_NEWCURSOR = RegisterWindowMessage("MAGPIE_WM_NEWCURSOR");
public readonly static uint WM_SETCURSOR = 0x0020;
public readonly static int HTCLIENT = 1;
[StructLayout(LayoutKind.Sequential)]
public struct POINT {
public int x;
public int y;
}
[StructLayout(LayoutKind.Sequential)]
public struct CURSORINFO {
public int cbSize;
public int flags;
public IntPtr hCursor;
public POINT ptScreenPos;
}
[DllImport("user32.dll")]
public static extern bool GetCursorInfo(ref CURSORINFO pci);
[DllImport("user32.dll")]
public static extern bool DestroyCursor(IntPtr hCursor);
[StructLayout(LayoutKind.Sequential)]
public struct ICONINFO {
public int fIcon;
public uint xHotspot;
public uint yHotspot;
public IntPtr hbmMask;
public IntPtr hbmColor;
}
[DllImport("user32.dll")]
public static extern bool GetIconInfo(IntPtr hIcon, ref ICONINFO piconinfo);
[DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr ho);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
private static extern uint RegisterWindowMessage([MarshalAs(UnmanagedType.LPWStr)]string lpString);
private const int SM_CXCURSOR = 13;
private const int SM_CYCURSOR = 14;
[DllImport("user32.dll")]
private static extern int GetSystemMetrics(int nIndex);
public static (int x, int y) GetCursorSize() {
return (GetSystemMetrics(SM_CXCURSOR), GetSystemMetrics(SM_CYCURSOR));
}
[DllImport("user32.dll")]
public static extern IntPtr CreateCursor(IntPtr hInst, int xHotSpot, int yHotSpot, int nWidth, int nHeight, byte[] pvANDPlane, byte[] pvXORPlane);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
private static extern IntPtr GetModuleHandle(IntPtr lpModuleName);
public static IntPtr GetModule() {
return GetModuleHandle(IntPtr.Zero);
}
[DllImport("user32.dll")]
public static extern bool IsWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern int GetWindowThreadProcessId(IntPtr hWnd, ref int lpdwProcessId);
[DllImport("user32.dll")]
private static extern int GetWindowThreadProcessId(IntPtr hWnd, IntPtr lpdwProcessId);
public static int GetWindowProcessId(IntPtr hWnd) {
if(hWnd == IntPtr.Zero) {
return 0;
}
int processId = 0;
GetWindowThreadProcessId(hWnd, ref processId);
return processId;
}
public static int GetWindowThreadId(IntPtr hWnd) {
if (hWnd == IntPtr.Zero) {
return 0;
}
return GetWindowThreadProcessId(hWnd, IntPtr.Zero);
}
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(
[MarshalAs(UnmanagedType.LPWStr)] string lpClassName,
IntPtr lpWindowName
);
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
}
}