gpt4 book ai didi

c# - 在 C# 中将控制台窗口置于最前面

转载 作者:可可西里 更新时间:2023-11-01 08:55:04 26 4
gpt4 key购买 nike

如何在 C# 中将控制台应用程序窗口置于最前面(尤其是在运行 Visual Studio 调试器时)?

最佳答案

它很老套,很可怕,但对我有用(谢谢,pinvoke.net!):

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;

public class Test
{

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);

[DllImport("user32.dll", EntryPoint="FindWindow", SetLastError = true)]
static extern IntPtr FindWindowByCaption(IntPtr zeroOnly, string lpWindowName);

public static void Main()
{
string originalTitle = Console.Title;
string uniqueTitle = Guid.NewGuid().ToString();
Console.Title = uniqueTitle;
Thread.Sleep(50);
IntPtr handle = FindWindowByCaption(IntPtr.Zero, uniqueTitle);

if (handle == IntPtr.Zero)
{
Console.WriteLine("Oops, cant find main window.");
return;
}
Console.Title = originalTitle;

while (true)
{
Thread.Sleep(3000);
Console.WriteLine(SetForegroundWindow(handle));
}
}
}

关于c# - 在 C# 中将控制台窗口置于最前面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/213480/

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