gpt4 book ai didi

c# - 如果所有者 hwnd、类名和窗口大小已知,则获取窗口 hwnd

转载 作者:太空宇宙 更新时间:2023-11-03 11:01:57 26 4
gpt4 key购买 nike

我需要一些具体的帮助。我知道类名、所有者 hwnd 和窗口的大小。我如何获得它的hwnd?或者,至少,获取满足这些条件的所有窗口的列表。

提前致谢!

瓦利

最佳答案

您可以尝试使用此代码。它将为您提供每个具有特定类名的顶级窗口的 IntPtr(窗口句柄):

/// <summary>
/// Returns a sequence of window handles (IntPtrs) for all top-level windows
/// matching the specfied window class name.
/// </summary>
/// <param name="className">The windows class name to match (not to be confused with a C# class name!)</param>
/// <returns>A non-null sequence of window handles. This will be an empty sequence if no windows match the class name.</returns>

public static IEnumerable<IntPtr> WindowsMatchingClassName(string className)
{
if (string.IsNullOrWhiteSpace(className))
{
throw new ArgumentOutOfRangeException("className", className, "className can't be null or blank.");
}

return WindowsByClassFinder.WindowsMatching(className);
}

/// <summary>Finds windows matching a particular window class name.</summary>

private class WindowsByClassFinder
{
/// <summary>Find the windows matching the specified class name.</summary>

public static IEnumerable<IntPtr> WindowsMatching(string className)
{
return new WindowsByClassFinder(className)._result;
}

private WindowsByClassFinder(string className)
{
_className = className;
EnumWindows(callback, IntPtr.Zero);
}

private bool callback(IntPtr hWnd, IntPtr lparam)
{
if (GetClassName(hWnd, _apiResult, _apiResult.Capacity) != 0)
{
if (string.CompareOrdinal(_apiResult.ToString(), _className) == 0)
{
_result.Add(hWnd);
}
}

return true; // Keep enumerating.
}

[SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage"), SuppressUnmanagedCodeSecurity]
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private extern static bool EnumWindows(EnumWindowsDelegate lpEnumFunc, IntPtr lparam);

[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);

private delegate bool EnumWindowsDelegate(IntPtr hWnd, IntPtr lparam);

private readonly string _className;
private readonly List<IntPtr> _result = new List<IntPtr>();
private readonly StringBuilder _apiResult = new StringBuilder(1024);
}

关于c# - 如果所有者 hwnd、类名和窗口大小已知,则获取窗口 hwnd,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17378973/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com