Console.SetWindowPosition() Method in C#

In this article, we will discuss the Console.SetWindowPosition() method in C# with its syntax and example.

Introduction

Developers may precisely control where the console window appears on the screen by using the Console.SetWindowPosition() function in C#. This function, which is a component of the Console class in the .NET framework, enables dynamic modifications to the visual layout of console-based applications. Developers can control the precise location of the console window on the user's display by providing the required horizontal and vertical coordinates. This feature significantly enhances the user experience, particularly when a certain window arrangement or layout is important. Console.SetWindowPosition() offers an adaptable way to customize the appearance and behavior of C# console programs, regardless of whether readability, compatibility with additional on-screen components, or design specifications are met.

Console.SetWindowPosition() Method in C#

Syntax:

It has the following syntax:

left: It indicates how far the console window is positioned horizontally (in pixels) away from the screen's left border.

top: It indicates the console window's vertical position (measured in pixels) relative to the highest edge of the display.

Potential Uses:

There are several uses of the Console.SetWindowPosition() function in C#. Some main uses of the Console.SetWindowPosition() function are as follows:

  1. Customized Layout: Reposition the console window on the display to meet a specific arrangement or design specification.
  2. Screen Real Estate Management: Make sure that the console window is situated as close to other displayed on the screen components or apps as possible.
  3. Enhanced User Experience: By carefully placing the terminal window in a way to allows for optimal interaction between users, we can improve readability and usability.
  4. Applications for numerous Console Windows: These applications allow individuals to dynamically set the locations of numerous console windows by application logic or input from the user.
  5. Presentation and Demonstration: Set up console windows to emphasize particular characteristics or materials during demonstrations.
  6. Applications for Kiosks: Manage the console window's location in applications for Kiosks to offer a standardized and user-friendly interface.

Example:

Let us take an example to illustrate the Console.SetWindowPosition() Method in C#.

Output:

Use arrow keys to move the window.
Current window position: Left: 0, Top: 0

Explanation:

1. Configuring the Console:

The console is employed to change the title of the control panel window. The "Positioned Console Window Game" is the property title.

The console window's dimensions are configured using Console with a dimension of 40 columns wide and 20 rows high. Employ the SetWindowSize() function.

2. Setting up Variables:

For keeping track of the location of the console window, we characterize two integer variables: windowTop and windowLeft.

The window's beginning location at the top-left corner of the display is indicated by setting window left and window top to 0.

3. Loop of Game Play:

Once the gameRunning flag is changed to false, we initiate a while loop (while (gameRunning)).
We clean the terminal screen (Console.clean()) inside the loop to reactivate the display's appearance.

Use the SetCursorPosition() method for entering game instructions and information while setting the cursor position to (0, 0).

The windowLeft and windowTop variables are used by SetWindowPosition() to constantly set the command line window's position.

4. Handling User Input:

We use the Console to patiently wait for a key hit. Use the ReadKey(true) method to read just a single key from the terminal input without showing it.

A switch statement has been used to manage distinct key presses:

We reduce the window top value when the user pushes the up arrow key to move the window up.

Simply raise the window top value when the user pushes the down arrow key to slide the window down.

We decrease our window left value when the user pushes the left arrow key to shift the window left. We improve the window left amount when an individual pushes the downward arrow key to shift the window right. After that, we set gameRunning to false to end the current game loop if the user hits the keyboard's Escape key.






Latest Courses