Console window size in C
MrSteinfeld
-
[Dal] Posted messages 6205 Registration date Status Contributeur Last intervention -
[Dal] Posted messages 6205 Registration date Status Contributeur Last intervention -
Good evening,
Here is my problem.
I am developing a C program that displays elements on the console.
However, when I compile, the window doesn’t open wide enough to display everything properly, and when I switch to fullscreen, the characters of one of the elements I display 2 seconds after opening the console are slightly offset.
So I have been looking for a solution to get a larger console window when I compile.
I found out about the function:
SetWindowPos(HWND, hWnd, int X, int Y, cx,cy,UINT uFlags);
However, I don't understand anything about the arguments I need to enter between the parentheses except for int X, int Y, cx,cy.
Could someone please explain to me what I should put so that the function works correctly?
And if anyone knows another method to manage the size of the console window in C, I am also open to that, please?
Thank you in advance.
Have a good evening.
MrSteinfeld
Configuration: Windows / Chrome 96.0.4664.110
Here is my problem.
I am developing a C program that displays elements on the console.
However, when I compile, the window doesn’t open wide enough to display everything properly, and when I switch to fullscreen, the characters of one of the elements I display 2 seconds after opening the console are slightly offset.
So I have been looking for a solution to get a larger console window when I compile.
I found out about the function:
SetWindowPos(HWND, hWnd, int X, int Y, cx,cy,UINT uFlags);
However, I don't understand anything about the arguments I need to enter between the parentheses except for int X, int Y, cx,cy.
Could someone please explain to me what I should put so that the function works correctly?
And if anyone knows another method to manage the size of the console window in C, I am also open to that, please?
Thank you in advance.
Have a good evening.
MrSteinfeld
Configuration: Windows / Chrome 96.0.4664.110
2 réponses
Hi MrSteinfeld,
SetWindowPos() is used to position a window of a Windows graphical application.
To act on the CMD.exe terminal window, you have specific functions in the Windows API.
The SetConsoleWindowInfo() function:
https://docs.microsoft.com/en-us/windows/console/setconsolewindowinfo
Using it is no small feat. You must:
This gives you:
(code tested under Windows 10 with the MinGW provided by Codeblocks)
Otherwise, in the current Microsoft documentation, the use of SetConsoleWindowInfo() is now discouraged by them (even though they indicate that they will continue to support it for the future) and they suggest using escape sequences (which only work on recent Windows 10 systems that support them).
https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences#window-width
SetConsoleWindowInfo() has been supported since Windows 2000 :-)
Dal
SetWindowPos() is used to position a window of a Windows graphical application.
To act on the CMD.exe terminal window, you have specific functions in the Windows API.
The SetConsoleWindowInfo() function:
https://docs.microsoft.com/en-us/windows/console/setconsolewindowinfo
Using it is no small feat. You must:
- obtain a handle to the CMD.exe window
- obtain a handle to the standard output
- get the maximum size for the largest possible console window, based on the current font and display size
- modify the screen buffer size of the CMD console you are working on
- reactivate the CMD console window by asking the system to maximize it
This gives you:
#include <stdio.h> #include <windows.h> int main(void) { printf("Please press Enter\n"); getchar(); HWND console_window = GetConsoleWindow(); if (!console_window) { printf("GetConsoleWindow() failed\n"); return EXIT_FAILURE; } HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE); if (handle_out == INVALID_HANDLE_VALUE) return GetLastError(); COORD largest_size = GetLargestConsoleWindowSize(handle_out); if (!largest_size.X && !largest_size.Y) return GetLastError(); largest_size.X--; largest_size.Y--; if (!SetConsoleScreenBufferSize(handle_out, largest_size)) { return GetLastError(); } if (!ShowWindow(console_window, SW_MAXIMIZE)) { printf("ShowWindow() failed\n"); return EXIT_FAILURE; } printf("This program continues now in the "largest possible terminal window\n"); printf("Please press Enter to finish this magnificent program\n"); getchar(); return 0; } (code tested under Windows 10 with the MinGW provided by Codeblocks)
Otherwise, in the current Microsoft documentation, the use of SetConsoleWindowInfo() is now discouraged by them (even though they indicate that they will continue to support it for the future) and they suggest using escape sequences (which only work on recent Windows 10 systems that support them).
https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences#window-width
SetConsoleWindowInfo() has been supported since Windows 2000 :-)
Dal
Hello,
I'm looking for something similar as well.
I'm using Linux Mint 20.1 and developing
with Code::Blocks 20.3.
Currently, I'm considering working around the problem
by installing a 5-second "timer" to give me time to enlarge it and refresh
the screen.
This is somewhat what I'm doing in my 80X24 version as well.
However, I'm still looking for a more elegant solution.
I'm connecting to your thread, in case I find someone who knows the solution...
I'm using gotoxy(x,y) to read/write to the screen.
Michel.
I'm looking for something similar as well.
I'm using Linux Mint 20.1 and developing
with Code::Blocks 20.3.
Currently, I'm considering working around the problem
by installing a 5-second "timer" to give me time to enlarge it and refresh
the screen.
This is somewhat what I'm doing in my 80X24 version as well.
However, I'm still looking for a more elegant solution.
I'm connecting to your thread, in case I find someone who knows the solution...
I'm using gotoxy(x,y) to read/write to the screen.
Michel.
Hello Michel_pc4,
1.
Under Linux, depending on the terminals you are using, the relevant escape sequences may or may not work.
Is supposed to maximize the window.
Is supposed to set the height to 48 characters and the width to 179.
However, this type of window manipulation sequences are rarely supported by virtual terminals, for example gnome-terminal on Debian does nothing (I believe it is also a gnome-terminal based terminal that is the default on Mint, but I am not sure). On the other hand, other CSI sequences may be supported, for example, those that allow changing the text colors:
but also those that allow moving the cursor to specific coordinates (maybe your gotoxy() function you are referring to uses them), clearing the screen, etc.
2.
Virtual terminals generally manage their own settings and allow users to change the window size through various methods. In the case of gnome-terminal on Debian, you can manage multiple profiles, each with specific preferences.
For example, gnome-terminal on Debian allows you to specify that you want to start it in maximized form and run a given program:
In a terminal, type
Dal
1.
Under Linux, depending on the terminals you are using, the relevant escape sequences may or may not work.
printf("\x1b[9;1;t"); Is supposed to maximize the window.
printf("\x1B[8;48;179t"); Is supposed to set the height to 48 characters and the width to 179.
However, this type of window manipulation sequences are rarely supported by virtual terminals, for example gnome-terminal on Debian does nothing (I believe it is also a gnome-terminal based terminal that is the default on Mint, but I am not sure). On the other hand, other CSI sequences may be supported, for example, those that allow changing the text colors:
printf("\x1b[104;93m"); printf("Colored text\n"); printf("\x1b[0m"); but also those that allow moving the cursor to specific coordinates (maybe your gotoxy() function you are referring to uses them), clearing the screen, etc.
2.
Virtual terminals generally manage their own settings and allow users to change the window size through various methods. In the case of gnome-terminal on Debian, you can manage multiple profiles, each with specific preferences.
For example, gnome-terminal on Debian allows you to specify that you want to start it in maximized form and run a given program:
gnome-terminal --quiet --maximize -- /usr/bin/links www.commentcamarche.net
In a terminal, type
gnome-terminal --helpor the name of your virtual terminal manager followed by
--helpto know its possible launch options.
Dal