ForceDpiAware.cs 1.3 KB

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