Created
November 9, 2017 21:34
-
-
Save toptensoftware/a6b8ca2cfc9ac63e7b6687968db408a2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Runtime.InteropServices; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace DpiTest | |
{ | |
class Program | |
{ | |
public enum MONITOR_DPI_TYPE | |
{ | |
MDT_EFFECTIVE_DPI = 0, | |
MDT_ANGULAR_DPI = 1, | |
MDT_RAW_DPI = 2, | |
MDT_DEFAULT = MDT_EFFECTIVE_DPI | |
} | |
[DllImport("shcore.dll")] | |
public static extern uint GetDpiForMonitor(IntPtr hMonitor, MONITOR_DPI_TYPE type, out int dpiX, out int dpiY); | |
public const int MONITOR_DEFAULTTONULL = 0; | |
public const int MONITOR_DEFAULTTOPRIMARY = 1; | |
public const int MONITOR_DEFAULTTONEAREST = 2; | |
[DllImport("user32.dll", ExactSpelling = true)] | |
public static extern IntPtr MonitorFromWindow(IntPtr handle, int flags); | |
[DllImport("user32.dll", SetLastError = false)] | |
public static extern IntPtr GetDesktopWindow(); | |
[DllImport("user32.dll", SetLastError = true)] | |
public static extern bool SetProcessDPIAware(); | |
static void Main(string[] args) | |
{ | |
SetProcessDPIAware(); | |
IntPtr hMonitor = MonitorFromWindow(GetDesktopWindow(), MONITOR_DEFAULTTOPRIMARY); | |
int dpi, unused; | |
GetDpiForMonitor(hMonitor, MONITOR_DPI_TYPE.MDT_RAW_DPI, out dpi, out unused); | |
Console.WriteLine("Raw DPI: {0}", dpi); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment