Change Windows console size












3















I have been trying to figure out how to resize the console window.
Here is the code of a function i am using:



#include <windows.h> 
#include <stdio.h>

#define WIDTH 70
#define HEIGHT 35

HANDLE wHnd;

void setup() {
SMALL_RECT windowSize = {0, 0, WIDTH - 1, HEIGHT - 1};
COORD bufferSize = { WIDTH , HEIGHT };
wHnd = GetStdHandle(STD_OUTPUT_HANDLE);

SetConsoleTitle("Conway's Game of Life");
SetConsoleWindowInfo(wHnd, 1, &windowSize);
SetConsoleScreenBufferSize(wHnd, bufferSize);
}


While it works for small widths and heights (like 70 and 35). it does not for the size I need (almost twice as big and yes i resized the buffersize accordingly, always a bit bigger than windowSize). Then it just is the regular size. My next thought was, since it already is quite big, why not go fullscreen.



SetConsoleDisplayMode(wHnd, CONSOLE_FULLSCREEN_MODE, 0);


Before this code snippet worked without a problem but now it does not, not even on other PCs. It stopped working on older projects too weirdly enough.



Any idea how I could launch it in fullscreen? (ALT + ENTER works) Or make the console window big at launch? I had a look at ncurses but I am on Windows 10 and don't know how to use it, besides that my Prof probably doesn't want me to use external libraries.
Thanks for your help! Let me know if I forgot something.










