Пример #1
0
        /// <include file='doc\Font.uex' path='docs/doc[@for="Font.FromHfont"]/*' />
        /// <devdoc>
        ///    Creates a <see cref='System.Drawing.Font'/> from the specified Windows
        ///    handle.
        /// </devdoc>
        public static Font FromHfont(IntPtr hfont)
        {
            SafeNativeMethods.LOGFONT lf = new SafeNativeMethods.LOGFONT();
            SafeNativeMethods.GetObject(new HandleRef(null, hfont), lf);

            Font   result;
            IntPtr screenDC = UnsafeNativeMethods.GetDC(NativeMethods.NullHandleRef);

            try
            {
                result = Font.FromLogFont(lf, screenDC);
            }
            finally
            {
                UnsafeNativeMethods.ReleaseDC(NativeMethods.NullHandleRef, new HandleRef(null, screenDC));
            }

            return(result);
        }
Пример #2
0
        public IntPtr ToHfont()
        {
            SafeNativeMethods.LOGFONT logFont = new SafeNativeMethods.LOGFONT();
            System.Drawing.IntSecurity.ObjectFromWin32Handle.Assert();
            try
            {
                this.ToLogFont(logFont);
            }
            finally
            {
                CodeAccessPermission.RevertAssert();
            }
            IntPtr ptr = IntUnsafeNativeMethods.IntCreateFontIndirect(logFont);

            if (ptr == IntPtr.Zero)
            {
                throw new Win32Exception();
            }
            return(ptr);
        }
Пример #3
0
        /// <include file='doc\Font.uex' path='docs/doc[@for="Font.ToHfont"]/*' />
        /// <devdoc>
        ///    Returns a handle to this <see cref='System.Drawing.Font'/>.
        /// </devdoc>
        public IntPtr ToHfont()
        {
            SafeNativeMethods.LOGFONT lf = new SafeNativeMethods.LOGFONT();

            IntSecurity.ObjectFromWin32Handle.Assert();
            try {
                this.ToLogFont(lf);
            }
            finally {
                System.Security.CodeAccessPermission.RevertAssert();
            }

            IntPtr handle = SafeNativeMethods.CreateFontIndirect(lf);

            if (handle == IntPtr.Zero)
            {
                throw new Win32Exception();
            }

            return(handle);
        }
Пример #4
0
        /// <summary>
        /// Creates a <see cref="Font"/> from the given LOGFONT using the given device context.
        /// </summary>
        /// <param name="lf">A boxed LOGFONT.</param>
        /// <param name="hdc">Handle to a device context (HDC).</param>
        /// <returns>The newly created <see cref="Font"/>.</returns>
        public unsafe static Font FromLogFont(object lf, IntPtr hdc)
        {
            if (lf == null)
            {
                throw new ArgumentNullException(nameof(lf));
            }

            if (lf is SafeNativeMethods.LOGFONT logFont)
            {
                // A boxed LOGFONT, just use it to create the font
                return(FromLogFontInternal(ref logFont, hdc));
            }

            Type type       = lf.GetType();
            int  nativeSize = sizeof(SafeNativeMethods.LOGFONT);

            if (Marshal.SizeOf(type) != nativeSize)
            {
                // If we don't actually have an object that is LOGFONT in size, trying to pass
                // it to GDI+ is likely to cause an AV.
                throw new ArgumentException();
            }

            // Now that we know the marshalled size is the same as LOGFONT, copy in the data
            logFont = new SafeNativeMethods.LOGFONT();

            if (!type.IsValueType)
            {
                // Only works with non value types
                Marshal.StructureToPtr(lf, new IntPtr(&logFont), fDeleteOld: false);
            }
            else
            {
                GCHandle handle = GCHandle.Alloc(lf, GCHandleType.Pinned);
                Buffer.MemoryCopy((byte *)handle.AddrOfPinnedObject(), &logFont, nativeSize, nativeSize);
                handle.Free();
            }

            return(FromLogFontInternal(ref logFont, hdc));
        }
