From 9a43fe87f671685e110ebc137f52c0fb3ebde649 Mon Sep 17 00:00:00 2001 From: BDisp Date: Thu, 16 Jul 2020 15:43:40 +0100 Subject: [PATCH] Fixes #787. Console default size is always restored on Driver Init. --- Terminal.Gui/ConsoleDrivers/WindowsDriver.cs | 40 ++++++++++---------- Terminal.Gui/Core/Application.cs | 5 +-- 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/Terminal.Gui/ConsoleDrivers/WindowsDriver.cs b/Terminal.Gui/ConsoleDrivers/WindowsDriver.cs index 756fa880c..ccd68efef 100644 --- a/Terminal.Gui/ConsoleDrivers/WindowsDriver.cs +++ b/Terminal.Gui/ConsoleDrivers/WindowsDriver.cs @@ -439,6 +439,23 @@ namespace Terminal.Gui { } } + [DllImport ("kernel32.dll", ExactSpelling = true)] + private static extern IntPtr GetConsoleWindow (); + + [DllImport ("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] + private static extern bool ShowWindow (IntPtr hWnd, int nCmdShow); + + public const int HIDE = 0; + public const int MAXIMIZE = 3; + public const int MINIMIZE = 6; + public const int RESTORE = 9; + + internal void ShowWindow (int state) + { + IntPtr thisConsole = GetConsoleWindow (); + ShowWindow (thisConsole, state); + } + #if false // See: https://github.com/migueldeicaza/gui.cs/issues/357 [StructLayout (LayoutKind.Sequential)] public struct SMALL_RECT { @@ -495,30 +512,15 @@ namespace Terminal.Gui { public override int Rows => rows; public WindowsDriver () - { - Initialize (); - } - - public WindowsDriver (int cols, int rows) - { - this.cols = cols; - this.rows = rows; - - Initialize (); - } - - void Initialize () { winConsole = new WindowsConsole (); SetupColorsAndBorders (); - if (cols == 0 && rows == 0) { - cols = Console.WindowWidth; - rows = Console.WindowHeight; - } else { - Console.SetWindowSize (cols, rows); - } + cols = Console.WindowWidth; + rows = Console.WindowHeight; + winConsole.ShowWindow (WindowsConsole.RESTORE); + WindowsConsole.SmallRect.MakeEmpty (ref damageRegion); ResizeScreen (); diff --git a/Terminal.Gui/Core/Application.cs b/Terminal.Gui/Core/Application.cs index e78801bae..0cbcb16a6 100644 --- a/Terminal.Gui/Core/Application.cs +++ b/Terminal.Gui/Core/Application.cs @@ -161,7 +161,6 @@ namespace Terminal.Gui { internal static bool _initialized = false; - static int cols, rows; static IMainLoopDriver oldMainLoopDriver; static ConsoleDriver oldDriver; @@ -189,7 +188,7 @@ namespace Terminal.Gui { mainLoopDriver = new NetMainLoop (() => Console.ReadKey (true)); Driver = new NetDriver (); } else if (p == PlatformID.Win32NT || p == PlatformID.Win32S || p == PlatformID.Win32Windows) { - var windowsDriver = cols == 0 && rows == 0 ? new WindowsDriver () : new WindowsDriver (cols, rows); + var windowsDriver = new WindowsDriver (); mainLoopDriver = windowsDriver; Driver = windowsDriver; } else { @@ -557,8 +556,6 @@ namespace Terminal.Gui { } last?.PositionCursor (); Driver.Refresh (); - cols = Driver.Cols; - rows = Driver.Rows; } internal static void End (View view, bool closeDriver = true)