Перейти из форума на сайт.

НовостиФайловые архивы
ПоискАктивные темыТоп лист
ПравилаКто в on-line?
Вход Забыли пароль? Первый раз на этом сайте? Регистрация
Компьютерный форум Ru.Board » Операционные системы » Microsoft Windows » Сценарии для Windows

Модерирует : KLASS, IFkO

 Версия для печати • ПодписатьсяДобавить в закладки
На первую страницук этому сообщениюк последнему сообщению

Открыть новую тему     Написать ответ в эту тему

iNNOKENTIY21



Silver Member
Редактировать | Профиль | Сообщение | Цитировать | Сообщить модератору


Код:

function EnumWindowText {
<#
    .EXAMPLE
    EnumWindowText "Блокнот"
    .LINK
    https://stackoverflow.com/questions/19867402/how-can-i-use-enumwindows-to-find-windows-with-a-specific-caption-title?noredirect=1&lq=1
#>
    param
    (
        # Заголовок или часть заголовка искомого окна
        [Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)]
        [ValidateNotNullOrEmpty()]
        [System.String]
        $Title
    )
 
    ($Unsafe = New-Object -TypeName System.CodeDom.Compiler.CompilerParameters
    ).CompilerOptions = '/unsafe'
 
    if (!('EnumWindowText' -as [type])) {
        Add-Type -CompilerParameters $Unsafe -TypeDefinition @'
        using System;
        using System.Collections.Generic;
        using System.Runtime.InteropServices;
        using System.Text;
 
        public class EnumWindowText
        {
            [DllImport("user32.dll", CharSet = CharSet.Unicode)]
            private static extern int GetWindowText(IntPtr hWnd, StringBuilder strText, int maxCount);
 
            [DllImport("user32.dll", CharSet = CharSet.Unicode)]
            private static extern int GetWindowTextLength(IntPtr hWnd);
 
            [DllImport("user32.dll")]
            private static extern bool EnumWindows(EnumWindowsProc enumProc, IntPtr lParam);
 
            // Delegate to filter which windows to include
            public delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
 
            /// <summary> Get the text for the window pointed to by hWnd </summary>
            public static string GetWindowText(IntPtr hWnd)
            {
                int size = GetWindowTextLength(hWnd);
 
                if (size > 0)
                {
                    var builder = new StringBuilder(size + 1);
                    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(EnumWindowsProc filter)
            {
                IntPtr found = IntPtr.Zero;
                List<IntPtr> windows = new List<IntPtr>();
 
                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;
            }
 
            /// <summary> Find all windows that contain the given title text </summary>
            /// <param name="titleText"> The text that the window title must contain. </param>
            public static IEnumerable<IntPtr> FindWindowsWithText(string titleText)
            {
                return FindWindows(delegate (IntPtr wnd, IntPtr param)
                {
                    return GetWindowText(wnd).Contains(titleText);
                });
            }
        }
'@
    }
 
    [EnumWindowText]::FindWindowsWithText($Title)
}


Всего записей: 3521 | Зарегистр. 16-08-2012 | Отправлено: 14:26 10-04-2019 | Исправлено: iNNOKENTIY21, 15:10 10-04-2019
Открыть новую тему     Написать ответ в эту тему

На первую страницук этому сообщениюк последнему сообщению

Компьютерный форум Ru.Board » Операционные системы » Microsoft Windows » Сценарии для Windows


Реклама на форуме Ru.Board.

Powered by Ikonboard "v2.1.7b" © 2000 Ikonboard.com
Modified by Ru.B0ard
© Ru.B0ard 2000-2024

BitCoin: 1NGG1chHtUvrtEqjeerQCKDMUi6S6CG4iC

Рейтинг.ru