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.
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.