private static void Initialize() { if (isInitialized) { return; } if (IsDpiAwarenessValueSet()) { enableHighDpi = true; } else { try { // For legacy users who define this constant in app settings. But we read it only when we do not see a valid dpiawareness value in the Microsoft section string value = ConfigurationManager.AppSettings.Get(ConfigurationStringConstants.EnableWindowsFormsHighDpiAutoResizingKeyName); if (!string.IsNullOrEmpty(value) && string.Equals(value, "true", StringComparison.InvariantCultureIgnoreCase)) { enableHighDpi = true; } } catch { } } if (enableHighDpi) { #if Microsoft_NAMESPACE try { if (!DpiHelper.SetWinformsApplicationDpiAwareness()) { System.Diagnostics.Debug.WriteLine("Failed to set Application DPI awareness"); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Failed to set Application DPI awareness " + ex.ToString()); } #endif IntPtr hDC = UnsafeNativeMethods.GetDC(NativeMethods.NullHandleRef); if (hDC != IntPtr.Zero) { deviceDpi = UnsafeNativeMethods.GetDeviceCaps(new HandleRef(null, hDC), CAPS.LOGPIXELSX); UnsafeNativeMethods.ReleaseDC(NativeMethods.NullHandleRef, new HandleRef(null, hDC)); } } isInitialized = true; }