123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389 |
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace PTMedicalInsurance.Common.WinAPI
- {
- class WndHelper
- {
- // 发送按键的方法(根据你的需求)
- [DllImport("user32.dll")]
- public static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo);
- // <summary>
- /// 枚举窗口时的委托参数
- /// </summary>
- /// <param name="hWnd"></param>
- /// <param name="lParam"></param>
- /// <returns></returns>
- public delegate bool WndEnumProc(IntPtr hWnd, int lParam);
- /// <summary>
- /// 枚举所有窗口
- /// </summary>
- /// <param name="lpEnumFunc"></param>
- /// <param name="lParam"></param>
- /// <returns></returns>
- [DllImport("user32")]
- public static extern bool EnumWindows(WndEnumProc lpEnumFunc, int lParam);
- /// <summary>
- /// 获取窗口的父窗口句柄
- /// </summary>
- /// <param name="hWnd"></param>
- /// <returns></returns>
- [DllImport("user32")]
- public static extern IntPtr GetParent(IntPtr hWnd);
- [DllImport("user32")]
- public static extern int GetClassName(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
- [DllImport("user32")]
- public static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);
- [DllImport("user32")]
- public static extern bool GetWindowRect(IntPtr hWnd, ref LPRECT rect);
- [DllImport("user32.dll")]
- public static extern IntPtr SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint wFlags);
- [StructLayout(LayoutKind.Sequential)]
- public readonly struct LPRECT
- {
- public readonly int Left;
- public readonly int Top;
- public readonly int Right;
- public readonly int Bottom;
- }
- public const int HWND_TOP = 0;
- public const int HWND_BOTTOM = 1;
- public const int HWND_TOPMOST = -1;
- public const int HWND_NOTOPMOST = -2;
- /// <summary>
- /// 设置窗体为TopMost
- /// </summary>
- /// <param name="hWnd"></param>
- public static void SetTopomost(IntPtr hWnd)
- {
- LPRECT rect = new LPRECT();
- GetWindowRect(hWnd, ref rect);
- SetWindowPos(hWnd, (IntPtr)HWND_TOPMOST, rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top, 0);
- }
- [DllImport("user32.dll")]
- [return: MarshalAs(UnmanagedType.Bool)]
- public static extern bool SetForegroundWindow(IntPtr hWnd);
- [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
- public static extern IntPtr GetForegroundWindow();
- /// <summary>
- /// 判断是否窗体
- /// </summary>
- /// <param name="hWnd"></param>
- /// <returns></returns>
- [DllImport("user32.dll", SetLastError = true)]
- public static extern bool IsWindow(IntPtr hWnd);
- /// <summary>
- /// 当前窗口是否最小化
- /// </summary>
- /// <param name="hWnd"></param>
- /// <returns></returns>
- [DllImport("user32.dll")]
- [return: MarshalAs(UnmanagedType.Bool)]
- public static extern bool IsIconic(IntPtr hWnd);
- /// <summary>
- /// 当前窗口是否最大化
- /// </summary>
- /// <param name="hWnd"></param>
- /// <returns></returns>
- [DllImport("user32.dll")]
- public static extern bool IsZoomed(IntPtr hWnd);
- /// <summary>
- /// 判断窗口是否可见
- /// </summary>
- /// <param name="hWnd"></param>
- /// <returns></returns>
- [DllImport("user32")]
- public static extern bool IsWindowVisible(IntPtr hWnd);
- [DllImport("user32.dll")]
- public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
- // 获取窗口文本长度
- /// <summary>
- /// 获取目标窗口标题的文本长度
- /// </summary>
- /// <param name="hWnd">目标窗口句柄</param>
- /// <returns>标题文本长度</returns>
- [DllImport("user32.dll")]
- public static extern int GetWindowTextLength(IntPtr hWnd);
- /// <summary>
- /// 获取窗口文本,文本会塞入StringBuilder中,需要指明字符串最大长度nMaxCount
- /// </summary>
- /// <param name="hwnd">窗口句柄</param>
- /// <param name="lpString">返回目标窗口的内容</param>
- /// <param name="nMaxCount">允许返回的字符数量上限</param>
- /// <returns>实际获取到的文本长度</returns>
- [DllImport("User32.dll", EntryPoint = "GetWindowText")]
- public static extern int GetWindowText(IntPtr hwnd, StringBuilder lpString, int nMaxCount);
- [DllImport("User32.dll", CharSet = CharSet.Auto)]
- public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
- /// <summary>
- /// 获取 Win32 窗口的一些基本信息。
- /// </summary>
- public struct WindowInfo
- {
- public WindowInfo(IntPtr hWnd, string className, string title, bool isVisible, Rectangle bounds) : this()
- {
- Hwnd = hWnd;
- ClassName = className;
- Title = title;
- IsVisible = isVisible;
- Bounds = bounds;
- }
- /// <summary>
- /// 获取窗口句柄。
- /// </summary>
- public IntPtr Hwnd { get; }
- /// <summary>
- /// 获取窗口类名。
- /// </summary>
- public string ClassName { get; }
- /// <summary>
- /// 获取窗口标题。
- /// </summary>
- public string Title { get; }
- /// <summary>
- /// 获取当前窗口是否可见。
- /// </summary>
- public bool IsVisible { get; }
- /// <summary>
- /// 获取窗口当前的位置和尺寸。
- /// </summary>
- public Rectangle Bounds { get; }
- /// <summary>
- /// 获取窗口当前是否是最小化的。
- /// </summary>
- public bool IsMinimized => Bounds.Left == -32000 && Bounds.Top == -32000;
- }
- public struct wndInfo
- {
- public IntPtr hWnd;
- public string szWindowName;
- public string szClassName;
- }
- [DllImport("user32.dll")]
- private static extern int GetWindowTextW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder lpString, int nMaxCount);
- [DllImport("user32.dll")]
- private static extern int GetClassNameW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder lpString, int nMaxCount);
- public static wndInfo[] GetAllDesktopWindows()
- {
- List<wndInfo> wndList = new List<wndInfo>();
- // 使用委托实现窗口枚举逻辑
- EnumWindows(delegate (IntPtr hWnd, int lParam)
- {
- wndInfo wnd = new wndInfo();
- StringBuilder sb = new StringBuilder(256);
- // 获取窗口句柄
- wnd.hWnd = hWnd;
- // 获取窗口名称
- GetWindowTextW(hWnd, sb, sb.Capacity);
- wnd.szWindowName = sb.ToString();
- // 获取窗口类名
- GetClassNameW(hWnd, sb, sb.Capacity);
- wnd.szClassName = sb.ToString();
- // 添加到列表中
- wndList.Add(wnd);
- return true;
- }, 0);
- return wndList.ToArray();
- }
- /// <summary>
- /// 查找当前用户空间下所有符合条件的(顶层)窗口。如果不指定条件,将仅查找可见且有标题栏的窗口。
- /// </summary>
- /// <param name="match">过滤窗口的条件。如果设置为 null,将仅查找可见和标题栏不为空的窗口。</param>
- /// <returns>找到的所有窗口信息</returns>
- public static IReadOnlyList<WindowInfo> FindAllWindows(Predicate<WindowInfo> match = null)
- {
- windowList = new List<WindowInfo>();
- //遍历窗口并查找窗口相关WindowInfo信息
- EnumWindows(OnWindowEnum, 0);
- return windowList.FindAll(match ?? DefaultPredicate);
- }
- /// <summary>
- /// 遍历窗体处理的函数
- /// </summary>
- /// <param name="hWnd"></param>
- /// <param name="lparam"></param>
- /// <returns></returns>
- public static bool OnWindowEnum(IntPtr hWnd, int lparam)
- {
- // 仅查找顶层窗口。
- if (GetParent(hWnd) == IntPtr.Zero)
- {
- // 获取窗口类名。
- var lpString = new StringBuilder(512);
- GetClassName(hWnd, lpString, lpString.Capacity);
- var className = lpString.ToString();
- // 获取窗口标题。
- var lptrString = new StringBuilder(512);
- GetWindowText(hWnd, lptrString, lptrString.Capacity);
- var title = lptrString.ToString().Trim();
- // 获取窗口可见性。
- var isVisible = IsWindowVisible(hWnd);
- // 获取窗口位置和尺寸。
- LPRECT rect = default;
- GetWindowRect(hWnd, ref rect);
- var bounds = new Rectangle(rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top);
- // 添加到已找到的窗口列表。
- windowList.Add(new WindowInfo(hWnd, className, title, isVisible, bounds));
- }
- return true;
- }
- /// <summary>
- /// 默认的查找窗口的过滤条件。可见 + 非最小化 + 包含窗口标题。
- /// </summary>
- public static readonly Predicate<WindowInfo> DefaultPredicate = x => x.IsVisible && !x.IsMinimized && x.Title.Length > 0;
- /// <summary>
- /// 窗体列表
- /// </summary>
- public static List<WindowInfo> windowList;
- /// <summary>
- /// 遍历子窗体(控件)
- /// </summary>
- /// <param name="hwndParent">父窗口句柄</param>
- /// <param name="lpEnumFunc">遍历的回调函数</param>
- /// <param name="lParam">传给遍历时回调函数的额外数据</param>
- /// <returns></returns>
- [DllImport("user32.dll")]
- [return: MarshalAs(UnmanagedType.Bool)]
- public static extern bool EnumChildWindows(IntPtr hwndParent, WndEnumProc lpEnumFunc, int lParam);
- /// <summary>
- /// 查找窗体
- /// </summary>
- /// <param name="lpClassName">窗体的类名称,比如Form、Window。若不知道,指定为null即可</param>
- /// <param name="lpWindowName">窗体的标题/文字</param>
- /// <returns></returns>
- [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
- public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
- /// <summary>
- /// 查找子窗体(控件)
- /// </summary>
- /// <param name="hwndParent">父窗体句柄,不知道窗体时可指定IntPtr.Zero</param>
- /// <param name="hwndChildAfter">子窗体(控件),通常不知道子窗体(句柄),指定0即可</param>
- /// <param name="lpszClass">子窗体(控件)的类名,通常指定null,它是window class name,并不等同于C#中的列名Button、Image、PictureBox等,两者并不相同,可通过GetClassName获取正确的类型名</param>
- /// <param name="lpszWindow">子窗体的名字或控件的Title、Text,通常为显示的文字</param>
- /// <returns></returns>
- [DllImport("user32.dll", EntryPoint = "FindWindowEx", SetLastError = true)]
- public static extern IntPtr FindWindowEx(IntPtr hwndParent, uint hwndChildAfter, string lpszClass, string lpszWindow);
- //消息发送API
- [DllImport("user32.dll", EntryPoint = "PostMessage")]
- public static extern bool PostMessage(IntPtr hWnd, int Msg, uint wParam, uint lParam);
-
- [DllImport("user32.dll", CharSet = CharSet.Auto)]
- static extern IntPtr SendMessage(IntPtr hWnd, int Msg, uint wParam, uint lParam);
- [DllImport("user32.dll")] private static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);
- private const int KEYEVENTF_EXTENDEDKEY = 0x1;
- private const int KEYEVENTF_KEYUP = 0x2;
- const uint WM_KEYDOWN = 0x100;
- const uint WM_KEYUP = 0x101;
- const uint WM_SYSKEYDOWN = 0x104;
- const uint WM_SYSKEYUP = 0x105;
- const int VK_CONTROL = 0x11; // Ctrl键
- const int VK_A = 0x41; // A键
- const int VK_R = 0x52;
- const int VK_P = 0x50;
- const int VK_S = 0x53;
- const int VK_F = 0x46;
- const int VK_C = 0x43;
- public enum msgType
- {
- send = 0,
- post =1,
- keybd_event =2
- }
- public static void SendMsg(msgType mType,IntPtr hWnd,int key)
- {
- //SetTopomost(hWnd);
- SetForegroundWindow(hWnd);
- switch (mType)
- {
- case msgType.send:
- {
- Thread.Sleep(2000);
- SendMessage(hWnd, 0X100, (uint)key, 0);//
- SendMessage(hWnd, 0X101, (uint)key, 0);//
- break;
- }
- case msgType.post:
- {
- PostMessage(hWnd, 0X100, (uint)key, 0);//
- PostMessage(hWnd, 0X101, (uint)key, 0);//
- break;
- }
- case msgType.keybd_event:
- {
- keybd_event(Convert.ToByte(key), 0, 0, 0);
- keybd_event(Convert.ToByte(key), 0, KEYEVENTF_KEYUP, 0);
- break;
- }
- default:
- {
- SendMessage(hWnd, 0X100, (uint)key, 0);//
- SendMessage(hWnd, 0X101, (uint)key, 0);//
- break;
- }
- }
- //Lparam的解释壳参考
- //https://blog.csdn.net/deniece1/article/details/103588428 扫描码
- //https://blog.csdn.net/yizhe0731/article/details/103194401
- }
- }
- }
|