System.Console.SetWindowSize 方法

方法描述

将控制台窗口的高度和宽度设置为指定值。

语法定义(C# System.Console.SetWindowSize 方法 的用法)

public static void SetWindowSize(
	int width,
	int height
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
width System-Int32 控制台窗口的宽度,以列为单位。
height System-Int32 控制台窗口的高度,以行为单位。
返回值 void

提示和注释

System.Console.SetWindowSize 方法例子

最后,按下任意键时,控制台窗口会还原为其原始维度,该示例终止。

// This example demonstrates the Console.SetWindowSize method,
//                           the Console.WindowWidth property, 
//                       and the Console.WindowHeight property.
using System;

class Sample 
{
    public static void Main() 
    {
    int origWidth, width;  
    int origHeight, height;
    string m1 = "The current window width is {0}, and the " +
                "current window height is {1}.";
    string m2 = "The new window width is {0}, and the new " +
                "window height is {1}.";
    string m4 = "  (Press any key to continue...)";
//
// Step 1: Get the current window dimensions.
//
    origWidth  = Console.WindowWidth;
    origHeight = Console.WindowHeight;
    Console.WriteLine(m1, Console.WindowWidth,
                          Console.WindowHeight);
    Console.WriteLine(m4);
    Console.ReadKey(true); 
//
// Step 2: Cut the window to 1/4 its original size.
//
    width  = origWidth/2;
    height = origHeight/2;
    Console.SetWindowSize(width, height);
    Console.WriteLine(m2, Console.WindowWidth,
                          Console.WindowHeight);
    Console.WriteLine(m4);
    Console.ReadKey(true); 
//
// Step 3: Restore the window to its original size.
//
    Console.SetWindowSize(origWidth, origHeight);
    Console.WriteLine(m1, Console.WindowWidth,
                          Console.WindowHeight);
    }
}
/*
This example produces the following results:

The current window width is 85, and the current window height is 43.
  (Press any key to continue...)
The new window width is 42, and the new window height is 21.
  (Press any key to continue...)
The current window width is 85, and the current window height is 43.

*/

异常

异常 异常描述
ArgumentOutOfRangeException
  • width 或 height 小于或等于零。
  • width 加 WindowLeft 或者 height 加 WindowTop 大于等于 Int16.MaxValue。
  • width 或 height 大于当前屏幕分辨率和控制台字体所允许的最大窗口宽度和高度。
SecurityException 该用户没有执行此操作的权限。
IOException 发生了 I/O 错误。

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。