Реализовать метод который будет находить дискриптор дочернего окна по заголовку используя EnumChildWindows - C#
Формулировка задачи:
Помогите создать метод который будет находить дискриптор дочернего окна по заголовку используя EnumChildWindows.
Решение задачи: «Реализовать метод который будет находить дискриптор дочернего окна по заголовку используя EnumChildWindows»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
class Window
{
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
delegate bool EnumWindowProc(IntPtr hWnd, IntPtr parameter);
[DllImport("user32")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr i);
[DllImport("user32.dll", SetLastError = true)]
static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true)]
static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll", SetLastError = true)]
static extern int GetClassName(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
static bool EnumWindow(IntPtr handle, IntPtr pointer)
{
GCHandle gch = GCHandle.FromIntPtr(pointer);
List<IntPtr> list = gch.Target as List<IntPtr>;
if (list == null)
throw new InvalidCastException("GCHandle Target could not be cast as List<IntPtr>");
list.Add(handle);
return true;
}
static List<IntPtr> GetChildWindows(IntPtr parent)
{
List<IntPtr> result = new List<IntPtr>();
GCHandle listHandle = GCHandle.Alloc(result);
try
{
EnumWindowProc childProc = new EnumWindowProc(EnumWindow);
EnumChildWindows(parent, childProc, GCHandle.ToIntPtr(listHandle));
}
finally
{
if (listHandle.IsAllocated)
listHandle.Free();
}
return result;
}
static string GetWindowText(IntPtr hWnd)
{
int len = GetWindowTextLength(hWnd) + 1;
StringBuilder sb = new StringBuilder(len);
len = GetWindowText(hWnd, sb, len);
return sb.ToString(0, len);
}
static string GetWindowClass(IntPtr hWnd)
{
int len = 260;
StringBuilder sb = new StringBuilder(len);
len = GetClassName(hWnd, sb, len);
return sb.ToString(0, len);
}
public static IntPtr get_hwnd_child_window_by_name(string WindowName, string ChildWindowName)
{
IntPtr hwnd_window = FindWindow(null, WindowName);
List<IntPtr> all_hwnd_child_window = GetChildWindows(hwnd_window);
foreach (var hwnd_child_window in all_hwnd_child_window)
{
if (GetWindowText(hwnd_child_window) == ChildWindowName)
{
return hwnd_child_window;
}
}
return IntPtr.Zero;
}
}