ForceDpiAware.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using Ryujinx.Common.Logging;
  2. using System;
  3. using System.Drawing;
  4. using System.Runtime.InteropServices;
  5. using System.Runtime.Versioning;
  6. namespace Ryujinx.Common.System
  7. {
  8. public static class ForceDpiAware
  9. {
  10. [DllImport("user32.dll")]
  11. private static extern bool SetProcessDPIAware();
  12. private static readonly double _standardDpiScale = 96.0;
  13. private static readonly double _maxScaleFactor = 1.25;
  14. /// <summary>
  15. /// Marks the application as DPI-Aware when running on the Windows operating system.
  16. /// </summary>
  17. public static void Windows()
  18. {
  19. // Make process DPI aware for proper window sizing on high-res screens.
  20. if (OperatingSystem.IsWindowsVersionAtLeast(6))
  21. {
  22. SetProcessDPIAware();
  23. }
  24. }
  25. public static double GetWindowScaleFactor()
  26. {
  27. double userDpiScale = 96.0;
  28. try
  29. {
  30. if (OperatingSystem.IsWindows())
  31. {
  32. userDpiScale = Graphics.FromHwnd(IntPtr.Zero).DpiX;
  33. }
  34. else
  35. {
  36. // TODO: Linux support
  37. }
  38. }
  39. catch (Exception e)
  40. {
  41. Logger.Warning?.Print(LogClass.Application, $"Couldn't determine monitor DPI: {e.Message}");
  42. }
  43. return Math.Min(userDpiScale / _standardDpiScale, _maxScaleFactor);
  44. }
  45. }
  46. }