/// <summary> /// Creates the font handle. /// </summary> private unsafe WindowsFont(User32.LOGFONTW logFont, FontStyle style, bool createHandle) { Debug.Assert(Hfont == IntPtr.Zero, "hFont is not null, this will generate a handle leak."); _logFont = logFont; if (_logFont.FaceName.Length == 0) { _logFont.FaceName = DefaultFaceName; } Style = style; if (createHandle) { Hfont = Gdi32.CreateFontIndirectW(ref _logFont); if (Hfont == IntPtr.Zero) { _logFont.FaceName = DefaultFaceName; _logFont.lfOutPrecision = Gdi32.OUT_PRECIS.TT_ONLY; // TrueType only. Hfont = Gdi32.CreateFontIndirectW(ref _logFont); } // Update logFont height and other adjusted parameters. Gdi32.GetObjectW(new HandleRef(this, Hfont), out _logFont); // We created the hFont, we will delete it on dispose. _ownHandle = true; } }
public unsafe void CreateFontIndirect() { User32.LOGFONTW logFont = default; IntPtr handle = Gdi32.CreateFontIndirectW(ref logFont); Assert.NotEqual(IntPtr.Zero, handle); Assert.True(Gdi32.DeleteObject(handle).IsTrue()); }
public unsafe void CreateFontIndirect() { LOGFONTW logFont = default; Gdi32.HFONT handle = Gdi32.CreateFontIndirectW(ref logFont); Assert.False(handle.IsNull); Assert.True(Gdi32.DeleteObject(handle).IsTrue()); }
/// <summary> /// Constructs a WindowsFont object from an existing System.Drawing.Font object (GDI+), based on the screen dc /// MapMode and resolution (normally: MM_TEXT and 96 dpi). /// </summary> private static Gdi32.HFONT FromFont(Font font, Gdi32.QUALITY quality = Gdi32.QUALITY.DEFAULT) { string familyName = font.FontFamily.Name; // Strip vertical-font mark from the name if needed. if (familyName != null && familyName.Length > 1 && familyName[0] == '@') { familyName = familyName.Substring(1); } // Now, creating it using the Font.SizeInPoints makes it GraphicsUnit-independent. Debug.Assert(font.SizeInPoints > 0.0f, "size has a negative value."); // Get the font height from the specified size. The size is in point units and height in logical units // (pixels when using MM_TEXT) so we need to make the conversion using the number of pixels per logical // inch along the screen height. (1 point = 1/72 inch.) int pixelsY = (int)Math.Ceiling(DpiHelper.DeviceDpi * font.SizeInPoints / 72); // The lfHeight represents the font cell height (line spacing) which includes the internal leading; we // specify a negative size value (in pixels) for the height so the font mapper provides the closest match // for the character height rather than the cell height. User32.LOGFONTW logFont = new User32.LOGFONTW { lfHeight = -pixelsY, lfCharSet = font.GdiCharSet, lfOutPrecision = Gdi32.OUT_PRECIS.TT, lfQuality = quality, lfWeight = (font.Style & FontStyle.Bold) == FontStyle.Bold ? Gdi32.FW.BOLD : Gdi32.FW.NORMAL, lfItalic = (font.Style & FontStyle.Italic) == FontStyle.Italic ? True : False, lfUnderline = (font.Style & FontStyle.Underline) == FontStyle.Underline ? True : False, lfStrikeOut = (font.Style & FontStyle.Strikeout) == FontStyle.Strikeout ? True : False, FaceName = familyName }; if (logFont.FaceName.IsEmpty) { logFont.FaceName = DefaultFaceName; } Gdi32.HFONT hfont = Gdi32.CreateFontIndirectW(ref logFont); if (hfont.IsNull) { // Get the default font if we couldn't get what we requested. logFont.FaceName = DefaultFaceName; logFont.lfOutPrecision = Gdi32.OUT_PRECIS.TT_ONLY; hfont = Gdi32.CreateFontIndirectW(ref logFont); Debug.Assert(!hfont.IsNull); } return(hfont); }