// 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(this.hDC != IntPtr.Zero, "Cannot get initial state without a valid HDC"); hCurrentPen = hInitialPen = IntUnsafeNativeMethods.GetCurrentObject(new HandleRef(this, hDC), IntNativeMethods.OBJ_PEN); hCurrentBrush = hInitialBrush = IntUnsafeNativeMethods.GetCurrentObject(new HandleRef(this, hDC), IntNativeMethods.OBJ_BRUSH); hCurrentBmp = hInitialBmp = IntUnsafeNativeMethods.GetCurrentObject(new HandleRef(this, hDC), IntNativeMethods.OBJ_BITMAP); hCurrentFont = hInitialFont = IntUnsafeNativeMethods.GetCurrentObject(new HandleRef(this, hDC), IntNativeMethods.OBJ_FONT); }
/// <summary> /// Creates a WindowsFont from the font selected in the supplied dc. /// </summary> public static WindowsFont FromHdc(IntPtr hdc) { IntPtr hFont = IntUnsafeNativeMethods.GetCurrentObject(new HandleRef(null, hdc), IntNativeMethods.OBJ_FONT); // don't call DeleteObject on handle from GetCurrentObject, it is the one selected in the hdc. return(FromHfont(hFont)); }
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)); }
internal void DisposeFont(bool disposing) { if (disposing) { DeviceContexts.RemoveDeviceContext(this); } if (selectedFont != null && selectedFont.Hfont != IntPtr.Zero) { IntPtr hCurrentFont = IntUnsafeNativeMethods.GetCurrentObject(new HandleRef(this, hDC), IntNativeMethods.OBJ_FONT); if (hCurrentFont == selectedFont.Hfont) { // select initial font back in IntUnsafeNativeMethods.SelectObject(new HandleRef(this, this.Hdc), new HandleRef(null, hInitialFont)); hCurrentFont = hInitialFont; } selectedFont.Dispose(disposing); selectedFont = null; } }