WindowsHelper
namespace ZL_WorkPlatform
{
public class WindowsHelper
{
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetWindowRect(HandleRef hWnd, out RECT lpRect);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
public static IntPtr GetHandleWindow(string title)
{
return FindWindow(null, title);
}
private delegate bool EnumWindowsProc(IntPtr hWnd, int lParam);
[DllImport("USER32.DLL")]
private static extern bool EnumWindows(EnumWindowsProc enumFunc, int lParam);
[DllImport("USER32.DLL")]
private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("USER32.DLL")]
private static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("USER32.DLL")]
private static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport("USER32.dll")]
public static extern bool IsIconic(IntPtr hWnd);
[DllImport("USER32.DLL")]
private static extern IntPtr GetShellWindow();
[DllImport("dwmapi.dll")]
static extern int DwmGetWindowAttribute(IntPtr hwnd, int dwAttribute, out bool pvAttribute, int cbAttribute);
[Flags]
public enum DwmWindowAttribute : uint
{
DWMWA_NCRENDERING_ENABLED = 1,
DWMWA_NCRENDERING_POLICY,
DWMWA_TRANSITIONS_FORCEDISABLED,
DWMWA_ALLOW_NCPAINT,
DWMWA_CAPTION_BUTTON_BOUNDS,
DWMWA_NONCLIENT_RTL_LAYOUT,
DWMWA_FORCE_ICONIC_REPRESENTATION,
DWMWA_FLIP3D_POLICY,
DWMWA_EXTENDED_FRAME_BOUNDS,
DWMWA_HAS_ICONIC_BITMAP,
DWMWA_DISALLOW_PEEK,
DWMWA_EXCLUDED_FROM_PEEK,
DWMWA_CLOAK,
DWMWA_CLOAKED,
DWMWA_FREEZE_REPRESENTATION,
DWMWA_LAST
}
public static IDictionary<IntPtr, string> GetOpeningWindows()
{
IntPtr shellWindow = GetShellWindow();
Dictionary<IntPtr, string> windows = new Dictionary<IntPtr, string>();
EnumWindows(delegate (IntPtr hWnd, int lParam)
{
if (hWnd == shellWindow) return true;
if (!IsWindowVisible(hWnd)) return true;
if (IsIconic(hWnd)) return true;
bool isCloacked;
DwmGetWindowAttribute(hWnd, (int)DwmWindowAttribute.DWMWA_CLOAKED, out isCloacked, Marshal.SizeOf(typeof(bool)));
if (isCloacked)
return true;
int length = GetWindowTextLength(hWnd);
if (length == 0) return true;
StringBuilder builder = new StringBuilder(length);
GetWindowText(hWnd, builder, length + 1);
windows[hWnd] = builder.ToString();
return true;
}, 0);
return windows;
}
public static IDictionary<IntPtr, string> GetOpeningWindowsAll()
{
IntPtr shellWindow = GetShellWindow();
Dictionary<IntPtr, string> windows = new Dictionary<IntPtr, string>();
EnumWindows(delegate (IntPtr hWnd, int lParam)
{
if (hWnd == shellWindow) return true;
if (!IsWindowVisible(hWnd)) return true;
bool isCloacked;
DwmGetWindowAttribute(hWnd, (int)DwmWindowAttribute.DWMWA_CLOAKED, out isCloacked, Marshal.SizeOf(typeof(bool)));
if (isCloacked)
return true;
int length = GetWindowTextLength(hWnd);
if (length == 0) return true;
StringBuilder builder = new StringBuilder(length);
GetWindowText(hWnd, builder, length + 1);
windows[hWnd] = builder.ToString();
return true;
}, 0);
return windows;
}
public static IDictionary<IntPtr, string> GetOpeningWindowsAllWithHide()
{
IntPtr shellWindow = GetShellWindow();
Dictionary<IntPtr, string> windows = new Dictionary<IntPtr, string>();
EnumWindows(delegate (IntPtr hWnd, int lParam)
{
if (hWnd == shellWindow) return true;
bool isCloacked;
DwmGetWindowAttribute(hWnd, (int)DwmWindowAttribute.DWMWA_CLOAKED, out isCloacked, Marshal.SizeOf(typeof(bool)));
if (isCloacked)
return true;
int length = GetWindowTextLength(hWnd);
if (length == 0) return true;
StringBuilder builder = new StringBuilder(length);
GetWindowText(hWnd, builder, length + 1);
windows[hWnd] = builder.ToString();
return true;
}, 0);
return windows;
}
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
public override string ToString()
{
return $"Left:{Left}, Top:{Top}, Right:{Right}, Bottom:{Bottom}";
}
}
public static Rectangle GetWinRectangleByName(string winName)
{
if (string.IsNullOrEmpty(winName))
return Rectangle.Empty;
var winHandle = GetHandleWindow(winName);
return GetWinRectangleByHandle(winHandle);
}
public static Rectangle GetWinRectangleByHandle(IntPtr hWnd)
{
if (hWnd == IntPtr.Zero) return Rectangle.Empty;
RECT rect;
var suc = GetWindowRect(new HandleRef(null, hWnd), out rect);
Rectangle rectangle = new Rectangle();
rectangle.X = rect.Left;
rectangle.Y = rect.Top;
rectangle.Width = rect.Right - rect.Left;
rectangle.Height = rect.Bottom - rect.Top;
return rectangle;
}
}
}