Exemplo n.º 1
0
        private static void Initialize()
        {
            if (isInitialized)
            {
                return;
            }

            IntPtr hDC = UnsafeNativeMethods.GetDC(CAPS.NullHandleRef);

            if (hDC != IntPtr.Zero)
            {
                deviceDpi = UnsafeNativeMethods.GetDeviceCaps(new HandleRef(null, hDC), CAPS.LOGPIXELSX);

                UnsafeNativeMethods.ReleaseDC(CAPS.NullHandleRef, new HandleRef(null, hDC));
            }
            isInitialized = true;
        }
Exemplo n.º 2
0
        private static void Initialize()
        {
            if (s_isInitialized)
            {
                return;
            }

            IntPtr hDC = UnsafeNativeMethods.GetDC(NativeMethods.NullHandleRef);

            if (hDC != IntPtr.Zero)
            {
                s_deviceDpiX = UnsafeNativeMethods.GetDeviceCaps(new HandleRef(null, hDC), SafeNativeMethods.LOGPIXELSX);
                s_deviceDpiY = UnsafeNativeMethods.GetDeviceCaps(new HandleRef(null, hDC), SafeNativeMethods.LOGPIXELSY);

                UnsafeNativeMethods.ReleaseDC(NativeMethods.NullHandleRef, new HandleRef(null, hDC));
            }

            s_isInitialized = true;
        }
Exemplo n.º 3
0
 private static void Initialize()
 {
     if (isInitialized)
     {
         return;
     }
     if (IsDpiAwarenessValueSet())
     {
         enableHighDpi = true;
     }
     else
     {
         try
         {
             string text = ConfigurationManager.AppSettings.Get("EnableWindowsFormsHighDpiAutoResizing");
             if (!string.IsNullOrEmpty(text) && string.Equals(text, "true", StringComparison.InvariantCultureIgnoreCase))
             {
                 enableHighDpi = true;
             }
         }
         catch
         {
         }
     }
     if (enableHighDpi)
     {
         try
         {
             SetWinformsApplicationDpiAwareness();
         }
         catch (Exception)
         {
         }
         IntPtr dC = UnsafeNativeMethods.GetDC(NativeMethods.NullHandleRef);
         if (dC != IntPtr.Zero)
         {
             deviceDpi = UnsafeNativeMethods.GetDeviceCaps(new HandleRef(null, dC), 88);
             UnsafeNativeMethods.ReleaseDC(NativeMethods.NullHandleRef, new HandleRef(null, dC));
         }
     }
     isInitialized = true;
 }
