This repository has been archived on 2022-08-05. You can view files and clone it, but cannot push or open issues or pull requests.
CC-Functions/W32/Wnd32.cs

304 lines
10 KiB
C#
Raw Normal View History

2019-09-07 10:12:24 +02:00
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
2019-09-07 10:12:24 +02:00
using System.Drawing;
using System.Linq;
2019-09-07 10:12:24 +02:00
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace CC_Functions.W32
{
2019-10-05 16:37:31 +02:00
public sealed class Wnd32 : IEquatable<Wnd32>
2019-09-07 10:12:24 +02:00
{
#region Exposed
2019-11-29 21:27:24 +01:00
2019-09-07 10:12:24 +02:00
#region CreateInstance
2019-11-29 21:27:24 +01:00
2019-12-22 16:58:16 +01:00
private Wnd32(IntPtr wndref) => hWnd = wndref;
2019-11-29 21:27:24 +01:00
2019-12-22 16:58:16 +01:00
public static Wnd32 fromHandle(IntPtr handle) => new Wnd32(handle);
2019-11-29 21:27:24 +01:00
public static Wnd32 getProcessMain(Process process) => fromHandle(process.MainWindowHandle);
2020-01-10 16:52:06 +01:00
public static Wnd32 fromMetadata(string? lpClassName = null, string? lpWindowName = null) =>
2019-12-22 16:58:16 +01:00
fromHandle(FindWindow(lpClassName, lpWindowName));
2019-11-29 21:27:24 +01:00
2019-12-22 16:58:16 +01:00
public static Wnd32 fromPoint(Point point) => fromHandle(WindowFromPoint(point.X, point.Y));
2019-11-29 21:27:24 +01:00
2019-12-22 16:58:16 +01:00
public static Wnd32 fromForm(Form form) => fromHandle(form.Handle);
2019-11-29 21:27:24 +01:00
2019-12-22 16:58:16 +01:00
public static Wnd32 foreground() => fromHandle(GetForegroundWindow());
2019-11-29 21:27:24 +01:00
public static Wnd32[] getVisible() =>
getAll().Where(s => IsWindowVisible(s.hWnd) && !string.IsNullOrEmpty(s.title)).ToArray();
public static Wnd32[] getAll()
{
WindowHandles = new List<IntPtr>();
if (!EnumDesktopWindows(IntPtr.Zero, FilterCallback, IntPtr.Zero))
throw new Win32Exception("There was a native error. This should never happen!");
return WindowHandles.Select(s => fromHandle(s)).ToArray();
}
2019-11-29 21:27:24 +01:00
#endregion CreateInstance
2019-09-07 10:12:24 +02:00
#region InstanceActions
2019-11-29 21:27:24 +01:00
2019-09-11 11:22:16 +02:00
public string title
{
get
{
2019-12-22 16:58:16 +01:00
int length = GetWindowTextLength(hWnd);
StringBuilder sb = new StringBuilder(length + 1);
2019-09-07 10:12:24 +02:00
GetWindowText(hWnd, sb, sb.Capacity);
return sb.ToString();
2019-09-11 11:22:16 +02:00
}
set => SetWindowText(hWnd, value);
2019-09-11 11:22:16 +02:00
}
2019-11-29 21:27:24 +01:00
2019-09-11 11:22:16 +02:00
public Rectangle position
{
get
{
2019-12-22 16:58:16 +01:00
RECT Rect = new RECT();
2019-09-07 10:12:24 +02:00
GetWindowRect(hWnd, ref Rect);
return new Rectangle(new Point(Rect.left, Rect.top),
new Size(Rect.right - Rect.left, Rect.bottom - Rect.top));
2019-09-11 11:22:16 +02:00
}
set
{
2019-12-22 16:58:16 +01:00
RECT Rect = new RECT();
2019-09-07 10:12:24 +02:00
GetWindowRect(hWnd, ref Rect);
MoveWindow(hWnd, value.X, value.Y, value.Width, value.Height, true);
2019-09-11 11:22:16 +02:00
}
}
2019-11-29 21:27:24 +01:00
2019-09-11 11:22:16 +02:00
public bool isForeground
{
get => GetForegroundWindow() == hWnd;
set
{
2019-09-07 10:12:24 +02:00
if (value)
SetForegroundWindow(hWnd);
else
throw new InvalidOperationException(
"You can't set a Window not to be in the foreground. Move another one over it!");
2019-09-07 10:12:24 +02:00
}
}
2019-11-29 21:27:24 +01:00
2019-09-11 11:22:16 +02:00
public bool enabled
{
get => IsWindowEnabled(hWnd);
set => EnableWindow(hWnd, value);
2019-09-07 10:12:24 +02:00
}
2019-11-29 21:27:24 +01:00
2019-09-11 11:22:16 +02:00
public Icon icon
{
get
{
2019-12-22 16:58:16 +01:00
IntPtr hicon = SendMessage(hWnd, 0x7F, 1, 0);
2019-09-11 11:22:16 +02:00
if (hicon == IntPtr.Zero)
hicon = SendMessage(hWnd, 0x7F, 0, 0);
if (hicon == IntPtr.Zero)
hicon = SendMessage(hWnd, 0x7F, 2, 0);
return Icon.FromHandle(hicon);
}
}
2019-11-29 21:27:24 +01:00
2019-09-11 11:22:16 +02:00
public bool shown
{
get => IsWindowVisible(hWnd);
set
{
2019-09-11 11:22:16 +02:00
if (value)
ShowWindow(hWnd, 9);
else
ShowWindow(hWnd, 0);
}
}
2019-11-29 21:27:24 +01:00
public string className
{
get
{
2019-12-22 16:58:16 +01:00
StringBuilder ClassName = new StringBuilder(256);
2019-11-29 21:27:24 +01:00
_ = GetClassName(hWnd, ClassName, ClassName.Capacity);
return ClassName.ToString();
}
}
2019-09-11 11:22:16 +02:00
public FormWindowState state
{
get
{
2019-12-22 16:58:16 +01:00
int style = GetWindowLong(hWnd, -16);
2019-09-11 11:22:16 +02:00
if ((style & 0x01000000) == 0x01000000)
return FormWindowState.Maximized;
if ((style & 0x20000000) == 0x20000000)
2019-09-11 11:22:16 +02:00
return FormWindowState.Minimized;
return FormWindowState.Normal;
2019-09-11 11:22:16 +02:00
}
set
{
2019-09-11 11:22:16 +02:00
switch (value)
{
case FormWindowState.Minimized:
ShowWindow(hWnd, 11);
break;
2019-11-29 21:27:24 +01:00
2019-09-11 11:22:16 +02:00
case FormWindowState.Normal:
ShowWindow(hWnd, 1);
break;
2019-11-29 21:27:24 +01:00
2019-09-11 11:22:16 +02:00
case FormWindowState.Maximized:
ShowWindow(hWnd, 3);
break;
}
}
}
public void MakeOverlay()
{
overlay = true;
}
2019-11-29 21:27:24 +01:00
public bool overlay
{
set
{
2019-12-22 16:58:16 +01:00
Rectangle tmp = position;
_ = SetWindowPos(hWnd, value ? HWND_TOPMOST : HWND_NOTOPMOST, tmp.X, tmp.Y, tmp.Width, tmp.Height,
value ? SWP_NOMOVE | SWP_NOSIZE : 0);
2019-11-29 21:27:24 +01:00
}
}
2019-09-07 10:12:24 +02:00
public bool Destroy()
{
if (DestroyWindow(hWnd))
return true;
throw new Exception("Failed.");
2019-09-07 10:12:24 +02:00
}
public bool stillExists => IsWindow(hWnd);
2019-11-29 21:27:24 +01:00
2019-12-22 16:58:16 +01:00
public override string ToString() => hWnd + "; " + title + "; " + position;
2019-11-29 21:27:24 +01:00
2019-12-22 16:58:16 +01:00
public override bool Equals(object obj) => Equals(obj as Wnd32);
2019-11-29 21:27:24 +01:00
2019-12-22 16:58:16 +01:00
public bool Equals(Wnd32 other) => other != null && EqualityComparer<IntPtr>.Default.Equals(hWnd, other.hWnd);
2019-11-29 21:27:24 +01:00
2019-12-22 16:58:16 +01:00
public override int GetHashCode() => -75345830 + EqualityComparer<IntPtr>.Default.GetHashCode(hWnd);
2019-11-29 21:27:24 +01:00
2019-12-22 16:58:16 +01:00
public static bool operator ==(Wnd32 left, Wnd32 right) => EqualityComparer<Wnd32>.Default.Equals(left, right);
2019-11-29 21:27:24 +01:00
2019-12-22 16:58:16 +01:00
public static bool operator !=(Wnd32 left, Wnd32 right) => !(left == right);
2019-11-29 21:27:24 +01:00
#endregion InstanceActions
#endregion Exposed
2019-09-07 10:12:24 +02:00
#region W32
2019-11-29 21:27:24 +01:00
2019-09-07 10:12:24 +02:00
public IntPtr hWnd;
2019-11-29 21:27:24 +01:00
2019-09-07 10:12:24 +02:00
[DllImport("user32.dll")]
2019-11-29 21:27:24 +01:00
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool IsWindowVisible(IntPtr hWnd);
2019-09-07 10:12:24 +02:00
2019-11-29 21:27:24 +01:00
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Ansi)]
private static extern long GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
2019-09-07 10:12:24 +02:00
[DllImport("user32.dll")]
2019-11-29 21:27:24 +01:00
private static extern IntPtr GetForegroundWindow();
2019-09-07 10:12:24 +02:00
2019-11-29 21:27:24 +01:00
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
private static extern IntPtr WindowFromPoint(int xPoint, int yPoint);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern bool SetWindowText(IntPtr hWnd, string lpString);
2019-09-07 10:12:24 +02:00
[DllImport("user32.dll", SetLastError = true)]
2019-11-29 21:27:24 +01:00
private static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
2019-09-07 10:12:24 +02:00
[DllImport("user32.dll")]
2019-11-29 21:27:24 +01:00
private static extern bool SetForegroundWindow(IntPtr hWnd);
2019-09-07 10:12:24 +02:00
[DllImport("user32.dll")]
2019-11-29 21:27:24 +01:00
private static extern bool EnableWindow(IntPtr hWnd, bool bEnable);
2019-09-07 10:12:24 +02:00
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
2019-11-29 21:27:24 +01:00
private static extern bool IsWindowEnabled(IntPtr hWnd);
2019-09-07 10:12:24 +02:00
[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
2019-11-29 21:27:24 +01:00
private static extern bool DestroyWindow(IntPtr hwnd);
private static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
private static readonly IntPtr HWND_TOP = new IntPtr(0);
private static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
private const uint SWP_NOSIZE = 0x0001;
private const uint SWP_NOMOVE = 0x0002;
2019-09-07 10:12:24 +02:00
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy,
uint uFlags);
2019-09-07 10:12:24 +02:00
2019-11-29 21:27:24 +01:00
[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
2019-09-07 10:12:24 +02:00
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall,
ExactSpelling = true, SetLastError = true)]
2019-11-29 21:27:24 +01:00
private static extern bool GetWindowRect(IntPtr hWnd, ref RECT rect);
2019-09-07 10:12:24 +02:00
2019-11-29 21:27:24 +01:00
private struct RECT
2019-09-07 10:12:24 +02:00
{
public int left;
public int top;
public int right;
public int bottom;
}
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
2019-11-29 21:27:24 +01:00
private static extern int GetWindowTextLength(IntPtr hWnd);
2019-09-07 10:12:24 +02:00
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
2019-11-29 21:27:24 +01:00
private static extern bool IsWindow(IntPtr hWnd);
2019-09-11 11:22:16 +02:00
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
2019-11-29 21:27:24 +01:00
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);
2019-09-11 11:22:16 +02:00
[DllImport("user32.dll")]
2019-11-29 21:27:24 +01:00
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
2019-09-11 11:22:16 +02:00
[DllImport("user32.dll", SetLastError = true)]
2019-11-29 21:27:24 +01:00
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll", EntryPoint = "EnumDesktopWindows", ExactSpelling = false, CharSet = CharSet.Auto,
SetLastError = true)]
private static extern bool EnumDesktopWindows(IntPtr hDesktop, EnumDelegate lpEnumCallbackFunction,
IntPtr lParam);
// Define the callback delegate's type.
private delegate bool EnumDelegate(IntPtr hWnd, int lParam);
private static List<IntPtr> WindowHandles;
private static bool FilterCallback(IntPtr hWnd, int lParam)
{
2019-12-22 16:58:16 +01:00
StringBuilder sb_title = new StringBuilder(1024);
GetWindowText(hWnd, sb_title, 1024);
WindowHandles.Add(hWnd);
return true;
}
2019-11-29 21:27:24 +01:00
#endregion W32
2019-09-07 10:12:24 +02:00
}
2019-11-29 21:27:24 +01:00
}