Widow – Rev 26

Subversion Repositories:
Rev:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

namespace Widow
{
    public static class Helpers
    {
        #region Public Methods

        /// <summary> Get the text for the window pointed to by hWnd </summary>
        public static string GetWindowText(IntPtr hWnd)
        {
            var size = Natives.GetWindowTextLength(hWnd);
            if (size > 0)
            {
                var builder = new StringBuilder(size + 1);
                Natives.GetWindowText(hWnd, builder, builder.Capacity);
                return builder.ToString();
            }

            return string.Empty;
        }

        /// <summary> Find all windows that match the given filter </summary>
        /// <param name="filter">
        ///     A delegate that returns true for windows
        ///     that should be returned and false for windows that should
        ///     not be returned
        /// </param>
        public static IEnumerable<IntPtr> FindWindows(Natives.EnumWindowsProc filter)
        {
            var windows = new List<IntPtr>();

            Natives.EnumWindows(delegate(IntPtr wnd, IntPtr param)
            {
                if (filter(wnd, param))
                {
                    // only add the windows that pass the filter
                    windows.Add(wnd);
                }

                // but return true here so that we iterate all windows
                return true;
            }, IntPtr.Zero);

            return windows;
        }

        public static string GetWindowClass(IntPtr hWnd)
        {
            var windowClass = new StringBuilder(256);
            Natives.GetClassName(hWnd, windowClass, windowClass.Capacity);
            return windowClass.ToString();
        }

        public static string GetWindowTitle(IntPtr hWnd)
        {
            var length = Natives.GetWindowTextLength(hWnd) + 1;
            var title = new StringBuilder(length);
            Natives.GetWindowText(hWnd, title, length);
            return title.ToString();
        }

        public static string GetProcessName(IntPtr hWnd)
        {
            Natives.GetWindowThreadProcessId(hWnd, out var pid);
            try
            {
                var process = Process.GetProcessById((int) pid);
                return process.ProcessName;
            }
            catch
            {
                return string.Empty;
            }
        }

        #endregion
    }
}

Generated by GNU Enscript 1.6.5.90.