我正在使用C#编写控制台应用程序,需要将控制台最大化。但当我点击控制台窗口的最大化按钮时,它只会在高度上最大化而不是宽度上。我尝试使用以下代码:
Console.WindowWidth = 150;
Console.WindowHeight = 61;
在我的电脑上几乎按照我想要的方式工作,但在其他一些电脑上会出现错误。 我该怎么做才能最大化控制台?
我正在使用C#编写控制台应用程序,需要将控制台最大化。但当我点击控制台窗口的最大化按钮时,它只会在高度上最大化而不是宽度上。我尝试使用以下代码:
Console.WindowWidth = 150;
Console.WindowHeight = 61;
在我的电脑上几乎按照我想要的方式工作,但在其他一些电脑上会出现错误。 我该怎么做才能最大化控制台?
无法使用CLR。需要导入Win32 API调用并操纵你的容器窗口。以下内容可能有所帮助。
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
}
[DllImport("kernel32.dll", ExactSpelling = true)]
private static extern IntPtr GetConsoleWindow();
private static IntPtr ThisConsole = GetConsoleWindow();
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
private const int HIDE = 0;
private const int MAXIMIZE = 3;
private const int MINIMIZE = 6;
private const int RESTORE = 9;
static void Main(string[] args)
{
ShowWindow(ThisConsole, MINIMIZE);
}
FindWindowByCaption
函数。 - nawfalShowWindow(ThisConsole, MAXIMIZE);
。 - MojtabaConsole.SetWindowSize(Console.LargestWindowWidth, Console.LargestWindowHeight)
using System.Runtime.InteropServices;
[DllImport("kernel32.dll", ExactSpelling = true)]
static extern IntPtr GetConsoleWindow();
IntPtr ThisConsole = GetConsoleWindow();
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
const int HIDE = 0;
const int MAXIMIZE = 3;
const int MINIMIZE = 6;
const int RESTORE = 9;
ShowWindow(ThisConsole, MAXIMIZE);