share|improve this question




















  • 1





    Microsoft ditched full screen support for console applications at some point trainsitioning to a new graphics driver model in between XP and Vista. (I'm too lazy too look it up.). If you want to resize the console window you have to make sure that the console output screenbuffer is at least the same size you want your window to resize to.

    – Swordfish
    Nov 9 '18 at 8:28











  • yea i read about that too, weird thing is that it worked a week ago and now it doesnt(fullscreen). well my console output screenbuffer is bigger than the size i want the window to be as you can see, yet it only works with small sizes but not for bigger ones

    – 2xWollert
    Nov 9 '18 at 8:35
















3















I have been trying to figure out how to resize the console window.
Here is the code of a function i am using:



#include <windows.h> 
#include <stdio.h>

#define WIDTH 70
#define HEIGHT 35

HANDLE wHnd;

void setup() {
SMALL_RECT windowSize = {0, 0, WIDTH - 1, HEIGHT - 1};
COORD bufferSize = { WIDTH , HEIGHT };
wHnd = GetStdHandle(STD_OUTPUT_HANDLE);

SetConsoleTitle("Conway's Game of Life");
SetConsoleWindowInfo(wHnd, 1, &windowSize);
SetConsoleScreenBufferSize(wHnd, bufferSize);
}


While it works for small widths and heights (like 70 and 35). it does not for the size I need (almost twice as big and yes i resized the buffersize accordingly, always a bit bigger than windowSize). Then it just is the regular size. My next thought was, since it already is quite big, why not go fullscreen.



SetConsoleDisplayMode(wHnd, CONSOLE_FULLSCREEN_MODE, 0);


Before this code snippet worked without a problem but now it does not, not even on other PCs. It stopped working on older projects too weirdly enough.



Any idea how I could launch it in fullscreen? (ALT + ENTER works) Or make the console window big at launch? I had a look at ncurses but I am on Windows 10 and don't know how to use it, besides that my Prof probably doesn't want me to use external libraries.
Thanks for your help! Let me know if I forgot something.










share|improve this question




















  • 1





    Microsoft ditched full screen support for console applications at some point trainsitioning to a new graphics driver model in between XP and Vista. (I'm too lazy too look it up.). If you want to resize the console window you have to make sure that the console output screenbuffer is at least the same size you want your window to resize to.

    – Swordfish
    Nov 9 '18 at 8:28











  • yea i read about that too, weird thing is that it worked a week ago and now it doesnt(fullscreen). well my console output screenbuffer is bigger than the size i want the window to be as you can see, yet it only works with small sizes but not for bigger ones

    – 2xWollert
    Nov 9 '18 at 8:35














3












3








3








I have been trying to figure out how to resize the console window.
Here is the code of a function i am using:



#include <windows.h> 
#include <stdio.h>

#define WIDTH 70
#define HEIGHT 35

HANDLE wHnd;

void setup() {
SMALL_RECT windowSize = {0, 0, WIDTH - 1, HEIGHT - 1};
COORD bufferSize = { WIDTH , HEIGHT };
wHnd = GetStdHandle(STD_OUTPUT_HANDLE);

SetConsoleTitle("Conway's Game of Life");
SetConsoleWindowInfo(wHnd, 1, &windowSize);
SetConsoleScreenBufferSize(wHnd, bufferSize);
}


While it works for small widths and heights (like 70 and 35). it does not for the size I need (almost twice as big and yes i resized the buffersize accordingly, always a bit bigger than windowSize). Then it just is the regular size. My next thought was, since it already is quite big, why not go fullscreen.



SetConsoleDisplayMode(wHnd, CONSOLE_FULLSCREEN_MODE, 0);


Before this code snippet worked without a problem but now it does not, not even on other PCs. It stopped working on older projects too weirdly enough.



Any idea how I could launch it in fullscreen? (ALT + ENTER works) Or make the console window big at launch? I had a look at ncurses but I am on Windows 10 and don't know how to use it, besides that my Prof probably doesn't want me to use external libraries.
Thanks for your help! Let me know if I forgot something.










share|improve this question
















I have been trying to figure out how to resize the console window.
Here is the code of a function i am using:



#include <windows.h> 
#include <stdio.h>

#define WIDTH 70
#define HEIGHT 35

HANDLE wHnd;

void setup() {
SMALL_RECT windowSize = {0, 0, WIDTH - 1, HEIGHT - 1};
COORD bufferSize = { WIDTH , HEIGHT };
wHnd = GetStdHandle(STD_OUTPUT_HANDLE);

SetConsoleTitle("Conway's Game of Life");
SetConsoleWindowInfo(wHnd, 1, &windowSize);
SetConsoleScreenBufferSize(wHnd, bufferSize);
}


While it works for small widths and heights (like 70 and 35). it does not for the size I need (almost twice as big and yes i resized the buffersize accordingly, always a bit bigger than windowSize). Then it just is the regular size. My next thought was, since it already is quite big, why not go fullscreen.



SetConsoleDisplayMode(wHnd, CONSOLE_FULLSCREEN_MODE, 0);


Before this code snippet worked without a problem but now it does not, not even on other PCs. It stopped working on older projects too weirdly enough.



Any idea how I could launch it in fullscreen? (ALT + ENTER works) Or make the console window big at launch? I had a look at ncurses but I am on Windows 10 and don't know how to use it, besides that my Prof probably doesn't want me to use external libraries.
Thanks for your help! Let me know if I forgot something.







c windows-console






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 '18 at 20:55









Swordfish

1




1










asked Nov 9 '18 at 8:01









2xWollert2xWollert

314




314








  • 1





    Microsoft ditched full screen support for console applications at some point trainsitioning to a new graphics driver model in between XP and Vista. (I'm too lazy too look it up.). If you want to resize the console window you have to make sure that the console output screenbuffer is at least the same size you want your window to resize to.

    – Swordfish
    Nov 9 '18 at 8:28











  • yea i read about that too, weird thing is that it worked a week ago and now it doesnt(fullscreen). well my console output screenbuffer is bigger than the size i want the window to be as you can see, yet it only works with small sizes but not for bigger ones

    – 2xWollert
    Nov 9 '18 at 8:35














  • 1





    Microsoft ditched full screen support for console applications at some point trainsitioning to a new graphics driver model in between XP and Vista. (I'm too lazy too look it up.). If you want to resize the console window you have to make sure that the console output screenbuffer is at least the same size you want your window to resize to.

    – Swordfish
    Nov 9 '18 at 8:28











  • yea i read about that too, weird thing is that it worked a week ago and now it doesnt(fullscreen). well my console output screenbuffer is bigger than the size i want the window to be as you can see, yet it only works with small sizes but not for bigger ones

    – 2xWollert
    Nov 9 '18 at 8:35








1




1





Microsoft ditched full screen support for console applications at some point trainsitioning to a new graphics driver model in between XP and Vista. (I'm too lazy too look it up.). If you want to resize the console window you have to make sure that the console output screenbuffer is at least the same size you want your window to resize to.

– Swordfish
Nov 9 '18 at 8:28





Microsoft ditched full screen support for console applications at some point trainsitioning to a new graphics driver model in between XP and Vista. (I'm too lazy too look it up.). If you want to resize the console window you have to make sure that the console output screenbuffer is at least the same size you want your window to resize to.

– Swordfish
Nov 9 '18 at 8:28













yea i read about that too, weird thing is that it worked a week ago and now it doesnt(fullscreen). well my console output screenbuffer is bigger than the size i want the window to be as you can see, yet it only works with small sizes but not for bigger ones

– 2xWollert
Nov 9 '18 at 8:35





yea i read about that too, weird thing is that it worked a week ago and now it doesnt(fullscreen). well my console output screenbuffer is bigger than the size i want the window to be as you can see, yet it only works with small sizes but not for bigger ones

– 2xWollert
Nov 9 '18 at 8:35












1 Answer
1






active

oldest

votes


















2














To maximize the console window you can do:



C++



#include <cstdlib>
#include <string>
#include <iostream>
#include <memory>
#include <type_traits>

#include <windows.h>

std::string get_last_error_msg()
{
auto error_code{ GetLastError() };
if (!error_code)
return {};

LPSTR buffer{};
auto size{ FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), reinterpret_cast<LPSTR>(&buffer), 0, nullptr) };

// use a unique_ptr for buffer since the ctor of string could throw
std::unique_ptr<std::remove_pointer_t<decltype(buffer)>, decltype(&LocalFree)> p{ buffer, LocalFree };
std::string message{ p.get(), size };
return message;
}

bool maximize_console()
{
auto console_window{ GetConsoleWindow() };

if (!console_window) {
std::cerr << "GetConsoleWindow() failed :(nn";
return false;
}

auto console_out{ GetStdHandle(STD_OUTPUT_HANDLE) };
if (console_out == INVALID_HANDLE_VALUE) {
std::cerr << "GetStdHandle() failed with "" << get_last_error_msg() << "" :(nn";
return false;
}

auto largest_size{ GetLargestConsoleWindowSize(console_out) };
if (!largest_size.X && !largest_size.Y) {
std::cerr << "GetLargestConsoleWindowSize() failed with "" << get_last_error_msg() << "" :(nn";
return false;
}

--largest_size.X;
--largest_size.Y;

if (!SetConsoleScreenBufferSize(console_out, largest_size)) {
std::cerr << "SetConsoleScreenBufferSize() failed with "" << get_last_error_msg() << "" :(nn";
return false;
}

if (!ShowWindow(console_window, SW_MAXIMIZE)) {
std::cerr << "ShowWindow() failed :(nn";
return false;
}

return true;
}

int main()
{
if (!maximize_console())
return EXIT_FAILURE;
}




C



#include <stdbool.h>
#include <stdio.h>

#include <windows.h>

LPSTR get_last_error_msg(void)
{
DWORD error_code = GetLastError();
if (!error_code)
return LocalAlloc(LMEM_ZEROINIT, 1);

LPSTR buffer = NULL;
FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&buffer, 0, NULL);

return buffer;
}

bool maximize_console(void)
{
HWND console_window = GetConsoleWindow();

if (!console_window) {
fputs("GetConsoleWindow() failed :(n", stderr);
return false;
}

HANDLE console_out = GetStdHandle(STD_OUTPUT_HANDLE);
if (console_out == INVALID_HANDLE_VALUE) {
LPSTR buffer = get_last_error_msg();
fprintf(stderr, "GetStdHandle() failed with "%s" :(nn", buffer);
LocalFree(buffer);
return false;
}

COORD largest_size = GetLargestConsoleWindowSize(console_out);
if (!largest_size.X && !largest_size.Y) {
LPSTR buffer = get_last_error_msg();
fprintf(stderr, "GetLargestConsoleWindowSize() failed with "%s" :(nn", buffer);
LocalFree(buffer);
return false;
}

--largest_size.X;
--largest_size.Y;

if (!SetConsoleScreenBufferSize(console_out, largest_size)) {
LPSTR buffer = get_last_error_msg();
fprintf(stderr, "SetConsoleScreenBufferSize() failed with "%s" :(nn", buffer);
LocalFree(buffer);
}

if (!ShowWindow(console_window, SW_MAXIMIZE)) {
fputs("ShowWindow() failed :(n", stderr);
return false;
}

return true;
}

int main(void)
{
if (!maximize_console())
return EXIT_FAILURE;
}





share|improve this answer


























  • Thank you very much! Sadly i have to use C and not C++.. i just notice that stackoverflow removed the "C - " from the title :( at least the tag C is still there. Nevertheless its helpful since C++ is what i want to learn but dont have in uni but i can probably use some or most of it, havent looked too close yet. Thank you very much! ^^

    – 2xWollert
    Nov 9 '18 at 15:56













  • @2xWollert Added C code.

    – Swordfish
    Nov 9 '18 at 19:50











  • @2xWollert accept the answer if you like it.

    – The Crow
    Nov 9 '18 at 22:46











  • This is calling GetLastError too late: "You should call the GetLastError function immediately when a function's return value indicates that such a call will return useful data." Don't assume, that the C++ I/O stream library wouldn't have any observable side effects with respect to the calling thread's last error code. The simplest (and safe) solution: Pass the error code into get_last_error_msg (and rename the function).

    – IInspectable
    Nov 26 '18 at 8:39













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53221855%2fchange-windows-console-size%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














To maximize the console window you can do:



C++



#include <cstdlib>
#include <string>
#include <iostream>
#include <memory>
#include <type_traits>

#include <windows.h>

std::string get_last_error_msg()
{
auto error_code{ GetLastError() };
if (!error_code)
return {};

LPSTR buffer{};
auto size{ FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), reinterpret_cast<LPSTR>(&buffer), 0, nullptr) };

// use a unique_ptr for buffer since the ctor of string could throw
std::unique_ptr<std::remove_pointer_t<decltype(buffer)>, decltype(&LocalFree)> p{ buffer, LocalFree };
std::string message{ p.get(), size };
return message;
}

bool maximize_console()
{
auto console_window{ GetConsoleWindow() };

if (!console_window) {
std::cerr << "GetConsoleWindow() failed :(nn";
return false;
}

auto console_out{ GetStdHandle(STD_OUTPUT_HANDLE) };
if (console_out == INVALID_HANDLE_VALUE) {
std::cerr << "GetStdHandle() failed with "" << get_last_error_msg() << "" :(nn";
return false;
}

auto largest_size{ GetLargestConsoleWindowSize(console_out) };
if (!largest_size.X && !largest_size.Y) {
std::cerr << "GetLargestConsoleWindowSize() failed with "" << get_last_error_msg() << "" :(nn";
return false;
}

--largest_size.X;
--largest_size.Y;

if (!SetConsoleScreenBufferSize(console_out, largest_size)) {
std::cerr << "SetConsoleScreenBufferSize() failed with "" << get_last_error_msg() << "" :(nn";
return false;
}

if (!ShowWindow(console_window, SW_MAXIMIZE)) {
std::cerr << "ShowWindow() failed :(nn";
return false;
}

return true;
}

int main()
{
if (!maximize_console())
return EXIT_FAILURE;
}




C



#include <stdbool.h>
#include <stdio.h>

#include <windows.h>

LPSTR get_last_error_msg(void)
{
DWORD error_code = GetLastError();
if (!error_code)
return LocalAlloc(LMEM_ZEROINIT, 1);

LPSTR buffer = NULL;
FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&buffer, 0, NULL);

return buffer;
}

bool maximize_console(void)
{
HWND console_window = GetConsoleWindow();

if (!console_window) {
fputs("GetConsoleWindow() failed :(n", stderr);
return false;
}

HANDLE console_out = GetStdHandle(STD_OUTPUT_HANDLE);
if (console_out == INVALID_HANDLE_VALUE) {
LPSTR buffer = get_last_error_msg();
fprintf(stderr, "GetStdHandle() failed with "%s" :(nn", buffer);
LocalFree(buffer);
return false;
}

COORD largest_size = GetLargestConsoleWindowSize(console_out);
if (!largest_size.X && !largest_size.Y) {
LPSTR buffer = get_last_error_msg();
fprintf(stderr, "GetLargestConsoleWindowSize() failed with "%s" :(nn", buffer);
LocalFree(buffer);
return false;
}

--largest_size.X;
--largest_size.Y;

if (!SetConsoleScreenBufferSize(console_out, largest_size)) {
LPSTR buffer = get_last_error_msg();
fprintf(stderr, "SetConsoleScreenBufferSize() failed with "%s" :(nn", buffer);
LocalFree(buffer);
}

if (!ShowWindow(console_window, SW_MAXIMIZE)) {
fputs("ShowWindow() failed :(n", stderr);
return false;
}

return true;
}

int main(void)
{
if (!maximize_console())
return EXIT_FAILURE;
}





share|improve this answer


























  • Thank you very much! Sadly i have to use C and not C++.. i just notice that stackoverflow removed the "C - " from the title :( at least the tag C is still there. Nevertheless its helpful since C++ is what i want to learn but dont have in uni but i can probably use some or most of it, havent looked too close yet. Thank you very much! ^^

    – 2xWollert
    Nov 9 '18 at 15:56













  • @2xWollert Added C code.

    – Swordfish
    Nov 9 '18 at 19:50











  • @2xWollert accept the answer if you like it.

    – The Crow
    Nov 9 '18 at 22:46











  • This is calling GetLastError too late: "You should call the GetLastError function immediately when a function's return value indicates that such a call will return useful data." Don't assume, that the C++ I/O stream library wouldn't have any observable side effects with respect to the calling thread's last error code. The simplest (and safe) solution: Pass the error code into get_last_error_msg (and rename the function).

    – IInspectable
    Nov 26 '18 at 8:39


















2














To maximize the console window you can do:



C++



#include <cstdlib>
#include <string>
#include <iostream>
#include <memory>
#include <type_traits>

#include <windows.h>

std::string get_last_error_msg()
{
auto error_code{ GetLastError() };
if (!error_code)
return {};

LPSTR buffer{};
auto size{ FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), reinterpret_cast<LPSTR>(&buffer), 0, nullptr) };

// use a unique_ptr for buffer since the ctor of string could throw
std::unique_ptr<std::remove_pointer_t<decltype(buffer)>, decltype(&LocalFree)> p{ buffer, LocalFree };
std::string message{ p.get(), size };
return message;
}

