1

I am experimenting with game development using the Windows Console and I've managed to resize the screen buffer and window to a desired size. The problem is that users can press Alt+Enter to force full screen mode - I want to avoid this, because:

  1. Entering full screen mode changes the screen buffer size from my carefully planned layout.

  2. Returning to windowed mode causes scroll bars to appear.

Ideally I'd like to disable the full screen mode change entirely, as a compromise I would want to detect the mode change so I can update buffer size variables/remove scroll bars when the change occurs. It was easy enough in Win32 via the WindowProc, but Console doesn't seem to have this.

Any suggestions?

phuclv
  • 37,963
  • 15
  • 156
  • 475
Wyrd Wolf
  • 21
  • 4
  • take [this answer](https://stackoverflow.com/questions/21508278/win32-console-disable-system-menu-buttons/21508714#21508714) and modify it to disable other window styles. Then subclass the HWND's wndproc if necessary to intercept keyboard events such as ALT+ENTER. – selbie Apr 02 '20 at 06:48
  • Also, this: https://learn.microsoft.com/en-us/windows/console/console-reference – selbie Apr 02 '20 at 06:51
  • @selbie The Console Reference link was handy, specifically the Console WinEvents link on that page. Funny how I hadn't come across that before. Thanks! – Wyrd Wolf Apr 03 '20 at 14:28

1 Answers1

1

While no amount of fiddling with window styles makes any difference (the style changes both going in and coming out of full screen mode), I was able to use the SetWinEventHook function to detect changes.

// Console Layout Change Detection

// Includes:
#include <Windows.h>
#include <iostream>

// Globals:
HWND g_hWindow;

// EventProc: User defined event procedure callback function
void CALLBACK EventProc(HWINEVENTHOOK hook, DWORD event, HWND wnd, LONG object, LONG child,
                                    DWORD thread, DWORD time) {
    std::cout << "Event received...\n";
    OutputDebugString(L"Event received...\n");

    // ToDo: Respond to layout change here...
}


// Main
int main() {
    // Grab the window handle
    g_hWindow = GetConsoleWindow();

    // Set a window event hook for the console layout changed events such
    // as resize, maximise/restore, enter/exit full screen mode, and others...
    HWINEVENTHOOK eventHook = SetWinEventHook(EVENT_CONSOLE_LAYOUT, EVENT_CONSOLE_LAYOUT,
                                            NULL, EventProc, 0, 0, WINEVENT_OUTOFCONTEXT);
    if (eventHook != 0) std::cout << "Hook started!\n";
    else std::cout << "Hook not started!\n";

    // Message loop. This one specifically listens for the console layout changed event.
    // If the window handle isn't specified, the hook will pick up ANY console window changes,
    // not just the one associated with this code.
    MSG msg;
    while (GetMessage(&msg, g_hWindow, EVENT_CONSOLE_LAYOUT, EVENT_CONSOLE_LAYOUT)) {
        DispatchMessage(&msg);

        /*...*/
    }
}
Wyrd Wolf
  • 21
  • 4