C
C#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);
}
}
}
B
blackSheep41d ago
I'm using windows 11 and visual studio Community version 2022
UU
Unknown User41d ago
Z
ZacharyPatten41d 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
B
blackSheep40d 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.
Z
ZacharyPatten40d 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 😛
J
Jester37d 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.
J
Jester37d ago
@blackSheep
Want results from more Discord servers?
Add your server
More Posts
✅ Creating a datetime with a timezoneHow do I create a `DateTime` with `new DateTime(2010, 05, 12);` and give it a specific `TimeZoneInfoCustom Newtonsoft JSON deserializationI have a dictionary where: - The key will always be a string - The value will be either: - string Issue with Form.Close() in WinformsI have a custom form called CustomMessageBox In that form, I have a method that defines a button clIssue with IMGUI.Net.Hello, I am creating an ImGUI application and It doesn't let me move the ui across my entire screen,Issue with deserializing Enums in saved JSONHey, I've been trying to deserialize a list of cards I serialized in C#, the issue I'm having is it From HttpClient, can I save/copy the request to share with someone?Is there a way to copy raw request details? For example, I'm looking for a similar output like: -3D Unity RaycastingHi guys, currently working on a puzzle game involving lasers and mirrors, however, have hit a brick Computer Science - Computer ArchitectureOn a 64 bit system (where the bus length and word size are 64 bits) how would we determine the ammouConnecting SQLite in MAUIHow to connect the database without absolute path? If with it, the program works only on the computeDifferent HttpClient headersWhen I make an HTTP request in my browser (Chrome), I get different response headers than I do in myHttpRequestException:An error occurred while sending the request. WebExceptionHttpRequestException:An error occurred while sending the request. WebException:Unable to connect to ✅ EF does not put datetimes properly to the datebaseI am writing a backend app in asp.net core. This is my service code : ```c# public virtual async TaShadow Prop Error with IdentityDbContextThis is my IdentityDbContext: public class ApplicationUser : IdentityUser { [Required(ErrorMessWpf custom button template probleHi! I am trying to create custom button control, but I have a problem with a content property. ButtoWinfoms problem in Hosts fileI am writing a mini site blocker, the program has 2 functions to block the site and add the site thaDTO validation with DRY principle (without repeating the same validation)Hello, I would like to know what is used in practice, regarding the DTO validation. Let's say for 2 I'm using fedora, and i want create some .NET formsWhat should i do to create projects with ASP.NET forms in Linux system(Fedora)?✅ My API returns 500 internal server error with the SQL exception text, why?Hello every one, I built an ASP.NET API in .NET and I wrote a controller Action that looks like thiAdvice on how to correctly integrate more complex Javascript codeHey, I've developed an ASP.NET app before, but that one was pretty much all about the backend and t✅ A generic error occurred in GDI+.' -> Save Image PathHey, I been trying to making some face recognition using EMGU.CV, but I faces a problem which, when