bool maximize_console()
{
auto console_window{ GetConsoleWindow() };

if (!console_window) {
std::cerr << "GetConsoleWindow() failed :(nn";
return false;
}

auto console_out{ GetStdHandle(STD_OUTPUT_HANDLE) };
if (console_out == INVALID_HANDLE_VALUE) {
std::cerr << "GetStdHandle() failed with "" << get_last_error_msg() << "" :(nn";
return false;
}

auto largest_size{ GetLargestConsoleWindowSize(console_out) };
if (!largest_size.X && !largest_size.Y) {
std::cerr << "GetLargestConsoleWindowSize() failed with "" << get_last_error_msg() << "" :(nn";
return false;
}

--largest_size.X;
--largest_size.Y;

if (!SetConsoleScreenBufferSize(console_out, largest_size)) {
std::cerr << "SetConsoleScreenBufferSize() failed with "" << get_last_error_msg() << "" :(nn";
return false;
}

if (!ShowWindow(console_window, SW_MAXIMIZE)) {
std::cerr << "ShowWindow() failed :(nn";
return false;
}

return true;
}

int main()
{
if (!maximize_console())
return EXIT_FAILURE;
}




C



#include <stdbool.h>
#include <stdio.h>

#include <windows.h>

LPSTR get_last_error_msg(void)
{
DWORD error_code = GetLastError();
if (!error_code)
return LocalAlloc(LMEM_ZEROINIT, 1);

LPSTR buffer = NULL;
FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&buffer, 0, NULL);

