| 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);
-
-
-
-
-
-
- public delegate bool WndEnumProc(IntPtr hWnd, int lParam);
-
-
-
-
-
-
- [DllImport("user32")]
- public static extern bool EnumWindows(WndEnumProc lpEnumFunc, int lParam);
-
-
-
-
-
- [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;
-
-
-
-
- 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();
-
-
-
-
-
- [DllImport("user32.dll", SetLastError = true)]
- public static extern bool IsWindow(IntPtr hWnd);
-
-
-
-
-
- [DllImport("user32.dll")]
- [return: MarshalAs(UnmanagedType.Bool)]
- public static extern bool IsIconic(IntPtr hWnd);
-
-
-
-
-
- [DllImport("user32.dll")]
- public static extern bool IsZoomed(IntPtr hWnd);
-
-
-
-
-
- [DllImport("user32")]
- public static extern bool IsWindowVisible(IntPtr hWnd);
- [DllImport("user32.dll")]
- public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
-
-
-
-
-
-
- [DllImport("user32.dll")]
- public static extern int GetWindowTextLength(IntPtr hWnd);
-
-
-
-
-
-
-
- [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);
-
-
-
- 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;
- }
-
-
-
- public IntPtr Hwnd { get; }
-
-
-
- public string ClassName { get; }
-
-
-
- public string Title { get; }
-
-
-
- public bool IsVisible { get; }
-
-
-
- public Rectangle Bounds { get; }
-
-
-
- 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();
- }
-
-
-
-
-
- public static IReadOnlyList<WindowInfo> FindAllWindows(Predicate<WindowInfo> match = null)
- {
- windowList = new List<WindowInfo>();
-
- EnumWindows(OnWindowEnum, 0);
- return windowList.FindAll(match ?? DefaultPredicate);
- }
-
-
-
-
-
-
- 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;
- }
-
-
-
- public static readonly Predicate<WindowInfo> DefaultPredicate = x => x.IsVisible && !x.IsMinimized && x.Title.Length > 0;
-
-
-
- public static List<WindowInfo> windowList;
-
-
-
-
-
-
-
- [DllImport("user32.dll")]
- [return: MarshalAs(UnmanagedType.Bool)]
- public static extern bool EnumChildWindows(IntPtr hwndParent, WndEnumProc lpEnumFunc, int lParam);
-
-
-
-
-
-
- [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
- public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
-
-
-
-
-
-
-
-
- [DllImport("user32.dll", EntryPoint = "FindWindowEx", SetLastError = true)]
- public static extern IntPtr FindWindowEx(IntPtr hwndParent, uint hwndChildAfter, string lpszClass, string lpszWindow);
-
- [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;
- const int VK_A = 0x41;
- 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)
- {
-
- 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;
- }
- }
-
-
-
- }
- }
- }
|