Console.WindowWidth Propriedade
Definição
Obtém ou define a largura da janela do console.Gets or sets the width of the console window.
public:
static property int WindowWidth { int get(); void set(int value); };
[get: System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
[set: System.Runtime.Versioning.SupportedOSPlatform("windows")]
public static int WindowWidth { get; set; }
public static int WindowWidth { get; set; }
[<get: System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
[<set: System.Runtime.Versioning.SupportedOSPlatform("windows")>]
member this.WindowWidth : int with get, set
member this.WindowWidth : int with get, set
Public Shared Property WindowWidth As Integer
Valor da propriedade
A largura da janela do console medida em colunas.The width of the console window measured in columns.
- Atributos
Exceções
O valor da propriedade WindowWidth ou o valor da propriedade WindowHeight é menor ou igual a 0.The value of the WindowWidth property or the value of the WindowHeight property is less than or equal to 0.
- ou --or-
O valor da propriedade WindowHeight mais o valor da propriedade WindowTop é menor ou igual a MaxValue.The value of the WindowHeight property plus the value of the WindowTop property is greater than or equal to MaxValue.
- ou --or-
O valor da propriedade WindowWidth ou o valor da propriedade WindowHeight é maior do que a maior altura ou largura de janela possível para a fonte de console e resolução de tela atual.The value of the WindowWidth property or the value of the WindowHeight property is greater than the largest possible window width or height for the current screen resolution and console font.
Erro na leitura ou na gravação das informações.Error reading or writing information.
A operação set é invocada em um sistema operacional diferente do Windows.The set operation is invoked on an operating system other than Windows.
Exemplos
Este exemplo demonstra o SetWindowSize método e as WindowWidth Propriedades e WindowHeight .This example demonstrates the SetWindowSize method, and the WindowWidth and WindowHeight properties. Você deve executar o exemplo para ver o efeito total da alteração do tamanho da janela do console.You must run the example to see the full effect of changing the console window size.
O exemplo relata as dimensões de uma janela de console definida como 85 colunas e 43 linhas e, em seguida, aguarda um pressionamento de tecla.The example reports the dimensions of a console window set to 85 columns and 43 rows, then waits for a key press. Quando qualquer tecla é pressionada, as dimensões da janela do console são divididas, as novas dimensões são relatadas e o exemplo espera por outro pressionamento de tecla.When any key is pressed, the dimensions of the console window are halved, the new dimensions are reported, and the example waits for another key press. Por fim, quando qualquer tecla for pressionada, a janela do console será restaurada para suas dimensões originais e o exemplo será encerrado.Finally, when any key is pressed the console window is restored to its original dimensions and the example terminates.
// This example demonstrates the Console.SetWindowSize method,
// the Console.WindowWidth property,
// and the Console.WindowHeight property.
using namespace System;
int main()
{
int origWidth;
int width;
int origHeight;
int 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.
*/
// 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.
*/
' This example demonstrates the Console.SetWindowSize method,
' the Console.WindowWidth property,
' and the Console.WindowHeight property.
Class Sample
Public Shared Sub Main()
Dim origWidth, width As Integer
Dim origHeight, height As Integer
Dim m1 As String = "The current window width is {0}, and the " & _
"current window height is {1}."
Dim m2 As String = "The new window width is {0}, and the new " & _
"window height is {1}."
Dim m4 As String = " (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)
End Sub
End Class
'
'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.
'
'
Comentários
A tentativa de definir o valor da WindowWidth propriedade quando a saída é redirecionada gera uma ArgumentOutOfRangeException ou uma IOException exceção.Attempting to set the value of the WindowWidth property when output is redirected throws either an ArgumentOutOfRangeException or an IOException exception. Para evitar uma exceção, você pode definir o valor dessa propriedade somente se a IsOutputRedirected Propriedade retornar false .To prevent an exception, you can set the value of this property only if the IsOutputRedirected property returns false.