C# .NET Framework控制台无法通过代码最大化问题求助
.NET Framework控制台应用窗口最大化失败问题
环境信息
- 项目类型:Console App (.NET Framework)
- .NET版本:4.7.2
- 操作系统:Windows 11(使用Windows终端作为命令行宿主)
问题描述
尝试通过C#代码实现控制台窗口最大化,但多次尝试均未成功;替换为最小化逻辑时,窗口能正常响应。以下是已尝试的两段代码:
示例1代码
using System; using System.Runtime.InteropServices; [DllImport("kernel32.dll", ExactSpelling = true)] private static extern IntPtr GetConsoleWindow(); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); private const int MAXIMIZE = 3; private const int MINIMIZE = 6; static void Main(string[] args) { ShowWindow(GetConsoleWindow(), MAXIMIZE); Console.ReadKey(); }
测试结果:将
MAXIMIZE替换为MINIMIZE时,窗口可正常最小化;使用MAXIMIZE参数无法实现最大化。
示例2代码
using System; using System.Diagnostics; using System.Runtime.InteropServices; [DllImport("user32.dll")] public static extern bool ShowWindow(System.IntPtr hWnd, int cmdShow); private static void Maximize() { Process p = Process.GetCurrentProcess(); ShowWindow(p.MainWindowHandle, 3); // SW_MAXIMIZE = 3 } static void Main(string[] args) { Maximize(); Console.ReadKey(); }
测试结果:同样无法实现窗口最大化。
内容的提问来源于stack exchange,提问作者Quidney
相关产品推荐
相关产品推荐

