-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #98 from AnonymousII/ScreenScaling
HighDPI Screen Scaling
- Loading branch information
Showing
2 changed files
with
57 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace GW_Launcher.Utilities; | ||
internal static class ScreenScaling | ||
{ | ||
[DllImport("gdi32.dll")] | ||
static extern int GetDeviceCaps(IntPtr hdc, int nIndex); | ||
|
||
private enum DeviceCap | ||
{ | ||
VERTRES = 10, | ||
DESKTOPVERTRES = 117, | ||
//... http://pinvoke.net/default.aspx/gdi32/GetDeviceCaps.html | ||
} | ||
|
||
//get screen scaling factor set under settings -> system -> screen: scaling (e.g. 225%) | ||
public static float GetScreenScalingFactor() | ||
{ | ||
using Graphics g = Graphics.FromHwnd(IntPtr.Zero); | ||
IntPtr desktop = g.GetHdc(); | ||
|
||
int logicalScreenHeight = GetDeviceCaps(desktop, (int)DeviceCap.VERTRES); //virtual screen resolution scaled down for DPI-unaware app | ||
int physicalScreenHeight = GetDeviceCaps(desktop, (int)DeviceCap.DESKTOPVERTRES); //actual screen resolution, e.g. 3840 x 2160 | ||
|
||
g.ReleaseHdc(desktop); | ||
|
||
float screenScalingFactor = physicalScreenHeight / (float) logicalScreenHeight; | ||
return screenScalingFactor; // e.g. 1.25 = 125% | ||
} | ||
} |