How to Resize a Console App in C# - Windows Terminal

Steph 61 Reputation points
2023-05-03T19:48:58.8+00:00

I am trying to create a Console game in C# with .net 6 framework.

My issue is in the formatting with, I believe, Windows Terminal- Currently, I am using Console.GetLargestConsoleWidth() to set the console to "full screen", and to help with centering my outputs in the console via a formula as below:

int PadSize = ((Console.LargestWindowWidth - 100) / 2);
public void Spacing()
{
	(_, int top) = Console.GetCursorPosition();  //<----The left value is not needed here because of PadSize
	Console.SetCursorPosition(PadSize, top);  //<-------Sets the cursor to a point calculated above on the screen
	Console.Write("||")  //<-------------------- -------This is to border the centered contents before every line
	Console.Write("Some line less than or equal to 100 characters here");
	Console.SetCursorPosition(PadSize + 100, top);  //<-Sets the cursor to the right side of the centered text
	Console.Write("||")  //<--------------------------- And this is the border on the right hand side
	top++;
}

//The output looks like below:(I will use a single '|' to simulate the edge of the window)

//|              ||Some line less than or equal to 100 characters here                         ||              |

//Where the blank space on the sides here is equal due to the formula based on the LargestWindowWidth

As I understand, using Console.LargestWindowWidth and its compatriots such as Console.GetLargestWindowWidth() are obsolete in Windows Terminal. When I have tried having my sister test the game on her PC (she has Windows 11, and Windows Terminal by default), the application throws an "Argument Out of Range" exception which confuses me considering I am attempting to use the Largest Console Width as determined by the program.

So, how do I accomplish the same thing as above in the Windows Terminal? So far it has run smoothly on computers that do not use Windows Terminal, but if the issue stems from something else entirely, I am all ears!

Thank you for reading!

Windows 10
Windows 10
A Microsoft operating system that runs on personal computers and tablets.
10,557 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,343 questions
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,572 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,193 questions
Windows 11
Windows 11
A Microsoft operating system designed for productivity, creativity, and ease of use.
8,092 questions
{count} votes

Accepted answer
  1. Jiale Xue - MSFT 30,581 Reputation points Microsoft Vendor
    2023-05-15T09:27:50.8066667+00:00

    Hi @Steph ,

    This method can maximize the Console window in windows11.

    It maximizes the console window and resizes it to fill the entire screen.

    You can modify the method you need according to this code.

    using System.Runtime.InteropServices;
    internal class Program
    {
        // Structure used by GetWindowRect
        struct Rect
        {
            public int Left;
            public int Top;
            public int Right;
            public int Bottom;
        }
        private static void Main(string[] args)
        {
            // Import the necessary functions from user32.dll
            [DllImport("user32.dll")]
            static extern IntPtr GetForegroundWindow();
            [DllImport("user32.dll")]
            static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
            [DllImport("user32.dll")]
            static extern bool GetWindowRect(IntPtr hWnd, out Rect lpRect);
            [DllImport("user32.dll")]
            static extern bool MoveWindow(IntPtr hWnd, int x, int y, int nWidth, int nHeight, bool bRepaint);
            // Constants for the ShowWindow function
            const int SW_MAXIMIZE = 3;
            // Get the handle of the console window
            IntPtr consoleWindowHandle = GetForegroundWindow();
            // Maximize the console window
            ShowWindow(consoleWindowHandle, SW_MAXIMIZE);
            // Get the screen size
            Rect screenRect;
            GetWindowRect(consoleWindowHandle, out screenRect);
            // Resize and reposition the console window to fill the screen
            int width = screenRect.Right - screenRect.Left;
            int height = screenRect.Bottom - screenRect.Top;
            MoveWindow(consoleWindowHandle, screenRect.Left, screenRect.Top, width, height, true);
            Console.ReadKey();
        }
    }
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Karen Payne MVP 35,031 Reputation points
    2023-05-03T22:00:40.8466667+00:00

    Your console app is hosted in the windows terminal so you need to find a way to resize the terminal which outside of code is wt -F. So this means there is not an exposed method to resize from your console app.


  2. Oliver Anthony 0 Reputation points
    2023-05-12T16:20:10.1566667+00:00

    To resize a console app in C# running in Windows Terminal, you can use the following code:

    using System;
    
    namespace ConsoleApp
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.SetWindowSize(100, 50); // set the size of the console window
                Console.SetBufferSize(100, 50); // set the size of the console buffer
                Console.WriteLine("Hello, world!"); // write some text to the console
                Console.ReadKey(); // wait for user input
            }
        }
    }
    
    

    In the above code, Console.SetWindowSize sets the size of the console window to 100 columns by 50 rows, and Console.SetBufferSize sets the size of the console buffer to the same dimensions.

    You can adjust the values passed to these methods to resize the console app as needed. Note that if you set the window or buffer size to be larger than the current size of the Windows Terminal window, the window will automatically resize to fit the new dimensions.

    Finally, the Console.WriteLine method writes some text to the console, and Console.ReadKey waits for the user to press a key before the program exits.
    See This Website: Amassed