Console.WindowWidth Eigenschap
Definitie
Belangrijk
Bepaalde informatie heeft betrekking op een voorlopige productversie die aanzienlijk kan worden gewijzigd voordat deze wordt uitgebracht. Microsoft biedt geen enkele expliciete of impliciete garanties met betrekking tot de informatie die hier wordt verstrekt.
Hiermee haalt u de breedte van het consolevenster op of stelt u deze in.
public:
static property int WindowWidth { int get(); void set(int value); };
public static int WindowWidth { get; set; }
static member WindowWidth : int with get, set
Public Shared Property WindowWidth As Integer
Waarde van eigenschap
De breedte van het consolevenster dat wordt gemeten in kolommen.
Uitzonderingen
De waarde van de WindowWidth eigenschap of de waarde van de WindowHeight eigenschap is kleiner dan of gelijk aan 0.
– of –
De waarde van de WindowHeight eigenschap plus de waarde van de WindowTop eigenschap is groter dan of gelijk aan Int16.MaxValue.
– of –
De waarde van de WindowWidth eigenschap of de waarde van de WindowHeight eigenschap is groter dan de grootst mogelijke breedte of hoogte van het venster voor de huidige schermresolutie en het lettertype van de console.
Fout bij het lezen of schrijven van informatie.
De setbewerking wordt aangeroepen op een ander besturingssysteem dan Windows.
Voorbeelden
In dit voorbeeld ziet u de SetWindowSize methode en de WindowWidth eigenschappen WindowHeight . U moet het voorbeeld uitvoeren om het volledige effect te zien van het wijzigen van de grootte van het consolevenster.
In het voorbeeld worden de dimensies van een consolevenster gerapporteerd dat is ingesteld op 85 kolommen en 43 rijen en wacht vervolgens op een toets. Wanneer een toets wordt ingedrukt, worden de afmetingen van het consolevenster gehalveerd, worden de nieuwe dimensies gerapporteerd en wacht het voorbeeld op een andere toets. Ten slotte wordt, wanneer een toets op het consolevenster wordt gedrukt, teruggezet naar de oorspronkelijke afmetingen en wordt het voorbeeld beëindigd.
// 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.
open System
//
// Step 1: Get the current window dimensions.
//
let origWidth = Console.WindowWidth
let origHeight = Console.WindowHeight
printfn $"The current window width is {Console.WindowWidth}, and the current window height is {Console.WindowHeight}."
printfn " (Press any key to continue...)"
Console.ReadKey true |> ignore
//
// Step 2: Cut the window to 1/4 its original size.
//
let width = origWidth / 2
let height = origHeight / 2
Console.SetWindowSize(width, height)
printfn $"The new window width is {Console.WindowWidth}, and the new window height is {Console.WindowHeight}."
printfn " (Press any key to continue...)"
Console.ReadKey true |> ignore
//
// Step 3: Restore the window to its original size.
//
Console.SetWindowSize(origWidth, origHeight)
printfn $"The current window width is {Console.WindowWidth}, and the current window height is {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.
'
'
Opmerkingen
Als u probeert de waarde van de WindowWidth eigenschap in te stellen wanneer uitvoer wordt omgeleid, wordt een ArgumentOutOfRangeException of een IOException uitzondering gegenereerd. Als u een uitzondering wilt voorkomen, kunt u de waarde van deze eigenschap alleen instellen als de IsOutputRedirected eigenschap wordt geretourneerd false.