c#判断控制台是否
时间: 2023-07-27 10:09:55 浏览: 62
我理解您的问题是判断控制台窗口是否处于活动状态。在 C# 中,可以使用 `GetForegroundWindow` 函数来获取当前处于活动状态的窗口句柄,然后使用 `GetConsoleWindow` 函数获取控制台窗口的句柄,最后比较两个句柄是否一致即可判断控制台窗口是否处于活动状态。
以下是示例代码:
```csharp
using System;
using System.Runtime.InteropServices;
class Program
{
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
[DllImport("kernel32.dll", ExactSpelling = true)]
static extern IntPtr GetConsoleWindow();
static void Main(string[] args)
{
IntPtr foregroundWindow = GetForegroundWindow();
IntPtr consoleWindow = GetConsoleWindow();
// 判断控制台窗口是否处于活动状态
if (foregroundWindow == consoleWindow)
{
Console.WriteLine("控制台窗口处于活动状态");
}
else
{
Console.WriteLine("控制台窗口未处于活动状态");
}
Console.ReadKey();
}
}
```