Script for automating a large assortment of AME related actions
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

200 lines
6.8 KiB

9 months ago
  1. using System;
  2. using System.Diagnostics;
  3. using System.Runtime.InteropServices;
  4. using System.Windows.Forms;
  5. using amecs;
  6. namespace Ameliorated.ConsoleUtils
  7. {
  8. public static partial class ConsoleTUI
  9. {
  10. private const int MF_BYCOMMAND = 0x00000000;
  11. private const int SC_CLOSE = 0xF060;
  12. private const int SC_MINIMIZE = 0xF020;
  13. private const int SC_MAXIMIZE = 0xF030;
  14. private const int SC_SIZE = 0xF000; //resize
  15. private const uint CHECK_QUICK_EDIT = 0x0040;
  16. private const int ENABLE_QUICK_EDIT = 0x40 | 0x80;
  17. // STD_INPUT_HANDLE (DWORD): -10 is the standard input device.
  18. private const int STD_INPUT_HANDLE = -10;
  19. private static string PreviousTitle;
  20. private static int PreviousBufferHeight = 26;
  21. private static int PreviousBufferWidth = 80;
  22. private static int PreviousSizeHeight = 26;
  23. private static int PreviousSizeWidth = 80;
  24. private static bool IsInitialized;
  25. private static int InitializedWidth = 80;
  26. public static void ShowErrorBox(string message, string caption)
  27. {
  28. NativeWindow window = new NativeWindow();
  29. window.AssignHandle(Process.GetCurrentProcess().MainWindowHandle);
  30. MessageBox.Show(window, message, caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
  31. }
  32. public enum BackdropType
  33. {
  34. None = 1,
  35. Mica = 2,
  36. Acrylic = 3,
  37. Tabbed = 4
  38. }
  39. [DllImport("dwmapi.dll")]
  40. private static extern int DwmSetWindowAttribute(IntPtr hwnd, int attribute, ref int pvAttribute, int cbAttribute);
  41. public static void Initialize(string title, int width = 80, int height = 26, bool resize = false, bool quickedit = false)
  42. {
  43. if (width < 2) throw new ArgumentException("Width must be greater than one.");
  44. IsInitialized = true;
  45. PreviousSizeHeight = Console.WindowHeight;
  46. PreviousSizeWidth = Console.WindowWidth;
  47. PreviousBufferHeight = Console.BufferHeight;
  48. PreviousBufferWidth = Console.BufferWidth;
  49. Console.SetWindowSize(width, height);
  50. Console.SetBufferSize(width, height);
  51. Console.SetWindowSize(width, height);
  52. InitializedWidth = width;
  53. Console.Clear();
  54. Console.CursorVisible = false;
  55. PreviousTitle = Console.Title;
  56. Console.Title = title;
  57. try
  58. {
  59. if ((Console.CursorLeft == 0 && Console.CursorTop == 0) || ParentProcess.ProcessName.Equals("Explorer", StringComparison.OrdinalIgnoreCase))
  60. {
  61. var bd = (int)BackdropType.Mica;
  62. var trueA = 0x01;
  63. if (Globals.WinVer >= 22523)
  64. {
  65. var handle = Process.GetCurrentProcess().MainWindowHandle;
  66. DwmSetWindowAttribute(handle, 38, ref bd, Marshal.SizeOf<int>());
  67. DwmSetWindowAttribute(handle, 20, ref trueA, Marshal.SizeOf<int>());
  68. }
  69. }
  70. } catch (Exception e) { }
  71. if (!resize)
  72. try
  73. {
  74. DisableResize();
  75. } catch (Exception e)
  76. {
  77. ConsoleUtils.WriteError("Error disabling window resize - " + e.Message);
  78. }
  79. if (!quickedit)
  80. try
  81. {
  82. DisableQuickEdit();
  83. } catch (Exception e)
  84. {
  85. ConsoleUtils.WriteError("Error disabling quickedit - " + e.Message);
  86. }
  87. }
  88. public static void Close()
  89. {
  90. if (!IsInitialized) throw new MethodAccessException("Console TUI must be initialized before calling other TUI functions.");
  91. IsInitialized = false;
  92. var parent = ParentProcess.ProcessName;
  93. if (parent.Equals("Explorer", StringComparison.CurrentCultureIgnoreCase)) return;
  94. try
  95. {
  96. EnableResize();
  97. } catch (Exception e)
  98. {
  99. ConsoleUtils.WriteError("Error enabling window resize - " + e.Message);
  100. }
  101. try
  102. {
  103. EnableQuickEdit();
  104. } catch (Exception e)
  105. {
  106. ConsoleUtils.WriteError("Error enabling quickedit - " + e.Message);
  107. }
  108. Console.CursorVisible = true;
  109. Console.Clear();
  110. Console.Title = PreviousTitle;
  111. Console.SetWindowSize(PreviousSizeWidth, PreviousSizeHeight);
  112. Console.SetBufferSize(PreviousBufferWidth, PreviousBufferHeight);
  113. }
  114. [DllImport("user32.dll")]
  115. public static extern int DeleteMenu(IntPtr hMenu, int nPosition, int wFlags);
  116. [DllImport("user32.dll")]
  117. private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
  118. [DllImport("kernel32.dll", ExactSpelling = true)]
  119. private static extern IntPtr GetConsoleWindow();
  120. private static void DisableResize()
  121. {
  122. var handle = GetConsoleWindow();
  123. var sysMenu = GetSystemMenu(handle, false);
  124. if (handle != IntPtr.Zero)
  125. {
  126. //DeleteMenu(sysMenu, SC_CLOSE, MF_BYCOMMAND);
  127. //DeleteMenu(sysMenu, SC_MINIMIZE, MF_BYCOMMAND);
  128. DeleteMenu(sysMenu, SC_MAXIMIZE, MF_BYCOMMAND);
  129. DeleteMenu(sysMenu, SC_SIZE, MF_BYCOMMAND); //resize
  130. }
  131. }
  132. private static void EnableResize()
  133. {
  134. var handle = GetConsoleWindow();
  135. GetSystemMenu(handle, true);
  136. }
  137. [DllImport("kernel32.dll", SetLastError = true)]
  138. private static extern IntPtr GetStdHandle(int nStdHandle);
  139. [DllImport("kernel32.dll")]
  140. private static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode);
  141. [DllImport("kernel32.dll")]
  142. private static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode);
  143. private static void DisableQuickEdit()
  144. {
  145. var consoleHandle = GetStdHandle(STD_INPUT_HANDLE);
  146. // get current console mode
  147. uint consoleMode;
  148. GetConsoleMode(consoleHandle, out consoleMode);
  149. // set the new mode
  150. SetConsoleMode(consoleHandle, consoleMode &= ~CHECK_QUICK_EDIT);
  151. }
  152. private static void EnableQuickEdit()
  153. {
  154. var consoleHandle = GetStdHandle(STD_INPUT_HANDLE);
  155. // get current console mode
  156. uint consoleMode;
  157. GetConsoleMode(consoleHandle, out consoleMode);
  158. // set the new mode
  159. SetConsoleMode(consoleHandle, consoleMode | ENABLE_QUICK_EDIT);
  160. }
  161. }
  162. }