static DpiHelper() { // Getting the Desktop, so we shouldn't have to release this DC. // This could potentially throw if used in a service. IntPtr desktop = NativeMethods.GetDC(IntPtr.Zero); // Can get these in the static constructor. They shouldn't vary window to window, // and changing the system DPI requires a restart. int pixelsPerInchX = NativeMethods.GetDeviceCaps(desktop, DeviceCap.LOGPIXELSX); int pixelsPerInchY = NativeMethods.GetDeviceCaps(desktop, DeviceCap.LOGPIXELSY); _transformToDip = Matrix.Identity; _transformToDip.Scale(96d / (double)pixelsPerInchX, 96d / (double)pixelsPerInchY); _transformToDevice = Matrix.Identity; _transformToDevice.Scale((double)pixelsPerInchX / 96d, (double)pixelsPerInchY / 96d); }
public static Standard.SafeDC GetDC(IntPtr hwnd) { Standard.SafeDC dC = null; try { dC = NativeMethods.GetDC(hwnd); } finally { if (dC != null) { dC.Hwnd = hwnd; } } if (dC.IsInvalid) { Standard.HRESULT.E_FAIL.ThrowIfFailed(); } return(dC); }
public static SafeDC GetDC(IntPtr hwnd) { SafeDC dc = null; try { dc = NativeMethods.GetDC(hwnd); } finally { if (dc != null) { dc.Hwnd = hwnd; } } if (dc.IsInvalid) { // GetDC does not set the last error... HRESULT.E_FAIL.ThrowIfFailed(); } return(dc); }