Skip to content

Instantly share code, notes, and snippets.

@toptensoftware
Created November 9, 2017 21:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toptensoftware/a6b8ca2cfc9ac63e7b6687968db408a2 to your computer and use it in GitHub Desktop.
Save toptensoftware/a6b8ca2cfc9ac63e7b6687968db408a2 to your computer and use it in GitHub Desktop.
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