You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 11:35:08