Пример #5
0
        private static Font GetFontFromData(SafeNativeMethods.LOGFONT logFont)
        {
            if (logFont == null)
            {
                return(null);
            }

            Font font = null;

            try
            {
                font = Font.FromLogFont(logFont);
            }
            catch (Exception ex) when(!IsCriticalFontException(ex))
            {
            }

            return
                (font == null ? DefaultFont :
                 font.Unit != GraphicsUnit.Point ? FontInPoints(font) :
                 font);
        }
Пример #6
0
        internal static Font FromLogFontInternal(ref SafeNativeMethods.LOGFONT logFont, IntPtr hdc)
        {
            int status = Gdip.GdipCreateFontFromLogfontW(new HandleRef(null, hdc), ref logFont, out IntPtr font);

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

            // GDI+ returns font = 0 even though the status is Ok.
            if (font == IntPtr.Zero)
            {
                throw new ArgumentException(SR.Format(SR.GdiplusNotTrueTypeFont, logFont.ToString()));
            }

            bool gdiVerticalFont = logFont.lfFaceName[0] == '@';

            return(new Font(font, logFont.lfCharSet, gdiVerticalFont));
        }
 public static extern bool SystemParametersInfo(int uiAction, int uiParam, [In, Out] SafeNativeMethods.LOGFONT pvParam, int fWinIni);
Пример #8
0
        public IntPtr ToHfont() {
            SafeNativeMethods.LOGFONT lf = new SafeNativeMethods.LOGFONT();
               
            IntSecurity.ObjectFromWin32Handle.Assert();

            try {
                this.ToLogFont(lf);
            }
            finally {
                System.Security.CodeAccessPermission.RevertAssert();
            }

            IntPtr handle = IntUnsafeNativeMethods.IntCreateFontIndirect(lf);

            if (handle == IntPtr.Zero) {
                throw new Win32Exception();
            }

            return handle;

        }
Пример #9
0
        public static Font FromHfont(IntPtr hfont) {
            IntSecurity.ObjectFromWin32Handle.Demand();

            SafeNativeMethods.LOGFONT lf = new SafeNativeMethods.LOGFONT();
            SafeNativeMethods.GetObject(new HandleRef(null, hfont), lf);

            Font result;
            IntPtr screenDC = UnsafeNativeMethods.GetDC(NativeMethods.NullHandleRef);
            try {
                result = Font.FromLogFont(lf, screenDC);
            }
            finally {
                UnsafeNativeMethods.ReleaseDC(NativeMethods.NullHandleRef, new HandleRef(null, screenDC));
            }

            return result;
        }
Пример #10
0
        public static Font FromHfont(IntPtr hfont)
        {
            IntPtr    newObject;
            IntPtr    hdc;
            FontStyle newStyle = FontStyle.Regular;
            float     newSize;

            SafeNativeMethods.LOGFONT lf = new SafeNativeMethods.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 = SafeNativeMethods.Gdip.GdipCreateFontFromHfont(hfont, out newObject, ref lf);
                SafeNativeMethods.Gdip.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));
        }
 public IntPtr ToHfont()
 {
     SafeNativeMethods.LOGFONT logFont = new SafeNativeMethods.LOGFONT();
     System.Drawing.IntSecurity.ObjectFromWin32Handle.Assert();
     try
     {
         this.ToLogFont(logFont);
     }
     finally
     {
         CodeAccessPermission.RevertAssert();
     }
     IntPtr ptr = IntUnsafeNativeMethods.IntCreateFontIndirect(logFont);
     if (ptr == IntPtr.Zero)
     {
         throw new Win32Exception();
     }
     return ptr;
 }
 public static Font FromHfont(IntPtr hfont)
 {
     Font font;
     System.Drawing.IntSecurity.ObjectFromWin32Handle.Demand();
     SafeNativeMethods.LOGFONT lp = new SafeNativeMethods.LOGFONT();
     SafeNativeMethods.GetObject(new HandleRef(null, hfont), lp);
     IntPtr dC = System.Drawing.UnsafeNativeMethods.GetDC(System.Drawing.NativeMethods.NullHandleRef);
     try
     {
         font = FromLogFont(lp, dC);
     }
     finally
     {
         System.Drawing.UnsafeNativeMethods.ReleaseDC(System.Drawing.NativeMethods.NullHandleRef, new HandleRef(null, dC));
     }
     return font;
 }