/// <summary> /// Creates the font handle. /// </summary> private void CreateFont() { Debug.Assert(hFont == IntPtr.Zero, "hFont is not null, this will generate a handle leak."); Debug.Assert(logFont != null, "WindowsFont.logFont not initialized."); hFont = IntUnsafeNativeMethods.CreateFontIndirect(logFont); #if TRACK_HFONT Debug.WriteLine(DbgUtil.StackTraceToStr(String.Format("HFONT[0x{0:x8}] = CreateFontIndirect( LOGFONT={1} )", (int)this.hFont, this.logFont))); #endif if (hFont == IntPtr.Zero) { logFont.lfFaceName = defaultFaceName; logFont.lfOutPrecision = IntNativeMethods.OUT_TT_ONLY_PRECIS; // True Type only. hFont = IntUnsafeNativeMethods.CreateFontIndirect(logFont); #if TRACK_HFONT Debug.WriteLine(DbgUtil.StackTraceToStr(String.Format("HFONT[0x{0:x8}] = CreateFontIndirect( LOGFONT={1} )", (int)this.hFont, this.logFont))); #endif } // Update logFont height and other adjusted parameters. // IntUnsafeNativeMethods.GetObject(new HandleRef(this, hFont), logFont); // We created the hFont, we will delete it on dispose. ownHandle = true; }
/// <summary> /// Returns a handle to this <see cref='Font'/>. /// </summary> public IntPtr ToHfont() { using (ScreenDC dc = ScreenDC.Create()) using (Graphics graphics = Graphics.FromHdcInternal(dc)) { SafeNativeMethods.LOGFONT lf = ToLogFontInternal(graphics); IntPtr handle = IntUnsafeNativeMethods.CreateFontIndirect(ref lf); if (handle == IntPtr.Zero) { throw new Win32Exception(); } return(handle); } }