C
C#•10mo ago
blackSheep

Resizing the Console window - Console Application

Hi there, I'm trying to resize the console window in a Console Application. I have tried multiple things and some samples from the MSDN to get this to work but so far didn't manage to get the console to work.
namespace ConsoleApp1
{
internal class Program
{
static void Main(string[] args)
{
Console.SetWindowSize(100, 100);
Console.WriteLine("{0} , {1}",Console.WindowWidth, Console.WindowHeight);
Console.ReadKey();

Console.SetWindowSize(50, 50);
Console.WriteLine("{0} , {1}", Console.WindowWidth, Console.WindowHeight);
}
}
}
namespace ConsoleApp1
{
internal class Program
{
static void Main(string[] args)
{
Console.SetWindowSize(100, 100);
Console.WriteLine("{0} , {1}",Console.WindowWidth, Console.WindowHeight);
Console.ReadKey();

Console.SetWindowSize(50, 50);
Console.WriteLine("{0} , {1}", Console.WindowWidth, Console.WindowHeight);
}
}
}
7 Replies
blackSheep
blackSheepOP•10mo ago
I'm using windows 11 and visual studio Community version 2022
Unknown User
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
ZacharyPatten
ZacharyPatten•10mo ago
what kind of program are you making? I like making console games and what I do is have a check to see if the window is large enough to render the current view. If it is not large enough then I just write "Please increase the size of the console window" rather than rendering the current view
blackSheep
blackSheepOP•10mo ago
I had this doubt it at the back of my mind and now it seems pretty clear what you just said. Thanks to take the time to answer 🙂 I planning to create a consolePad app (like a notepad Console version ) and one the things that I wanted was to have the size of that window to be resized.
ZacharyPatten
ZacharyPatten•10mo ago
The main issue is that from my experience, the methods in the BCL for setting the console windows size are not cross platform and don't even work on all terminals in windows, such as Terminal. So I would recommend not trying to change the window size in your code, but rather let the user manage that, and jsut tell them if their window isn't large enough in other words... I wish Console.SetWindowSize worked, but it just doesn't 😛
Jester
Jester•10mo ago
to resize the console window in windows (but it has to be a console window, not the new terminal) you have to dll import some functions
var windowHandle = GetConsoleWindow();
SetWindowPos(windowHandle, default, posx, posy, width, heitht, flags);
var windowHandle = GetConsoleWindow();
SetWindowPos(windowHandle, default, posx, posy, width, heitht, flags);
https://learn.microsoft.com/en-us/windows/console/getconsolewindow https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwindowpos https://learn.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.dllimportattribute?view=net-8.0
GetConsoleWindow function - Windows Console
Retrieves the window handle used by the console associated with the calling process.
SetWindowPos function (winuser.h) - Win32 apps
Changes the size, position, and Z order of a child, pop-up, or top-level window. These windows are ordered according to their appearance on the screen. The topmost window receives the highest rank and is the first window in the Z order.
DllImportAttribute Class (System.Runtime.InteropServices)
Indicates that the attributed method is exposed by an unmanaged dynamic-link library (DLL) as a static entry point.
Jester
Jester•10mo ago
@blackSheep
Want results from more Discord servers?
Add your server