cleanup some GDI object lifecycle stuff

This commit is contained in:
zeromus 2014-08-10 18:49:17 +00:00
parent c16a4fefc6
commit a4988cca22
2 changed files with 42 additions and 38 deletions

View File

@ -33,7 +33,7 @@ namespace BizHawk.Client.EmuHawk.CustomControls
/// <summary> /// <summary>
/// The wrapped WinForms graphics object /// The wrapped WinForms graphics object
/// </summary> /// </summary>
private readonly Graphics _g; private Graphics _g;
/// <summary> /// <summary>
/// the initialized HDC used /// the initialized HDC used
@ -42,28 +42,36 @@ namespace BizHawk.Client.EmuHawk.CustomControls
#endregion #endregion
public GDIRenderer(System.Windows.Forms.Control c) public class GdiGraphicsLock : IDisposable
{ {
_c = c; public GdiGraphicsLock(GDIRenderer gdi)
_hdc = GetDC(c.Handle); {
this.gdi = gdi;
}
public void Dispose()
{
gdi._g.ReleaseHdc(gdi._hdc);
gdi._hdc = IntPtr.Zero;
gdi._g = null;
}
GDIRenderer gdi;
} }
public void NewHdc(IntPtr hdc) public GdiGraphicsLock LockGraphics(Graphics g)
{ {
ReleaseDC(_c.Handle, _hdc); _g = g;
_hdc = hdc; _hdc = g.GetHdc();
SetBkMode(_hdc, (int)BkModes.TRANSPARENT); SetBkMode(_hdc, (int)BkModes.TRANSPARENT);
return new GdiGraphicsLock(this);
} }
System.Windows.Forms.Control _c;
/// <summary> /// <summary>
/// Init. /// Init.
/// </summary> /// </summary>
public GDIRenderer(Graphics g) public GDIRenderer()
{ {
_g = g;
_hdc = _g.GetHdc();
SetBkMode(_hdc, (int)BkModes.OPAQUE); SetBkMode(_hdc, (int)BkModes.OPAQUE);
} }
@ -155,18 +163,8 @@ namespace BizHawk.Client.EmuHawk.CustomControls
} }
} }
if (_c != null) System.Diagnostics.Debug.Assert(_hdc == IntPtr.Zero, "Disposed a GDIRenderer while it held an HDC");
{ System.Diagnostics.Debug.Assert(_g == null, "Disposed a GDIRenderer while it held a Graphics");
ReleaseDC(_c.Handle, _hdc);
_hdc = IntPtr.Zero;
}
if (_hdc != IntPtr.Zero)
{
SelectClipRgn(_hdc, IntPtr.Zero);
_g.ReleaseHdc(_hdc);
_hdc = IntPtr.Zero;
}
} }
#region Private methods #region Private methods

View File

@ -24,14 +24,18 @@ namespace BizHawk.Client.EmuHawk
CellPadding = 3; CellPadding = 3;
//SetStyle(ControlStyles.OptimizedDoubleBuffer, true); //SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true); SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.SupportsTransparentBackColor, true); SetStyle(ControlStyles.SupportsTransparentBackColor, true);
SetStyle(ControlStyles.Opaque, true); SetStyle(ControlStyles.Opaque, true);
this.Font = new Font("Courier New", 8); // Only support fixed width this.Font = new Font("Courier New", 8); // Only support fixed width
//BackColor = Color.Transparent; //BackColor = Color.Transparent;
Gdi = new GDIRenderer(this); Gdi = new GDIRenderer();
using (var g = CreateGraphics())
using(var LCK = Gdi.LockGraphics(g))
_charSize = Gdi.MeasureString("A", this.Font);
_charSize = Gdi.MeasureString("A", this.Font);
CurrentCell = null; CurrentCell = null;
} }
@ -302,22 +306,24 @@ namespace BizHawk.Client.EmuHawk
} }
} }
static int ctr;
protected override void OnPaint(PaintEventArgs e) protected override void OnPaint(PaintEventArgs e)
{ {
Gdi.NewHdc(e.Graphics.GetHdc()); using (var LCK = Gdi.LockGraphics(e.Graphics))
// Header
if (Columns.Any())
{ {
DrawColumnBg(Gdi, e); // Header
DrawColumnText(Gdi, e); if (Columns.Any())
{
DrawColumnBg(Gdi, e);
DrawColumnText(Gdi, e);
}
// Background
DrawBg(Gdi, e);
// ForeGround
DrawData(Gdi, e);
} }
// Background
DrawBg(Gdi, e);
// ForeGround
DrawData(Gdi, e);
} }
#endregion #endregion