1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
|
internal class Win32Api { [StructLayout(LayoutKind.Sequential)] public struct Rect { public int Left; public int Top; public int Right; public int Bottom; }
public readonly 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; }
[DllImport("user32.dll", SetLastError = true)] public static extern IntPtr GetWindow(IntPtr hwnd, uint windowType);
public delegate bool WNDENUMPROC(IntPtr hwnd, int lParam);
[DllImport("user32.dll", SetLastError = true)] public static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, uint lParam);
[DllImport("user32.dll", SetLastError = true)] public static extern IntPtr GetParent(IntPtr hWnd);
[DllImport("user32.dll")] public static extern uint GetWindowThreadProcessId(IntPtr hWnd, ref uint lpdwProcessId); [DllImport("user32.dll")] public static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("User32.dll", CharSet = CharSet.Auto)] public static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int nMaxCount);
[DllImport("kernel32.dll")] public static extern void SetLastError(uint dwErrCode);
[DllImport("user32")] public static extern int GetClassName(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32")] public static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport("user32.dll")] public static extern int GetWindowRect(IntPtr hwnd, out Rect lpRect);
public static IntPtr ptrWnd = IntPtr.Zero; static string Title;
public static IntPtr GetProcessWnd(string title) { if (ptrWnd == IntPtr.Zero) { Title = title; uint pid = (uint)Process.GetCurrentProcess().Id; bool bResult = EnumWindows(OnWindowEnum, pid); return (!bResult && Marshal.GetLastWin32Error() == 0) ? ptrWnd : IntPtr.Zero; } return ptrWnd; }
static bool OnWindowEnum(IntPtr hwnd, int lParam) { uint id = 0;
if (GetParent(hwnd) == IntPtr.Zero) { GetWindowThreadProcessId(hwnd, ref id); if (id == lParam) { int length = GetWindowTextLength(hwnd); StringBuilder windowName = new StringBuilder(length + 1); GetWindowText(hwnd, windowName, windowName.Capacity); if (windowName.ToString().CompareTo(Title) == 0) { ptrWnd = hwnd; SetLastError(0); return false; } } } return true; }
public static bool IsCover(IntPtr handle) {
var currentWindow = GetWindowDetail(handle);
var windows = GetAllAboveWindows(handle); var windowInfos = windows.Where(I => I.IsVisible && !I.IsMinimized && I.Bounds.Width > 1 && I.Bounds.Height > 1 && I.Hwnd != handle && !(I.ClassName.StartsWith("Shell_") && I.ClassName.EndsWith("TrayWnd"))) .ToList();
foreach (var item in windowInfos) {
if (item.Bounds.Contains(currentWindow.Bounds)) return true; }
return false; }
public static IReadOnlyList<WindowInfo> FindAll(Predicate<WindowInfo> match = null) { var windowList = new List<WindowInfo>(); EnumWindows(OnWindowEnum, 0); return match == null ? windowList : windowList.FindAll(match);
bool OnWindowEnum(IntPtr hWnd, int lparam) { if (GetParent(hWnd) == IntPtr.Zero) { var windowDetail = GetWindowDetail(hWnd); windowList.Add(windowDetail); } return true; } }
public static WindowInfo GetWindowDetail(IntPtr hWnd) { 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);
Rect rect = default; GetWindowRect(hWnd, out rect); var bounds = new Rectangle(rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top);
return new WindowInfo(hWnd, className, title, isVisible, bounds); } public static List<WindowInfo> GetAllAboveWindows(IntPtr hwnd) { var windowInfos = new List<WindowInfo>(); var intPtr = GetWindow(hwnd, 3); if (intPtr == IntPtr.Zero) { return windowInfos; } var windowDetail = GetWindowDetail(intPtr); windowInfos.AddRange(GetAllAboveWindows(intPtr)); windowInfos.Add(windowDetail); return windowInfos; } public static List<WindowInfo> GetAllBelowWindows(IntPtr hwnd) { var windowInfos = new List<WindowInfo>(); var intPtr = GetWindow(hwnd, 2); if (intPtr == IntPtr.Zero) { return windowInfos; } var windowDetail = GetWindowDetail(intPtr); windowInfos.AddRange(GetAllBelowWindows(intPtr)); windowInfos.Add(windowDetail); return windowInfos; } }
|