Exemplo n.º 4
0
        private static void Initialize()
        {
            if (isInitialized)
            {
                return;
            }

            if (Environment.OSVersion.Platform != System.PlatformID.Win32NT)
            {
                deviceDpi = 96;     // Default $$$$
            }
            else
            {
                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;
        }
Exemplo n.º 5
0
        /// <include file='doc\FontDialog.uex' path='docs/doc[@for="FontDialog.UpdateFont"]/*' />
        /// <devdoc>
        /// </devdoc>
        /// <internalonly/>
        private void UpdateFont(NativeMethods.LOGFONT lf)
        {
            IntPtr screenDC = UnsafeNativeMethods.GetDC(NativeMethods.NullHandleRef);

            try {
                Font fontInWorldUnits = null;
                try {
                    fontInWorldUnits = Font.FromLogFont(lf, screenDC);

                    // The dialog claims its working in points (a device-independent unit),
                    // but actually gives us something in world units (device-dependent).
                    font = ControlPaint.FontInPoints(fontInWorldUnits);
                }
                finally {
                    if (fontInWorldUnits != null)
                    {
                        fontInWorldUnits.Dispose();
                    }
                }
            }
            finally {
                UnsafeNativeMethods.ReleaseDC(NativeMethods.NullHandleRef, new HandleRef(null, screenDC));
            }
        }
Exemplo n.º 6
0
        private static void Initialize()
        {
            if (s_isInitialized)
            {
                return;
            }

            // NOTE: In the .NET Framework, this value can be controlled via ConfigurationManager.
            // In .NET Core, the value always defaults to the value "true".

            if (s_enableHighDpi)
            {
                IntPtr hDC = UnsafeNativeMethods.GetDC(NativeMethods.NullHandleRef);
                if (hDC != IntPtr.Zero)
                {
                    s_deviceDpiX = UnsafeNativeMethods.GetDeviceCaps(new HandleRef(null, hDC), SafeNativeMethods.LOGPIXELSX);
                    s_deviceDpiY = UnsafeNativeMethods.GetDeviceCaps(new HandleRef(null, hDC), SafeNativeMethods.LOGPIXELSY);

                    UnsafeNativeMethods.ReleaseDC(NativeMethods.NullHandleRef, new HandleRef(null, hDC));
                }
            }

            s_isInitialized = true;
        }
Exemplo n.º 7
0
        /// <include file='doc\FontDialog.uex' path='docs/doc[@for="FontDialog.RunDialog"]/*' />
        /// <internalonly/>
        /// <devdoc>
        ///    <para>
        ///       The actual implementation of running the dialog. Inheriting classes
        ///       should override this if they want to add more functionality, and call
        ///       base.runDialog() if necessary
        ///
        ///    </para>
        /// </devdoc>
        protected override bool RunDialog(IntPtr hWndOwner)
        {
            NativeMethods.WndProc    hookProcPtr = new NativeMethods.WndProc(this.HookProc);
            NativeMethods.CHOOSEFONT cf          = new NativeMethods.CHOOSEFONT();
            IntPtr screenDC = UnsafeNativeMethods.GetDC(NativeMethods.NullHandleRef);

            NativeMethods.LOGFONT lf = new NativeMethods.LOGFONT();

            Graphics graphics = Graphics.FromHdcInternal(screenDC);

            try {
                Font.ToLogFont(lf, graphics);
            }
            finally {
                graphics.Dispose();
            }
            UnsafeNativeMethods.ReleaseDC(NativeMethods.NullHandleRef, new HandleRef(null, screenDC));

            IntPtr logFontPtr = IntPtr.Zero;

            try {
                logFontPtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(NativeMethods.LOGFONT)));
                Marshal.StructureToPtr(lf, logFontPtr, false);

                cf.lStructSize = Marshal.SizeOf(typeof(NativeMethods.CHOOSEFONT));
                cf.hwndOwner   = hWndOwner;
                cf.hDC         = IntPtr.Zero;
                cf.lpLogFont   = logFontPtr;
                cf.Flags       = Options | NativeMethods.CF_INITTOLOGFONTSTRUCT | NativeMethods.CF_ENABLEHOOK;
                if (minSize > 0 || maxSize > 0)
                {
                    cf.Flags |= NativeMethods.CF_LIMITSIZE;
                }

                //if ShowColor=true then try to draw the sample text in color,
                //if ShowEffects=false then we will draw the sample text in standard control text color regardless.
                //(limitation of windows control)
                //
                if (ShowColor || ShowEffects)
                {
                    cf.rgbColors = ColorTranslator.ToWin32(color);
                }
                else
                {
                    cf.rgbColors = ColorTranslator.ToWin32(SystemColors.ControlText);
                }

                cf.lpfnHook  = hookProcPtr;
                cf.hInstance = UnsafeNativeMethods.GetModuleHandle(null);
                cf.nSizeMin  = minSize;
                if (maxSize == 0)
                {
                    cf.nSizeMax = int.MaxValue;
                }
                else
                {
                    cf.nSizeMax = maxSize;
                }
                Debug.Assert(cf.nSizeMin <= cf.nSizeMax, "min and max font sizes are the wrong way around");
                if (!SafeNativeMethods.ChooseFont(cf))
                {
                    return(false);
                }


                NativeMethods.LOGFONT lfReturned = null;
                lfReturned = (NativeMethods.LOGFONT)UnsafeNativeMethods.PtrToStructure(logFontPtr, typeof(NativeMethods.LOGFONT));

                if (lfReturned.lfFaceName != null && lfReturned.lfFaceName.Length > 0)
                {
                    lf = lfReturned;
                    UpdateFont(lf);
                    UpdateColor(cf.rgbColors);
                }

                return(true);
            }
            finally {
                if (logFontPtr != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(logFontPtr);
                }
            }
        }