长话短说:大约一周前,我刚刚接过C#。我的大部分编程知识都来自Java。我想尝试制作一个文本冒险游戏(是的,我知道我可以使用Unity,但我想从头开始),设置的一部分是获得一个没有干扰的全屏幕控制台窗口。
我已经在StackOverflow上搜寻过了,到目前为止我已经把这些放在一起了:
[DllImport("kernel32.dll")]
private static extern IntPtr GetStdHandle(int handle);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool SetConsoleDisplayMode(IntPtr ConsoleOutput, uint Flags, out COORD NewScreenBufferDimensions);
[StructLayout(LayoutKind.Sequential)]
public struct COORD
{
public short X;
public short Y;
public COORD(short X, short Y)
{
this.X = X;
this.Y = Y;
}
}在Main方法中,除了这一位之外:
IntPtr hConsole = GetStdHandle(-11);
SetConsoleDisplayMode(hConsole, 1, out COORD b1);
Console.ReadLine();所以我已经成功地进入全屏,但问题是滚动条仍然在那里。
我已经尽了最大的努力来准确地理解这是如何工作的,以及如何删除滚动条。我目前的理解是,控制台的屏幕缓冲区需要更改以匹配屏幕的大小,以便滚动条可以消失。
从我设法拼凑的代码来看,缓冲区似乎已经被使用,以便全屏显示。看起来'SetConsoleDisplayMode‘方法可以做到这一点。
所以我的问题是,我如何在代码中添加删除滚动条的功能?我的想法是它与'COORD‘结构有关,但老实说,我完全不熟悉这里的新语言和新概念(如结构),任何帮助都将不胜感激!
发布于 2018-05-04 04:43:45
当缓冲区高度等于屏幕高度(以行为单位)时,滚动条将消失。你可以用一个普通的cmd.exe窗口来测试它。
当你让你的主机全屏时,如果缓冲区太小,Windows会让你的缓冲区变大,并在SetConsoleDisplayMode的最后一个参数中返回新的大小。在项目进展过程中,您可能需要这样的大小。所以如果你让缓冲区变得很小,Windows会帮你填充“修复”它。
为了更改缓冲区大小,您需要调用SetConsoleScreenBufferSize
BOOL WINAPI SetConsoleScreenBufferSize(
_In_ HANDLE hConsoleOutput,
_In_ COORD dwSize
);它看起来像这样(未经测试):
[DllImport("kernel32.dll")]
private static extern bool SetConsoleScreenBufferSize(int handle, COORD newSize);发布于 2020-06-12 12:41:09
这个类肯定是为你工作,它全屏自动,控制台和删除滚动条,清晰和黑色的控制台屏幕是应用程序,你做任何你想要的##
public class SetLayout
{
// Control the console whole setting
public void Set()
{
Console.SetBufferSize(Console.LargestWindowWidth, Console.LargestWindowHeight); // Remove the console both scroll bars
IntPtr hConsole = FullScreen.GetStdHandle(-11); // Get console handle
FullScreen.COORD xy = new FullScreen.COORD(100, 100);
FullScreen.SetConsoleDisplayMode(hConsole, 1, out xy); // Set the console to fullscreen
AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit); // Set to not show the "Press any key to continue"
Console.CursorVisible = false; // Remove the cursor from the console
}
// Handler Method to remove the "Press any key to continue........."
void CurrentDomain_ProcessExit(object sender, EventArgs e)
{
FullScreen.ShowWindow(FullScreen.ThisConsole,0);
}
}
// Class is setting the console window full screen
internal static class FullScreen
{
[StructLayout(LayoutKind.Sequential)]
public struct COORD
{
public short X;
public short Y;
public COORD(short x, short y)
{
this.X = x;
this.Y = y;
}
}
[DllImport("kernel32.dll")]
public static extern IntPtr GetStdHandle(int handle);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool SetConsoleDisplayMode(IntPtr ConsoleOutput, uint Flags, out COORD NewScreenBufferDimensions);
[DllImport("kernel32.dll", ExactSpelling = true)]
public static extern IntPtr GetConsoleWindow();
public static IntPtr ThisConsole = GetConsoleWindow();
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
}您只需在main方法中编写:
新建s= SetLayout SetLayout();s.Set();
https://stackoverflow.com/questions/50163122
复制相似问题