diff --git a/BizHawk.Client.EmuHawk/CustomControls/GDITextRenderer.cs b/BizHawk.Client.EmuHawk/CustomControls/GDITextRenderer.cs index dc9a250e5a..84db6dc997 100644 --- a/BizHawk.Client.EmuHawk/CustomControls/GDITextRenderer.cs +++ b/BizHawk.Client.EmuHawk/CustomControls/GDITextRenderer.cs @@ -33,7 +33,7 @@ namespace BizHawk.Client.EmuHawk.CustomControls /// /// The wrapped WinForms graphics object /// - private readonly Graphics _g; + private Graphics _g; /// /// the initialized HDC used @@ -42,28 +42,36 @@ namespace BizHawk.Client.EmuHawk.CustomControls #endregion - public GDIRenderer(System.Windows.Forms.Control c) + public class GdiGraphicsLock : IDisposable { - _c = c; - _hdc = GetDC(c.Handle); + public GdiGraphicsLock(GDIRenderer gdi) + { + 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); - _hdc = hdc; + _g = g; + _hdc = g.GetHdc(); SetBkMode(_hdc, (int)BkModes.TRANSPARENT); + return new GdiGraphicsLock(this); } - System.Windows.Forms.Control _c; - /// /// Init. /// - public GDIRenderer(Graphics g) + public GDIRenderer() { - _g = g; - _hdc = _g.GetHdc(); SetBkMode(_hdc, (int)BkModes.OPAQUE); } @@ -155,18 +163,8 @@ namespace BizHawk.Client.EmuHawk.CustomControls } } - if (_c != null) - { - ReleaseDC(_c.Handle, _hdc); - _hdc = IntPtr.Zero; - } - - if (_hdc != IntPtr.Zero) - { - SelectClipRgn(_hdc, IntPtr.Zero); - _g.ReleaseHdc(_hdc); - _hdc = IntPtr.Zero; - } + 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"); } #region Private methods diff --git a/BizHawk.Client.EmuHawk/tools/TAStudio/InputRoll.cs b/BizHawk.Client.EmuHawk/tools/TAStudio/InputRoll.cs index 6ef7d98f2f..0f9c37fe8d 100644 --- a/BizHawk.Client.EmuHawk/tools/TAStudio/InputRoll.cs +++ b/BizHawk.Client.EmuHawk/tools/TAStudio/InputRoll.cs @@ -24,14 +24,18 @@ namespace BizHawk.Client.EmuHawk CellPadding = 3; //SetStyle(ControlStyles.OptimizedDoubleBuffer, true); SetStyle(ControlStyles.AllPaintingInWmPaint, true); + SetStyle(ControlStyles.UserPaint, true); SetStyle(ControlStyles.SupportsTransparentBackColor, true); SetStyle(ControlStyles.Opaque, true); this.Font = new Font("Courier New", 8); // Only support fixed width //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; } @@ -302,22 +306,24 @@ namespace BizHawk.Client.EmuHawk } } + static int ctr; protected override void OnPaint(PaintEventArgs e) { - Gdi.NewHdc(e.Graphics.GetHdc()); - - // Header - if (Columns.Any()) + using (var LCK = Gdi.LockGraphics(e.Graphics)) { - DrawColumnBg(Gdi, e); - DrawColumnText(Gdi, e); + // Header + 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