将缓冲区大小与窗口大小相匹配

问题描述 投票:0回答:1

此代码使控制台窗口无边框且全尺寸:

public static Coord InitializeConsole(nint consoleHandle, nint stdOutput, in int fontWidth, in int fontHeight, ColorPalette colorPalette)
{
    var consoleRect = new RECT();
    GetWindowRect(consoleHandle, ref consoleRect);

    var desktopWidth = GetSystemMetrics(0);
    var desktopHeight = GetSystemMetrics(1);

    _ = SetWindowLong(consoleHandle, -16, 0x00080000);

    var csbe = new CONSOLE_SCREEN_BUFFER_INFO_EX();
    csbe.cbSize = Marshal.SizeOf(csbe);

    GetConsoleScreenBufferInfoEx(stdOutput, ref csbe);

    SetColorPalette(stdOutput, colorPalette, ref csbe);

    csbe.dwSize.X = (short)(desktopWidth / fontWidth);
    csbe.dwSize.Y = (short)(desktopHeight / fontHeight);
    ++csbe.srWindow.Bottom;
    ++csbe.srWindow.Right;

    SetConsoleScreenBufferInfoEx(stdOutput, ref csbe);

    SetWindowPos(
        consoleHandle,
        nint.Zero,
        0, 0,
        windowWidth,
        windowHeight,
        0x0040);
    
    DrawMenuBar(consoleHandle);

    GetConsoleScreenBufferInfoEx(stdOutput, ref csbe);

    return new(csbe.dwSize.X, csbe.dwSize.Y);
}

我的问题是,在某些字体高度上,控制台缓冲区对于控制台窗口来说太大,并且出现滚动条。

如何设置滚动条不出现的Buffer大小?

c# windows console
1个回答
0
投票
public static Coord InitializeConsole(nint consoleHandle, nint stdOutput, in int fontWidth, in int fontHeight, ColorPalette colorPalette)
{
    var consoleRect = new RECT();
    GetWindowRect(consoleHandle, ref consoleRect);

    var desktopWidth = GetSystemMetrics(0);
    var desktopHeight = GetSystemMetrics(1);

    _ = SetWindowLong(consoleHandle, -16, 0x00080000);

    var csbe = new CONSOLE_SCREEN_BUFFER_INFO_EX();
    csbe.cbSize = Marshal.SizeOf(csbe);

    GetConsoleScreenBufferInfoEx(stdOutput, ref csbe);

    SetColorPalette(stdOutput, colorPalette, ref csbe);

    csbe.dwSize.X = 1;
    csbe.dwSize.Y = 1;
    ++csbe.srWindow.Bottom;
    ++csbe.srWindow.Right;

    SetConsoleScreenBufferInfoEx(stdOutput, ref csbe);

    SetWindowPos(
        consoleHandle,
        nint.Zero,
        0, 0,
        windowWidth,
        windowHeight,
        0x0040);
    
    DrawMenuBar(consoleHandle);

    GetConsoleScreenBufferInfoEx(stdOutput, ref csbe);

    return new(csbe.dwSize.X, csbe.dwSize.Y);
}

通过将缓冲区高度和宽度设置为 1 来修复此问题。

© www.soinside.com 2019 - 2024. All rights reserved.