ConsoleHelper.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using Ryujinx.Common.Logging;
  2. using System;
  3. using System.Runtime.InteropServices;
  4. using System.Runtime.Versioning;
  5. namespace Ryujinx.Ui.Common.Helper
  6. {
  7. public static class ConsoleHelper
  8. {
  9. public static bool SetConsoleWindowStateSupported => OperatingSystem.IsWindows();
  10. public static void SetConsoleWindowState(bool show)
  11. {
  12. if (OperatingSystem.IsWindows())
  13. {
  14. SetConsoleWindowStateWindows(show);
  15. }
  16. else if (show == false)
  17. {
  18. Logger.Warning?.Print(LogClass.Application, "OS doesn't support hiding console window");
  19. }
  20. }
  21. [SupportedOSPlatform("windows")]
  22. private static void SetConsoleWindowStateWindows(bool show)
  23. {
  24. const int SW_HIDE = 0;
  25. const int SW_SHOW = 5;
  26. IntPtr hWnd = GetConsoleWindow();
  27. if (hWnd == IntPtr.Zero)
  28. {
  29. Logger.Warning?.Print(LogClass.Application, "Attempted to show/hide console window but console window does not exist");
  30. return;
  31. }
  32. ShowWindow(hWnd, show ? SW_SHOW : SW_HIDE);
  33. }
  34. [SupportedOSPlatform("windows")]
  35. [DllImport("kernel32")]
  36. static extern IntPtr GetConsoleWindow();
  37. [SupportedOSPlatform("windows")]
  38. [DllImport("user32")]
  39. static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
  40. }
  41. }