如何在Windows控制台中禁用或检测模式更改? [英] How do I disable or detect mode changes in Windows Console?

查看:89
本文介绍了如何在Windows控制台中禁用或检测模式更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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. 进入全屏模式会更改我精心计划的布局中的屏幕缓冲区大小.

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

返回窗口模式将导致出现滚动条.

Returning to windowed mode causes scroll bars to appear.

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

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.

有什么建议吗?

推荐答案

尽管对窗口样式的摆弄没有任何区别(无论样式进入和退出全屏模式),我都可以使用 SetWinEventHook 函数来检测更改.

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

        /*...*/
    }
}

这篇关于如何在Windows控制台中禁用或检测模式更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