// this DC is cached and should only be deleted on Dispose or when the size changes. public Gdi32.HDC GetCachedItemDC(Gdi32.HDC toolStripHDC, Size bitmapSize) { if (_cachedHDCSize.Width < bitmapSize.Width || _cachedHDCSize.Height < bitmapSize.Height) { if (_cachedItemHDC.IsNull) { // Create a new DC - we don't have one yet. _cachedItemHDC = Gdi32.CreateCompatibleDC(toolStripHDC); } // Create compatible bitmap with the correct size. _cachedItemBitmap = Gdi32.CreateCompatibleBitmap(toolStripHDC, bitmapSize.Width, bitmapSize.Height); Gdi32.HGDIOBJ oldBitmap = Gdi32.SelectObject(_cachedItemHDC, _cachedItemBitmap); // Delete the old bitmap if (!oldBitmap.IsNull) { Gdi32.DeleteObject(oldBitmap); } // remember what size we created. _cachedHDCSize = bitmapSize; } return(_cachedItemHDC); }
/// <summary> /// Restores the device context to the specified state. The DC is restored by popping state information off a /// stack created by earlier calls to the SaveHdc function. /// The stack can contain the state information for several instances of the DC. If the state specified by the /// specified parameter is not at the top of the stack, RestoreDC deletes all state information between the top /// of the stack and the specified instance. /// Specifies the saved state to be restored. If this parameter is positive, nSavedDC represents a specific /// instance of the state to be restored. If this parameter is negative, nSavedDC represents an instance relative /// to the current state. For example, -1 restores the most recently saved state. /// See MSDN for more info. /// </summary> public void RestoreHdc() { // Note: Don't use the Hdc property here, it would force handle creation. Gdi32.RestoreDC(_hDC, -1); Debug.Assert(_contextStack != null, "Someone is calling RestoreHdc() before SaveHdc()"); if (_contextStack != null) { GraphicsState g = _contextStack.Pop(); _hCurrentBmp = g.hBitmap; _hCurrentBrush = g.hBrush; _hCurrentPen = g.hPen; _hCurrentFont = g.hFont; if (g.font != null && g.font.IsAlive) { ActiveFont = g.font.Target as WindowsFont; } else { WindowsFont?previousFont = ActiveFont; ActiveFont = null; if (previousFont != null && MeasurementDCInfo.IsMeasurementDC(this)) { previousFont.Dispose(); } } } // in this case, GDI will copy back the previously saved font into the DC. // we dont actually know what the font is in our measurement DC so // we need to clear it off. MeasurementDCInfo.ResetIfIsMeasurementDC(_hDC); }
// 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(this, Gdi32.ObjectType.OBJ_PEN); _hCurrentBrush = _hInitialBrush = Gdi32.GetCurrentObject(this, Gdi32.ObjectType.OBJ_BRUSH); _hCurrentBmp = _hInitialBmp = Gdi32.GetCurrentObject(this, Gdi32.ObjectType.OBJ_BITMAP); _hCurrentFont = _hInitialFont = Gdi32.GetCurrentObject(this, Gdi32.ObjectType.OBJ_FONT); }
public void GetStockBrushes(int id, uint color, uint brushStyle) { Gdi32.HGDIOBJ hgdiobj = Gdi32.GetStockObject((Gdi32.StockObject)id); Assert.False(hgdiobj.IsNull); Gdi32.GetObjectW(hgdiobj, out Gdi32.LOGBRUSH logBrush); Assert.Equal(color, logBrush.lbColor); Assert.Equal((Gdi32.BS)brushStyle, logBrush.lbStyle); }
private void FillRectDither(Gdi32.HDC dc, RECT rc) { Gdi32.HGDIOBJ hbrushOld = Gdi32.SelectObject(dc, _hbrushDither); if (!hbrushOld.IsNull) { int oldTextColor = Gdi32.SetTextColor(dc, ColorTranslator.ToWin32(SystemColors.ControlLightLight)); int oldBackColor = Gdi32.SetBkColor(dc, ColorTranslator.ToWin32(SystemColors.Control)); Gdi32.PatBlt(dc, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, Gdi32.ROP.PATCOPY); Gdi32.SetTextColor(dc, oldTextColor); Gdi32.SetBkColor(dc, oldBackColor); } }
public void DeleteObject(Gdi32.HGDIOBJ handle, GdiObjectType type) { Gdi32.HGDIOBJ handleToDelete = default; switch (type) { case GdiObjectType.Pen: if (handle == _hCurrentPen) { Gdi32.HGDIOBJ currentPen = Gdi32.SelectObject(this, _hInitialPen); Debug.Assert(currentPen == _hCurrentPen, "DeviceContext thinks a different pen is selected than the HDC"); _hCurrentPen = default; } handleToDelete = handle; break; case GdiObjectType.Brush: if (handle == _hCurrentBrush) { Gdi32.HGDIOBJ currentBrush = Gdi32.SelectObject(this, _hInitialBrush); Debug.Assert(currentBrush == _hCurrentBrush, "DeviceContext thinks a different brush is selected than the HDC"); _hCurrentBrush = default; } handleToDelete = handle; break; case GdiObjectType.Bitmap: if (handle == _hCurrentBmp) { Gdi32.HGDIOBJ currentBmp = Gdi32.SelectObject(this, _hInitialBmp); Debug.Assert(currentBmp == _hCurrentBmp, "DeviceContext thinks a different brush is selected than the HDC"); _hCurrentBmp = default; } handleToDelete = handle; break; } Gdi32.DeleteObject(handleToDelete); }