![](/img/trans.png)
[英]How to remove Vertical and Horizontal scroll bar from DataGridView in C sharp?
[英]How to remove scroll bar from fullscreen Console in C#?
長話短說:大約一周前我剛剛開始學習 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();
所以我設法成功進入全屏,但問題是滾動條仍然存在。
我已盡最大努力准確了解這是如何工作的以及如何刪除滾動條。 我目前的理解是需要更改Console 的屏幕緩沖區以匹配屏幕的大小,以便滾動條可以消失。
從我設法拼湊的代碼來看,似乎緩沖區已經在播放以全屏顯示。 看起來 'SetConsoleDisplayMode' 方法就是這樣做的。
所以我的問題是,如何將滾動條的刪除添加到此代碼中? 我的想法是說它與“COORD”結構有關,但老實說,我完全不喜歡這里的新語言和新概念(如結構),任何幫助將不勝感激!
當您的緩沖區高度等於您的屏幕高度(以行為單位)時,滾動條將消失。 您可以使用 noraml 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);
這個課程絕對適合你,它會自動全屏顯示控制台並刪除滾動條清晰和黑色的控制台屏幕只是appers,你可以做任何你想做的##
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 方法中寫:
SetLayout s = new SetLayout(); s.Set();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.