예제 #1
0
        public static Font FromHfont(IntPtr hfont)
        {
            IntPtr    newObject;
            IntPtr    hdc;
            FontStyle newStyle = FontStyle.Regular;
            float     newSize;
            LOGFONT   lf = new LOGFONT();

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

            if (GDIPlus.RunningOnUnix())
            {
                // If we're on Unix we use our private gdiplus API to avoid Wine
                // dependencies in S.D
                Status s = GDIPlus.GdipCreateFontFromHfont(hfont, out newObject, ref lf);
                GDIPlus.CheckStatus(s);
            }
            else
            {
                // This needs testing
                // GetDC, SelectObject, ReleaseDC GetTextMetric and
                // GetFontFace are not really GDIPlus, see gdipFunctions.cs

                newStyle = FontStyle.Regular;

                hdc = GDIPlus.GetDC(IntPtr.Zero);
                try
                {
                    return(FromLogFont(lf, hdc));
                }
                finally
                {
                    GDIPlus.ReleaseDC(IntPtr.Zero, hdc);
                }
            }

            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, newStyle, newSize));
        }