我正在尝试使用Windows控制台(C ++)进行游戏开发,并且设法将屏幕缓冲区和窗口的大小调整为所需的大小。问题是用户可以按alt + enter强制进入全屏模式-我想避免这种情况,因为:
1:进入全屏模式会更改我精心计划的布局中的屏幕缓冲区大小。
2:返回窗口模式将导致出现滚动条。
理想情况下,我想完全禁用全屏模式更改,作为一种折衷,我想检测模式更改,以便在更改发生时可以更新缓冲区大小变量/删除滚动条。通过WindowProc在Win32中足够简单,但是Console似乎没有。
有什么建议吗?
虽然对窗口样式的摆弄没有任何区别(样式更改同时进入和退出全屏模式,但我仍然可以使用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);
/*...*/
}
}