return buffer;
}

bool maximize_console(void)
{
HWND console_window = GetConsoleWindow();

if (!console_window) {
fputs("GetConsoleWindow() failed :(n", stderr);
return false;
}

HANDLE console_out = GetStdHandle(STD_OUTPUT_HANDLE);
if (console_out == INVALID_HANDLE_VALUE) {
LPSTR buffer = get_last_error_msg();
fprintf(stderr, "GetStdHandle() failed with "%s" :(nn", buffer);
LocalFree(buffer);
return false;
}

COORD largest_size = GetLargestConsoleWindowSize(console_out);
if (!largest_size.X && !largest_size.Y) {
LPSTR buffer = get_last_error_msg();
fprintf(stderr, "GetLargestConsoleWindowSize() failed with "%s" :(nn", buffer);
LocalFree(buffer);
return false;
}

--largest_size.X;
--largest_size.Y;

if (!SetConsoleScreenBufferSize(console_out, largest_size)) {
LPSTR buffer = get_last_error_msg();
fprintf(stderr, "SetConsoleScreenBufferSize() failed with "%s" :(nn", buffer);
LocalFree(buffer);
}

if (!ShowWindow(console_window, SW_MAXIMIZE)) {
fputs("ShowWindow() failed :(n", stderr);
return false;
}

return true;
}

