可以参考这个代码:
using System;
using System.Runtime.InteropServices;
class Program
{
// 导入必要的 Win32 API 函数
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);
private const uint GW_HWNDNEXT = 2;
static void Main()
{
IntPtr currentWindowHandle = GetForegroundWindow();
IntPtr nextWindowHandle = GetWindow(currentWindowHandle, GW_HWNDNEXT);
Console.WriteLine("当前前景窗口句柄: " + currentWindowHandle);
Console.WriteLine("下一个窗口句柄: " + nextWindowHandle);
}
}
![](https://files.getquicker.net/_sitefiles/kb/2024/07/09/171536_3_image.png)