0

我的本地 PC 上有大约 15 个不同的控制台应用程序,它们以不同的时间段作为计划任务运行。

由于我将此计算机用作个人用途(例如在 YouTube 上冲浪或观看电影),它们在我的屏幕上跳跃,但我必须始终手动将它们最小化。

我的目标是,我希望它们首先出现(已经在这样做)并在几秒钟后自动失去焦点。

是否可以在 Windows 上使用控制台应用程序?

4

1 回答 1

3

如果要最小化控制台窗口,可以使用 WinApi

const Int32 SW_MINIMIZE = 6;

[DllImport("Kernel32.dll", CallingConvention = CallingConvention.StdCall, SetLastError = true)]
private static extern IntPtr GetConsoleWindow();

[DllImport("User32.dll", CallingConvention = CallingConvention.StdCall, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool ShowWindow([In] IntPtr hWnd, [In] Int32 nCmdShow);

private static void MinimizeConsoleWindow()
{
    IntPtr hWndConsole = GetConsoleWindow();
    ShowWindow(hWndConsole, SW_MINIMIZE);
}

用法:

static void Main(string[] args)
{
    Console.WriteLine("Starting foo...");
    Thread.Sleep(1000); // hold console for a second on the screen
    MinimizeConsoleWindow();
    Console.ReadKey();
}
于 2012-11-17T23:13:33.543 回答