示例#1
0
        /// <include file='doc\Font.uex' path='docs/doc[@for="Font.FromLogFont1"]/*' />
        /// <devdoc>
        ///    <para>[To be supplied.]</para>
        /// </devdoc>
        public static Font FromLogFont(object lf, IntPtr hdc)
        {
            IntSecurity.ObjectFromWin32Handle.Demand();

            IntPtr font = IntPtr.Zero;
            int    status;

            if (Marshal.SystemDefaultCharSize == 1)
            {
                status = SafeNativeMethods.GdipCreateFontFromLogfontA(new HandleRef(null, hdc), lf, out font);
            }
            else
            {
                status = SafeNativeMethods.GdipCreateFontFromLogfontW(new HandleRef(null, hdc), lf, out font);
            }

            // Special case this incredibly common error message to give more information
            if (status == SafeNativeMethods.NotTrueTypeFont)
            {
                throw new ArgumentException(SR.GetString(SR.GdiplusNotTrueTypeFont_NoName));
            }
            else if (status != SafeNativeMethods.Ok)
            {
                throw SafeNativeMethods.StatusException(status);
            }

            // GDI+ returns font = 0 even though the status is Ok.
            if (font == IntPtr.Zero)
            {
                throw new ArgumentException("GDI+ does not handle non True-type fonts: " + lf.ToString());
            }

            bool gdiVerticalFont;

            if (Marshal.SystemDefaultCharSize == 1)
            {
                gdiVerticalFont = (Marshal.ReadByte(lf, LogFontNameOffset) == (byte)(short)'@');
            }
            else
            {
                gdiVerticalFont = (Marshal.ReadInt16(lf, LogFontNameOffset) == (short)'@');
            }

            return(new Font(font, Marshal.ReadByte(lf, LogFontCharSetOffset), gdiVerticalFont));
        }