历数从.net另一个应用程序的窗口/控件 [英] Enumerating Windows/Controls of another application from .Net

查看:278
本文介绍了历数从.net另一个应用程序的窗口/控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发需要检测另外一个人是否有它的MDI子窗口打开的一个小工具,应用程序(它是一个现成的现成的Win32业务应用上,我既没有源$ C ​​$ C和控制) 。
从我的应用程序,我希望能够投票或当一个特定的MDI子窗口打开检测。

I'm developing a small utility application that needs to detect whether another one has one of its MDI child windows open (it's an off-the-shelf Win32 business application over which I have neither source code nor control). From my app, I would like to be able to poll or detect when a particular MDI Child window is open.

在.NET中,可以很容易地遍历正在运行的进程,但我还没有找到一种简单的方式,通过从净给定的Win32过程(分)窗口和控件进行迭代。

In .Net, it's easy to iterate over running processes, but I haven't found an easy way to iterate through the (sub)windows and controls of a given Win32 process from .Net.

任何想法?

更新

感谢他们让我在正确的道路上的答案。

我发现了一个测试项目的文章同时使用 EnumWindows的 EnumChidWindows 和其他API调用来获得控制扩展信息。

Update
Thanks for the answers they got me on the right path.
I found an article with a test project that uses both EnumWindowsand EnumChidWindows and other API calls to get extended information on controls.

推荐答案

您必须使用本机的Win32 API。

You must use native Win32 API.

EnumChildWindows(USER32)

[DllImport("user32")]

[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr i);

/// <summary>
/// Returns a list of child windows
/// </summary>
/// <param name="parent">Parent of the windows to return</param>
/// <returns>List of child windows</returns>
public 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;
}

/// <summary>
/// Callback method to be used when enumerating windows.
/// </summary>
/// <param name="handle">Handle of the next window</param>
/// <param name="pointer">Pointer to a GCHandle that holds a reference to the list to fill</param>
/// <returns>True to continue the enumeration, false to bail</returns>
private 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);
//  You can modify this to check to see if you want to cancel the operation, then return a null here
return true;
}

/// <summary>
/// Delegate for the EnumChildWindows method
/// </summary>
/// <param name="hWnd">Window handle</param>
/// <param name="parameter">Caller-defined variable; we use it for a pointer to our list</param>
/// <returns>True to continue enumerating, false to bail.</returns>
public delegate bool EnumWindowProc(IntPtr hWnd, IntPtr parameter);

这篇关于历数从.net另一个应用程序的窗口/控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