int main(void)
{
if (!maximize_console())
return EXIT_FAILURE;
}





share|improve this answer


























  • Thank you very much! Sadly i have to use C and not C++.. i just notice that stackoverflow removed the "C - " from the title :( at least the tag C is still there. Nevertheless its helpful since C++ is what i want to learn but dont have in uni but i can probably use some or most of it, havent looked too close yet. Thank you very much! ^^

    – 2xWollert
    Nov 9 '18 at 15:56













  • @2xWollert Added C code.

    – Swordfish
    Nov 9 '18 at 19:50











  • @2xWollert accept the answer if you like it.

    – The Crow
    Nov 9 '18 at 22:46











  • This is calling GetLastError too late: "You should call the GetLastError function immediately when a function's return value indicates that such a call will return useful data." Don't assume, that the C++ I/O stream library wouldn't have any observable side effects with respect to the calling thread's last error code. The simplest (and safe) solution: Pass the error code into get_last_error_msg (and rename the function).

    – IInspectable
    Nov 26 '18 at 8:39
















2












2








2







To maximize the console window you can do:



C++



#include <cstdlib>
#include <string>
#include <iostream>
#include <memory>
#include <type_traits>

#include <windows.h>

std::string get_last_error_msg()
{
auto error_code{ GetLastError() };
if (!error_code)
return {};

LPSTR buffer{};
auto size{ FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), reinterpret_cast<LPSTR>(&buffer), 0, nullptr) };

