在循环开头写了个readkey 回车来控制每一次重复循环,有没有办法在运行到那的时候让控制台主动转移焦点?谢谢各位了
2条回答 默认 最新
wanghui0380 2022-07-13 10:43关注using System.Diagnostics; using System.Runtime.InteropServices; [DllImport("kernel32.dll")] static extern IntPtr GetConsoleWindow(); [DllImport("user32.dll")] static extern bool SetForegroundWindow(IntPtr hWnd); [DllImport("user32.dll")] static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); //获取控制台窗体句柄 var handle = GetConsoleWindow(); while (true) { Console.WriteLine("提示:请输入"); //重新设置窗体显示为Normal ShowWindow(handle, 1); //将窗体带回前台激活 SetForegroundWindow(handle); await Task.Delay(10 * 1000); } Console.ReadKey();本回答被题主选为最佳回答 , 对您是否有帮助呢?评论 打赏 举报解决 1无用