如何正确声明EnumChildWindows,GCHandle,EnumWindow,EnumWindowProc及其参数? [英] How to declare EnumChildWindows, GCHandle, EnumWindow, EnumWindowProc with theirs parameters correctly?
本文介绍了如何正确声明EnumChildWindows,GCHandle,EnumWindow,EnumWindowProc及其参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好!
我在EnumChildWindows,GCHandle,EnumWindow,EnumWindowProc中遇到参数问题。我想从应用程序中获取句柄,findWindowEx仅在第一个子窗口上工作,我想要第二个句柄而不是第一个。
我不知道他们的参数是什么,除了句柄(hwnd)。请帮我正确申报/填写。
以下是pinvoke.net的代码:
Hi Everyone!
I have a problem with parameters in EnumChildWindows, GCHandle, EnumWindow, EnumWindowProc. I wanna get the handles from an application and the findWindowEx works only on the first child window and I wanna get the second handle not the first.
I don't know what their parameters are except the handle (hwnd). Please help me to correctly declare / fill them.
Here is a code from pinvoke.net:
using System.Runtime.InteropServices;
[DllImport("user32.dll")]
[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);
推荐答案
您需要的信息都在文档中。从 https://msdn.microsoft.com/开始en-us / library / windows / desktop / ms633497%28v = vs.85%29.aspx [ ^ ]。
The information you want is all in the documentation. Start at https://msdn.microsoft.com/en-us/library/windows/desktop/ms633497%28v=vs.85%29.aspx[^].
这篇关于如何正确声明EnumChildWindows,GCHandle,EnumWindow,EnumWindowProc及其参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文