// use a unique_ptr for buffer since the ctor of string could throw
std::unique_ptr<std::remove_pointer_t<decltype(buffer)>, decltype(&LocalFree)> p{ buffer, LocalFree };
std::string message{ p.get(), size };
return message;
}

bool maximize_console()
{
auto console_window{ GetConsoleWindow() };

if (!console_window) {
std::cerr << "GetConsoleWindow() failed :(nn";
return false;
}

auto console_out{ GetStdHandle(STD_OUTPUT_HANDLE) };
if (console_out == INVALID_HANDLE_VALUE) {
std::cerr << "GetStdHandle() failed with "" << get_last_error_msg() << "" :(nn";
return false;
}

auto largest_size{ GetLargestConsoleWindowSize(console_out) };
if (!largest_size.X && !largest_size.Y) {
std::cerr << "GetLargestConsoleWindowSize() failed with "" << get_last_error_msg() << "" :(nn";
return false;
}

--largest_size.X;
--largest_size.Y;

if (!SetConsoleScreenBufferSize(console_out, largest_size)) {
std::cerr << "SetConsoleScreenBufferSize() failed with "" << get_last_error_msg() << "" :(nn";
return false;
}

if (!ShowWindow(console_window, SW_MAXIMIZE)) {
std::cerr << "ShowWindow() failed :(nn";
return false;
}

return true;
}

int main()
{
if (!maximize_console())
return EXIT_FAILURE;
}




C



#include <stdbool.h>
#include <stdio.h>

#include <windows.h>

LPSTR get_last_error_msg(void)
{
DWORD error_code = GetLastError();
if (!error_code)
return LocalAlloc(LMEM_ZEROINIT, 1);

LPSTR buffer = NULL;
FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&buffer, 0, NULL);

return buffer;
}

