/// <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; }
public static WindowsBrush FromDC(DeviceContext dc) { IntPtr hBrush = IntUnsafeNativeMethods.GetCurrentObject(new HandleRef(null, dc.Hdc), IntNativeMethods.OBJ_BRUSH); IntNativeMethods.LOGBRUSH logBrush = new IntNativeMethods.LOGBRUSH(); IntUnsafeNativeMethods.GetObject(new HandleRef(null, hBrush), logBrush); // don't call DeleteObject on handle from GetCurrentObject, it is the one selected in the hdc. return(WindowsBrush.FromLogBrush(dc, logBrush)); }
/// <devdoc> /// Creates a WindowsFont from the handle to a native GDI font and optionally takes ownership of managing /// the lifetime of the handle. /// </devdoc> public static WindowsFont FromHfont(IntPtr hFont, bool takeOwnership) { IntNativeMethods.LOGFONT lf = new IntNativeMethods.LOGFONT(); IntUnsafeNativeMethods.GetObject(new HandleRef(null, hFont), lf); WindowsFont wf = new WindowsFont(lf, /*createHandle*/ false); wf.hFont = hFont; wf.ownHandle = takeOwnership; // if true, hFont will be deleted on dispose. return(wf); }