private static readonly int LOGPIXELSX = 88; // Used for GetDeviceCaps(). private static readonly int LOGPIXELSY = 90; // Used for GetDeviceCaps().
/// <summary>Determines the current screen resolution in DPI.</summary>
/// <returns>Point.X is the X DPI, Point.Y is the Y DPI.</returns>
public static Point GetDpi()
{
Point result = new Point();
IntPtr hDC = GetDC(IntPtr.Zero);
result.X = GetDeviceCaps(hDC, LOGPIXELSX);
result.Y = GetDeviceCaps(hDC, LOGPIXELSY);
ReleaseDC(IntPtr.Zero, hDC);
return result;
}
/// <summary>
/// Checks if font is not default.
/// </summary>
/// <returns>True if font DPI is not 96.</returns>
public static bool IsDifferentFont()
{
Point result = GetDpi();
return result.X != 96 || result.Y != 96;
}
[DllImport("gdi32.dll")]
private static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
[DllImport("user32.dll")]
private static extern IntPtr GetDC(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
private double PtToCNT(int Width)
{
int dpi = GetDpi().X;
return (Width * 2.54) / dpi;
}
private double CNTToPt(int Width)
{
int dpi = GetDpi().X;
return (Width * dpi) / 2.54;
}