bool maximize_console(void)
{
HWND console_window = GetConsoleWindow();

if (!console_window) {
fputs("GetConsoleWindow() failed :(n", stderr);
return false;
}

HANDLE console_out = GetStdHandle(STD_OUTPUT_HANDLE);
if (console_out == INVALID_HANDLE_VALUE) {
LPSTR buffer = get_last_error_msg();
fprintf(stderr, "GetStdHandle() failed with "%s" :(nn", buffer);
LocalFree(buffer);
return false;
}

COORD largest_size = GetLargestConsoleWindowSize(console_out);
if (!largest_size.X && !largest_size.Y) {
LPSTR buffer = get_last_error_msg();
fprintf(stderr, "GetLargestConsoleWindowSize() failed with "%s" :(nn", buffer);
LocalFree(buffer);
return false;
}

--largest_size.X;
--largest_size.Y;

if (!SetConsoleScreenBufferSize(console_out, largest_size)) {
LPSTR buffer = get_last_error_msg();
fprintf(stderr, "SetConsoleScreenBufferSize() failed with "%s" :(nn", buffer);
LocalFree(buffer);
}

if (!ShowWindow(console_window, SW_MAXIMIZE)) {
fputs("ShowWindow() failed :(n", stderr);
return false;
}

return true;
}

int main(void)
{
if (!maximize_console())
return EXIT_FAILURE;
}





share|improve this answer















To maximize the console window you can do:



C++



#include <cstdlib>
#include <string>
#include <iostream>
#include <memory>
#include <type_traits>

#include <windows.h>

std::string get_last_error_msg()
{
auto error_code{ GetLastError() };
if (!error_code)
return {};

LPSTR buffer{};
auto size{ FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), reinterpret_cast<LPSTR>(&buffer), 0, nullptr) };

// use a unique_ptr for buffer since the ctor of string could throw
std::unique_ptr<std::remove_pointer_t<decltype(buffer)>, decltype(&LocalFree)> p{ buffer, LocalFree };
std::string message{ p.get(), size };
return message;
}

bool maximize_console()
{
auto console_window{ GetConsoleWindow() };

if (!console_window) {
std::cerr << "GetConsoleWindow() failed :(nn";
return false;
}

auto console_out{ GetStdHandle(STD_OUTPUT_HANDLE) };
if (console_out == INVALID_HANDLE_VALUE) {
std::cerr << "GetStdHandle() failed with "" << get_last_error_msg() << "" :(nn";
return false;
}

auto largest_size{ GetLargestConsoleWindowSize(console_out) };
if (!largest_size.X && !largest_size.Y) {
std::cerr << "GetLargestConsoleWindowSize() failed with "" << get_last_error_msg() << "" :(nn";
return false;
}

--largest_size.X;
--largest_size.Y;

if (!SetConsoleScreenBufferSize(console_out, largest_size)) {
std::cerr << "SetConsoleScreenBufferSize() failed with "" << get_last_error_msg() << "" :(nn";
return false;
}

if (!ShowWindow(console_window, SW_MAXIMIZE)) {
std::cerr << "ShowWindow() failed :(nn";
return false;
}

return true;
}

int main()
{
if (!maximize_console())
return EXIT_FAILURE;
}




C



#include <stdbool.h>
#include <stdio.h>

#include <windows.h>

LPSTR get_last_error_msg(void)
{
DWORD error_code = GetLastError();
if (!error_code)
return LocalAlloc(LMEM_ZEROINIT, 1);

LPSTR buffer = NULL;
FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&buffer, 0, NULL);

return buffer;
}

bool maximize_console(void)
{
HWND console_window = GetConsoleWindow();

if (!console_window) {
fputs("GetConsoleWindow() failed :(n", stderr);
return false;
}

HANDLE console_out = GetStdHandle(STD_OUTPUT_HANDLE);
if (console_out == INVALID_HANDLE_VALUE) {
LPSTR buffer = get_last_error_msg();
fprintf(stderr, "GetStdHandle() failed with "%s" :(nn", buffer);
LocalFree(buffer);
return false;
}

COORD largest_size = GetLargestConsoleWindowSize(console_out);
if (!largest_size.X && !largest_size.Y) {
LPSTR buffer = get_last_error_msg();
fprintf(stderr, "GetLargestConsoleWindowSize() failed with "%s" :(nn", buffer);
LocalFree(buffer);
return false;
}

--largest_size.X;
--largest_size.Y;

if (!SetConsoleScreenBufferSize(console_out, largest_size)) {
LPSTR buffer = get_last_error_msg();
fprintf(stderr, "SetConsoleScreenBufferSize() failed with "%s" :(nn", buffer);
LocalFree(buffer);
}

if (!ShowWindow(console_window, SW_MAXIMIZE)) {
fputs("ShowWindow() failed :(n", stderr);
return false;
}

return true;
}

