/// <summary> /// Creates a WindowsFont from the font selected in the supplied dc. /// </summary> public static WindowsFont FromHdc(IntPtr hdc) { IntPtr hFont = Gdi32.GetCurrentObject(hdc, Gdi32.ObjectType.OBJ_FONT); // don't call DeleteObject on handle from GetCurrentObject, it is the one selected in the hdc. return(FromHfont(hFont)); }
// Due to a problem with calling DeleteObject() on currently selected GDI objects, // we now track the initial set of objects when a DeviceContext is created. Then, // we also track which objects are currently selected in the DeviceContext. When // a currently selected object is disposed, it is first replaced in the DC and then // deleted. private void CacheInitialState() { _hCurrentPen = _hInitialPen = Gdi32.GetCurrentObject(new HandleRef(this, _hDC), Gdi32.ObjectType.OBJ_PEN); _hCurrentBrush = _hInitialBrush = Gdi32.GetCurrentObject(new HandleRef(this, _hDC), Gdi32.ObjectType.OBJ_BRUSH); _hCurrentBmp = _hInitialBmp = Gdi32.GetCurrentObject(new HandleRef(this, _hDC), Gdi32.ObjectType.OBJ_BITMAP); _hCurrentFont = _hInitialFont = Gdi32.GetCurrentObject(new HandleRef(this, _hDC), Gdi32.ObjectType.OBJ_FONT); }
// Not all state is handled yet. Backfilling in as we write specific tests. Of special note is that we don't // have tracking for Save/RestoreDC yet. /// <summary> /// Initialize the current state of <paramref name="hdc"/>. /// </summary> public DeviceContextState(Gdi32.HDC hdc) { MapMode = Gdi32.GetMapMode(hdc); BackColor = Gdi32.GetBkColor(hdc); TextColor = Gdi32.GetTextColor(hdc); Rop2Mode = Gdi32.GetROP2(hdc); TextAlign = Gdi32.GetTextAlign(hdc); BackgroundMode = Gdi32.GetBkMode(hdc); Point point = default; Gdi32.GetBrushOrgEx(hdc, ref point); BrushOrigin = point; var hfont = Gdi32.GetCurrentObject(hdc, Gdi32.OBJ.FONT); Gdi32.GetObjectW(hfont, out User32.LOGFONTW logfont); SelectedFont = logfont; var hpen = Gdi32.GetCurrentObject(hdc, Gdi32.OBJ.PEN); Gdi32.GetObjectW(hpen, out Gdi32.LOGPEN logpen); SelectedPen = logpen; var hbrush = Gdi32.GetCurrentObject(hdc, Gdi32.OBJ.BRUSH); Gdi32.GetObjectW(hbrush, out Gdi32.LOGBRUSH logbrush); SelectedBrush = logbrush; }
// Due to a problem with calling DeleteObject() on currently selected GDI objects, // we now track the initial set of objects when a DeviceContext is created. Then, // we also track which objects are currently selected in the DeviceContext. When // a currently selected object is disposed, it is first replaced in the DC and then // deleted. private void CacheInitialState() { Debug.Assert(_hDC != IntPtr.Zero, "Cannot get initial state without a valid HDC"); _hCurrentPen = _hInitialPen = Gdi32.GetCurrentObject(new HandleRef(this, _hDC), Gdi32.ObjectType.OBJ_PEN); _hCurrentBrush = _hInitialBrush = Gdi32.GetCurrentObject(new HandleRef(this, _hDC), Gdi32.ObjectType.OBJ_BRUSH); _hCurrentBmp = _hInitialBmp = Gdi32.GetCurrentObject(new HandleRef(this, _hDC), Gdi32.ObjectType.OBJ_BITMAP); _hCurrentFont = _hInitialFont = Gdi32.GetCurrentObject(new HandleRef(this, _hDC), Gdi32.ObjectType.OBJ_FONT); }
public static FontCache.Scope GetHFONT(Font?font, Gdi32.QUALITY quality, Gdi32.HDC hdc) { if (font != null) { return(GetHFONT(font, quality)); } // Font is null, build off of the specified HDC's current font. Gdi32.HFONT hfont = (Gdi32.HFONT)Gdi32.GetCurrentObject(hdc, Gdi32.OBJ.FONT); return(new FontCache.Scope(hfont)); }
internal void DisposeFont(bool disposing) { if (disposing) { DeviceContexts.RemoveDeviceContext(this); } if (selectedFont != null && selectedFont.Hfont != IntPtr.Zero) { IntPtr hCurrentFont = Gdi32.GetCurrentObject(new HandleRef(this, hDC), Gdi32.ObjectType.OBJ_FONT); if (hCurrentFont == selectedFont.Hfont) { // select initial font back in Gdi32.SelectObject(new HandleRef(this, Hdc), hInitialFont); hCurrentFont = hInitialFont; } selectedFont.Dispose(disposing); selectedFont = null; } }