Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Text;
- namespace TomLib.WinApi
- {
- /// <summary>
- /// Models a Windows API window and acts as a wrapper for many unmanaged methods.
- /// </summary>
- public class Window
- {
- #region Windows API P/Invokes
- [DllImport("user32.dll", SetLastError = true)]
- private static extern int GetWindowTextLength(IntPtr hWnd);
- [DllImport("user32.dll", SetLastError = true)]
- private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
- [DllImport("user32.dll", SetLastError = true)]
- [return: MarshalAs(UnmanagedType.Bool)]
- private static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);
- [DllImport("user32.dll", SetLastError = true)]
- [return: MarshalAs(UnmanagedType.Bool)]
- private static extern bool CloseWindow(IntPtr hWnd);
- [DllImport("user32.dll", SetLastError = false)]
- private static extern IntPtr GetDesktopWindow();
- [DllImport("user32.dll")]
- [return: MarshalAs(UnmanagedType.Bool)]
- private static extern bool IsWindowVisible(IntPtr hWnd);
- [DllImport("user32.dll")]
- [return: MarshalAs(UnmanagedType.Bool)]
- private static extern bool EnumChildWindows(IntPtr hwndParent, EnumWindowsProc lpEnumFunc, IntPtr lParam);
- [DllImport("user32.dll", SetLastError = true)]
- private static extern int GetWindowLong(IntPtr hWnd, GetWindowLongNIndex nIndex);
- [DllImport("user32.dll", SetLastError = true)]
- private static extern IntPtr FindWindow(IntPtr lpClassName, string lpWindowName);
- [DllImport("user32.dll")]
- [return: MarshalAs(UnmanagedType.Bool)]
- private static extern bool SetForegroundWindow(IntPtr hWnd);
- [DllImport("user32.dll")]
- private static extern IntPtr GetForegroundWindow();
- [DllImport("user32.dll")]
- private static extern IntPtr GetWindowThreadProcessId(IntPtr hWnd, out IntPtr lpdwProcessId);
- #endregion
- private readonly IntPtr _handle;
- /// <summary>
- /// Initialise a new Window instance from a window handle. Window objects are thread-safe.
- /// </summary>
- /// <param name="handle">An <see cref="IntPtr"/> which identifies the window.</param>
- public Window(IntPtr handle)
- {
- _handle = handle;
- }
- /// <summary>
- /// Initialise a new Window instance from the case-insensitive title of a top-level window.
- /// </summary>
- /// <param name="title">The title of the window.</param>
- public Window(string title)
- {
- IntPtr foundHandle = FindWindow(IntPtr.Zero, title);
- if (foundHandle.Equals(IntPtr.Zero))
- {
- throw new ArgumentException("Could not find a window with a title matching: " + title, "title");
- }
- _handle = foundHandle;
- }
- /// <summary>
- /// Get a pointer to the window.
- /// </summary>
- public IntPtr Handle
- {
- get { return _handle; }
- }
- /// <summary>
- /// Get the text which is found in the window's title bar.
- /// </summary>
- public string Title
- {
- get
- {
- int length = GetWindowTextLength(Handle);
- var title = new StringBuilder(length + 1);
- GetWindowText(Handle, title, title.Capacity);
- return title.ToString();
- }
- }
- /// <summary>
- /// Get a boolean value indicating whether the window is visible or not.
- /// </summary>
- public bool IsVisible
- {
- get { return IsWindowVisible(Handle); }
- }
- /// <summary>
- /// Get an enumeration of all windows belonging to this window.
- /// </summary>
- public IEnumerable<Window> ChildWindows
- {
- get
- {
- var childWindowsTemp = new List<Window>();
- var callbackPointer = new EnumWindowsProc((hWnd, lParam) =>
- {
- childWindowsTemp.Add(new Window(hWnd));
- return true;
- });
- EnumChildWindows(Handle, callbackPointer, (IntPtr) 0);
- return childWindowsTemp;
- }
- }
- /// <summary>
- /// Get the flags representing the combination of window styles.
- /// </summary>
- public WindowStyleExtended StyleFlags
- {
- get
- {
- int numericFlags = GetWindowLong(Handle, GetWindowLongNIndex.GWL_EXSTYLE);
- return (WindowStyleExtended) Enum.Parse(typeof (WindowStyleExtended), numericFlags.ToString());
- }
- }
- /// <summary>
- /// Get the <see cref="System.Diagnostics.Process"/> which created the Window.
- /// </summary>
- public Process ParentProcess
- {
- get
- {
- IntPtr processId;
- GetWindowThreadProcessId(Handle, out processId);
- return Process.GetProcessById(processId.ToInt32());
- }
- }
- /// <summary>
- /// Get an enumeration of all windows.
- /// </summary>
- public static IEnumerable<Window> AllWindows
- {
- get
- {
- var openWindowsTemp = new List<Window>();
- var callbackPointer = new EnumWindowsProc((hWnd, lParam) =>
- {
- openWindowsTemp.Add(new Window(hWnd));
- return true;
- });
- EnumWindows(callbackPointer, (IntPtr) 0);
- return openWindowsTemp;
- }
- }
- /// <summary>
- /// Get the desktop window.
- /// </summary>
- public static Window DesktopWindow
- {
- get { return new Window(GetDesktopWindow()); }
- }
- /// <summary>
- /// Get an enumeration of all the windows which appear in the taskbar.
- /// </summary>
- public static IEnumerable<Window> TaskbarWindows
- {
- get { return DesktopWindow.ChildWindows.Where(window => window.StyleFlags.HasFlag(WindowStyleExtended.WS_EX_APPWINDOW)).ToList(); }
- }
- /// <summary>
- /// Get or set the foreground window.
- /// </summary>
- public static Window ForegroundWindow
- {
- get { return new Window(GetForegroundWindow()); }
- set
- {
- bool successful = SetForegroundWindow(value.Handle);
- if (!successful)
- {
- throw new WinApiException("Could not set foreground window.");
- }
- }
- }
- /// <summary>
- /// Minimise but do not destroy the window.
- /// </summary>
- public void Minimise()
- {
- CloseWindow(Handle);
- }
- /// <summary>
- /// Checks for the existence of any window whose title matches a given string (case-sensitive).
- /// </summary>
- /// <param name="title">The string to match.</param>
- /// <returns><c>true</c> or <c>false</c>, depending on whether a window with a matching title exists.</returns>
- public static bool Exists(string title)
- {
- return AllWindows.Any(window => window.Title == title);
- }
- // The definition of the method signature used for callback when enumerating windows.
- private delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement