1

I have programmed a game running on console. The console host I chose to use is Window Console Host because I can easily and successfully setup and confiugre it with the API functions from <Windows.h>. The game's window I expected was like below: enter image description here

I had made the window of above console app not able to be resized by anyway. But when I released that app and sent to my friend to test, then it became like this because he was using Windows Terminal:

enter image description here

Is there any solution to make the released console app run with fixed console host on any computer to keep my setup?

Here is the code snippet I wanted to setup the console's window:

#include <Windows.h>
int main(void) {
    HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
    HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
    HWND hWnd = GetConsoleWindow();
    
    //set font size
    CONSOLE_FONT_INFOEX cfi = { sizeof(cfi) };
    // Populate cfi with the screen buffer's current font info
    GetCurrentConsoleFontEx(hStdout, FALSE, &cfi);

    // Modify the font size in cfi
    //cfi.dwFontSize.X *= 2;
    cfi.dwFontSize.Y = 36;

    // Use cfi to set the screen buffer's new font
    SetCurrentConsoleFontEx(hStdout, FALSE, &cfi);

    //delete cursor
    const CONSOLE_CURSOR_INFO CUR_INFO = { 10, 0 };
    SetConsoleCursorInfo(hStdout, &CUR_INFO);

    //set size of console
    SMALL_RECT WindowSize = { 0, 0, CONSOLE_WIDTH, CONSOLE_HEIGHT};
    SetConsoleWindowInfo(hStdout, 1, &WindowSize);

    //remove console screen buffer. This command must be put after 'SetConsoleWindowInfo',...
    //...and value of 'NewSize' must be greater than CONSOLE_WIDTH and CONSOLE_HEIGHT just 1
    COORD NewSize = { CONSOLE_WIDTH + 1, CONSOLE_HEIGHT + 1 };
    SetConsoleScreenBufferSize(hStdout, NewSize);

    //disable resize console by mouse
    SetWindowLong(hWnd, GWL_STYLE, GetWindowLong(hWnd, GWL_STYLE) & ~WS_SIZEBOX);

    //disable maximizing
    DeleteMenu(GetSystemMenu(hWnd, FALSE), SC_MAXIMIZE, MF_BYCOMMAND);

    //Hide scroll bar
    ShowScrollBar(hWnd, SB_BOTH, FALSE);

    //disable select text by mouse
    SetConsoleMode(hStdin, ~ENABLE_QUICK_EDIT_MODE);

    //set title of console window
    SetConsoleTitleW(TEXT("Console Run"));
}
Cong Hau
  • 29
  • 3
  • According to MSDN, `SetConsoleWindowInfo` _fails if the specified window rectangle extends beyond the boundaries of the console screen buffer._ So maybe try calling `SetConsoleScreenBufferSize` before `SetConsoleWindowInfo`. – 001 May 28 '23 at 16:21
  • Side note: checking and logging the return values from all those Windows API calls would probably help you diagnose the issue. [`GetLastError()`](https://learn.microsoft.com/en-us/windows/win32/api/errhandlingapi/nf-errhandlingapi-getlasterror) is your friend. – 001 May 28 '23 at 16:23

1 Answers1

0

What you're seeing is microsoft/terminal#5094. The Windows Terminal currently doesn't support a commandline application resizing the dimensions of the viewport.

You could try making a helper exe or .bat/.cmd file that runs

conhost.exe -- yourGame.exe

and that will manually launch yourGame.exe in the Windows Console Host, regardless of their Default Terminal setting.

zadjii
  • 529
  • 2
  • 4