【问题标题】:How do I disable or detect mode changes in Windows Console?如何在 Windows 控制台中禁用或检测模式更改?
【发布时间】:2020-07-13 23:15:50
【问题描述】:

我正在尝试使用 Windows 控制台进行游戏开发,并且我已设法将屏幕缓冲区和窗口的大小调整为所需的大小。问题是用户可以按 Alt+Enter 强制全屏模式 - 我想避免这种情况,因为:

  1. 进入全屏模式会改变我精心设计的布局的屏幕缓冲区大小。

  2. 返回窗口模式会导致出现滚动条。

理想情况下,我想完全禁用全屏模式更改,作为一种折衷方案,我想检测模式更改,以便更新缓冲区大小变量/发生更改时删除滚动条。在 Win32 中通过WindowProc 很容易,但控制台似乎没有这个。

有什么建议吗?

【问题讨论】:

  • this answer 并修改它以禁用其他窗口样式。然后在必要时对 HWND 的 wndproc 进行子类化,以拦截 ALT+ENTER 等键盘事件。
  • @selbie 控制台参考链接很方便,特别是该页面上的控制台 WinEvents 链接。有趣的是我以前没有遇到过。谢谢!

标签: c++ windows terminal console fullscreen


【解决方案1】:

虽然对窗口样式的修改没有任何影响(样式在进入和退出全屏模式时都会发生变化),但我能够使用 SetWinEventHook 函数来检测变化。

// 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);

        /*...*/
    }
}

【讨论】:

    猜你喜欢
    • 2012-03-07
    • 2012-04-30
    • 1970-01-01
    • 2019-05-14
    • 1970-01-01
    • 2011-04-05
    • 2018-03-15
    • 1970-01-01
    相关资源
    最近更新 更多