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.

Swordfish
  • 12,971
  • 3
  • 21
  • 43
2xWollert
  • 61
  • 6
  • 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 09 '18 at 08: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 09 '18 at 08:35

1 Answers1

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 :(\n\n";
        return false;
    }

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

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

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

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

    if (!ShowWindow(console_window, SW_MAXIMIZE)) {
        std::cerr << "ShowWindow() failed :(\n\n";
        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\" :(\n\n", 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\" :(\n\n", 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\" :(\n\n", 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;
}
Swordfish
  • 12,971
  • 3
  • 21
  • 43
  • 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 09 '18 at 15:56
  • This is calling [GetLastError](https://msdn.microsoft.com/en-us/library/windows/desktop/ms679360.aspx) 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 08:39