int main(void)
{
if (!maximize_console())
return EXIT_FAILURE;
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 9 '18 at 21:02

























answered Nov 9 '18 at 10:19









SwordfishSwordfish

1




1













  • Thank you very much! Sadly i have to use C and not C++.. i just notice that stackoverflow removed the "C - " from the title :( at least the tag C is still there. Nevertheless its helpful since C++ is what i want to learn but dont have in uni but i can probably use some or most of it, havent looked too close yet. Thank you very much! ^^

    – 2xWollert
    Nov 9 '18 at 15:56













  • @2xWollert Added C code.

    – Swordfish
    Nov 9 '18 at 19:50











  • @2xWollert accept the answer if you like it.

    – The Crow
    Nov 9 '18 at 22:46











  • This is calling GetLastError too late: "You should call the GetLastError function immediately when a function's return value indicates that such a call will return useful data." Don't assume, that the C++ I/O stream library wouldn't have any observable side effects with respect to the calling thread's last error code. The simplest (and safe) solution: Pass the error code into get_last_error_msg (and rename the function).

    – IInspectable
    Nov 26 '18 at 8:39





















  • Thank you very much! Sadly i have to use C and not C++.. i just notice that stackoverflow removed the "C - " from the title :( at least the tag C is still there. Nevertheless its helpful since C++ is what i want to learn but dont have in uni but i can probably use some or most of it, havent looked too close yet. Thank you very much! ^^

    – 2xWollert
    Nov 9 '18 at 15:56













  • @2xWollert Added C code.

    – Swordfish
    Nov 9 '18 at 19:50











  • @2xWollert accept the answer if you like it.

    – The Crow
    Nov 9 '18 at 22:46











  • This is calling GetLastError too late: "You should call the GetLastError function immediately when a function's return value indicates that such a call will return useful data." Don't assume, that the C++ I/O stream library wouldn't have any observable side effects with respect to the calling thread's last error code. The simplest (and safe) solution: Pass the error code into get_last_error_msg (and rename the function).

    – IInspectable
    Nov 26 '18 at 8:39



















Thank you very much! Sadly i have to use C and not C++.. i just notice that stackoverflow removed the "C - " from the title :( at least the tag C is still there. Nevertheless its helpful since C++ is what i want to learn but dont have in uni but i can probably use some or most of it, havent looked too close yet. Thank you very much! ^^

– 2xWollert
Nov 9 '18 at 15:56







Thank you very much! Sadly i have to use C and not C++.. i just notice that stackoverflow removed the "C - " from the title :( at least the tag C is still there. Nevertheless its helpful since C++ is what i want to learn but dont have in uni but i can probably use some or most of it, havent looked too close yet. Thank you very much! ^^

– 2xWollert
Nov 9 '18 at 15:56















@2xWollert Added C code.

– Swordfish
Nov 9 '18 at 19:50





@2xWollert Added C code.

– Swordfish
Nov 9 '18 at 19:50













@2xWollert accept the answer if you like it.

– The Crow
Nov 9 '18 at 22:46





@2xWollert accept the answer if you like it.

– The Crow
Nov 9 '18 at 22:46













This is calling GetLastError too late: "You should call the GetLastError function immediately when a function's return value indicates that such a call will return useful data." Don't assume, that the C++ I/O stream library wouldn't have any observable side effects with respect to the calling thread's last error code. The simplest (and safe) solution: Pass the error code into get_last_error_msg (and rename the function).

– IInspectable
Nov 26 '18 at 8:39







This is calling GetLastError too late: "You should call the GetLastError function immediately when a function's return value indicates that such a call will return useful data." Don't assume, that the C++ I/O stream library wouldn't have any observable side effects with respect to the calling thread's last error code. The simplest (and safe) solution: Pass the error code into get_last_error_msg (and rename the function).

– IInspectable
Nov 26 '18 at 8:39






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53221855%2fchange-windows-console-size%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin