如何检测控制台中当前是否启用了Windows 10缓冲区包装模式 [英] How to detect whether Windows 10 buffer wrapping mode is currently enabled in the console

查看:99
本文介绍了如何检测控制台中当前是否启用了Windows 10缓冲区包装模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何方法可以检测控制台应用程序是否在启用Windows 10的新功能的情况下运行?

Is there any way to detect whether a console app is running with Windows 10's new features enabled?

此MSDN页面显示 HKEY_CURRENT_USER\Console\ForceV2 HKEY_CURRENT_USER\Console\LineWrap HKEY_CURRENT_USER\Console\ {name} \LineWrap 控制它,但除此之外解析起来不够健壮,可能不正确。如果用户切换到旧版模式或从旧版模式切换到新模式,则更改将在控制台重新启动后生效。

This MSDN page shows that HKEY_CURRENT_USER\Console\ForceV2, HKEY_CURRENT_USER\Console\LineWrap and HKEY_CURRENT_USER\Console\{name}\LineWrap control it, but besides that being less robust to parse, it may not be correct. If the user switches to or from legacy mode, the change won't take effect until the console relaunches.

如果我开发了该应用程序,则可以在启动时进行检查。但是,可能存在争用情况,这使注册表检查对于任何实际使用都毫无用处。我很好奇第三方控制台窗口的解决方案。

If I develop the app, I can do the check at startup. There could have been a race condition though, which renders the registry check useless for any practical use. I am curious what the solution would be for third party console windows.

推荐答案

似乎没有用于此的API,尽管我可能希望在以后的SDK中出现(也许是GetConsoleMode中的其他超扩展标志)。

There seems to be no API for that, though I'd expect one to surface in some later SDK (maybe additional hyper-extended flags in GetConsoleMode).

同时,以下内容是一种快速的尝试,试图检测调整大小

Meanwhile, the following is a quick hack which attempts to detect the resize-wider capability of the new console, based on checking the ptMaxTrackSize.X value returned by GetMinMaxInfo.

旧版控制台不允许将窗口的大小调整为大于屏幕缓冲区的大小,这是新控制台的更广泛的功能,其基础是检查GetMinMaxInfo返回的ptMaxTrackSize.X值。宽度,而新的宽度。假设(a)控制台以全缓冲区宽度运行,即没有水平滚动条,并且(b)尚未拉伸到全屏/最大屏幕宽度,那么检查窗口是否允许调整自身大小非常简单宽(新控制台)或不宽(旧控制台)。应该注意的是,从技术上可以通过手动将缓冲区宽度从字符转换为像素而不是依靠GetWindowRect来避免假设(a),但是假设(b)几乎是不可避免的。

The legacy console doesn't allow resizing the window wider than the screen buffer width, while the new one does. On the assumptions that (a) the console is running at full buffer width i.e. has no horizontal scrollbar, and (b) it's not already stretched to the full/max screen width, it's fairly straightforward to check whether the window allows itself to be resized wider (new console) or not (legacy console). Should be noted that assumption (a) could technically be avoided by manually converting the buffer width from characters to pixels, rather than relying on GetWindowRect, but assumption (b) is pretty much unavoidable.

这是代码(免责声明:快速可靠的概念证明,没有错误检查等)。

int main()
{
    // largest possible console size for given font and desktop
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD cd = GetLargestConsoleWindowSize(hOut);
    SHORT nScrMaxXch = cd.X, 
        nScrMaxYch = cd.Y;

    // current and max console sizes for given screen buffer
    CONSOLE_SCREEN_BUFFER_INFOEX csbix = { sizeof(csbix) };
    GetConsoleScreenBufferInfoEx(hOut, &csbix);
    SHORT nWndXch = csbix.srWindow.Right - csbix.srWindow.Left + 1,
        nWndYch = csbix.srWindow.Bottom - csbix.srWindow.Top + 1;
    SHORT nWndMaxXch = csbix.dwMaximumWindowSize.X,
        nWndMaxYch = csbix.dwMaximumWindowSize.Y;

    wprintf(L"chars:   wnd-size %d %d, max-wnd-size %d %d, largest-size %d %d\n",
        nWndXch, nWndYch, nWndMaxXch, nWndMaxYch, nScrMaxXch, nScrMaxYch);

    // current window size
    HWND hWnd = GetConsoleWindow();
    RECT rc; GetWindowRect(hWnd, &rc);
    LONG nWndXpx = rc.right - rc.left,
        nWndYpx = rc.bottom - rc.top;

    // max window tracking size
    MINMAXINFO mmi = { 0 };
    SendMessage(hWnd, WM_GETMINMAXINFO, 0, (LPARAM)&mmi);
    LONG nWndMaxXpx = mmi.ptMaxTrackSize.x,
        nWndMaxYpx = mmi.ptMaxTrackSize.y;

    wprintf(L"pixels:  wnd-size %lu %lu, max-tracking-size %lu %lu\n",
        nWndXpx, nWndYpx, nWndMaxXpx, nWndMaxYpx);

    if (nWndXch == nWndMaxXch   // full buffer width, no h-scrollbar 
    && nWndXch < nScrMaxXch     // not already stretched to full screen width
    && nWndMaxXpx > nWndXpx)    // allowed to resized wider
        wprintf(L"\n...most likely a Win10 console with ForceV2 enabled\n");

    return 0;
}

这是在旧版控制台中运行时的输出。

This is the output when run in a legacy console.

chars:   wnd-size 80 25, max-wnd-size 80 71, largest-size 240 71
pixels:  wnd-size 677 443, max-tracking-size 677 1179

这是在新控制台中运行时的输出。 / p>

This is the output when run in the new console.

chars:   wnd-size 80 25, max-wnd-size 80 71, largest-size 239 71
pixels:  wnd-size 677 443, max-tracking-size 1936 1186

...most likely a Win10 console with ForceV2 enabled

这篇关于如何检测控制台中当前是否启用了Windows 10缓冲区包装模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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