-
Notifications
You must be signed in to change notification settings - Fork 78
/
DpiHelper.cs
47 lines (41 loc) · 1.35 KB
/
DpiHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System;
using System.Drawing;
using PiP_Tool.Native;
namespace PiP_Tool.Shared.Helpers
{
public static class DpiHelper
{
/// <summary>
/// Get monitor dpi for a window
/// </summary>
/// <param name="hwnd">handle of the window</param>
/// <param name="dpiX">out dpi X (horizontal)</param>
/// <param name="dpiY">out dpi Y (vertical)</param>
public static void GetDpi(IntPtr hwnd, out float dpiX, out float dpiY)
{
try
{
var handle = NativeMethods.MonitorFromWindow(hwnd, MonitorDefaultTo.MONITOR_DEFAULTTONEAREST);
NativeMethods.GetDpiForMonitor(handle, MONITOR_DPI_TYPE.MDT_EFFECTIVE_DPI, out var x, out var y);
dpiX = x / 96f;
dpiY = y / 96f;
}
catch (Exception)
{
dpiX = 1;
dpiY = 1;
}
}
/// <summary>
/// Get dpi of primary monitor
/// </summary>
/// <param name="dpiX">out dpi X (horizontal)</param>
/// <param name="dpiY">out dpi Y (vertical)</param>
public static void GetPrimaryDpi(out float dpiX, out float dpiY)
{
var g = Graphics.FromHwnd(IntPtr.Zero);
dpiX = g.DpiX / 96f;
dpiY = g.DpiY / 96f;
}
}
}