Tim*_*son 17
您可以执行以下Process.MainWindowHandle
操作:使用P/Invoke调用EnumWindows
函数,该函数为系统中的每个顶级窗口调用回调方法.
在回调中,调用GetWindowThreadProcessId
并比较窗口的进程ID Process.Id
; 如果进程ID匹配,则将窗口句柄添加到列表中.
Mez*_*Mez 10
首先,您必须获取应用程序主窗口的窗口句柄.
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
IntPtr hWnd = (IntPtr)FindWindow(windowName, null);
Run Code Online (Sandbox Code Playgroud)
然后,您可以使用此句柄来获取所有子窗口:
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool EnumChildWindows(IntPtr hwndParent, EnumWindowsProc lpEnumFunc, IntPtr lParam);
private 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;
}
Run Code Online (Sandbox Code Playgroud)