コード例 #1
0
        public static Font FromHfont(IntPtr hfont)
        {
            IntPtr    newObject;
            FontStyle newStyle = FontStyle.Regular;
            float     newSize;

            Interop.User32.LOGFONT lf = default;

            // Sanity. Should we throw an exception?
            if (hfont == IntPtr.Zero)
            {
                Font result = new Font("Arial", (float)10.0, FontStyle.Regular);
                return(result);
            }

            // If we're on Unix we use our private gdiplus API to avoid Wine
            // dependencies in S.D
            int s = Gdip.GdipCreateFontFromHfont(hfont, out newObject, ref lf);

            Gdip.CheckStatus(s);

            if (lf.lfItalic != 0)
            {
                newStyle |= FontStyle.Italic;
            }

            if (lf.lfUnderline != 0)
            {
                newStyle |= FontStyle.Underline;
            }

            if (lf.lfStrikeOut != 0)
            {
                newStyle |= FontStyle.Strikeout;
            }

            if (lf.lfWeight > 400)
            {
                newStyle |= FontStyle.Bold;
            }

            if (lf.lfHeight < 0)
            {
                newSize = lf.lfHeight * -1;
            }
            else
            {
                newSize = lf.lfHeight;
            }

            return(new Font(newObject, lf.lfFaceName.ToString(), newStyle, newSize));
        }