New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
API Proposal: Add IDeviceContext
to events that hold Graphics
#3570
Comments
How the smuggling works: namespace System.Windows.Forms
{
public class PaintEventArgs : EventArgs, IDeviceContext, IInternalInterface {}
public class SomeControl : Control
{
protected override void OnPaint(PaintEventArgs e)
{
RendererHelper.DrawThing(e);
}
}
internal static class RendererHelper
{
internal static void DrawThing(IDeviceContext context)
{
if (context is IInternalInterface internals) ...
}
}
} |
namespace System.Windows.Forms
{
public partial class PaintEventArgs : IDisposable, IDeviceContext
{
}
public partial class DrawItemEventArgs : IDisposable, IDeviceContext
{
}
public partial class DataGridViewRowPostPaintEventArgs : IDisposable, IDeviceContext
{
}
public partial class DrawListViewItemEventArgs : IDisposable, IDeviceContext
{
}
public partial class DrawToolTipEventArgs : IDisposable, IDeviceContext
{
}
public partial class DrawTreeNodeEventArgs : IDisposable, IDeviceContext
{
}
public partial class ToolStripArrowRenderEventArgs : IDisposable, IDeviceContext
{
}
public partial class ToolStripContentPanelRenderEventArgs : IDisposable, IDeviceContext
{
}
public partial class ToolStripItemRenderEventArgs : IDisposable, IDeviceContext
{
}
public partial class ToolStripPanelRenderEventArgs : IDisposable, IDeviceContext
{
}
public partial class ToolStripRenderEventArgs : IDisposable, IDeviceContext
{
}
} |
Even if you make it public instead of explicit it still should not dispose the contained |
Yes, and I comment on that in the original description. We never dispose passed in |
Resolved in #3553 |
Proposal
Add
IDeviceContext
to all Windows FormsEventArgs
that wrap aSystem.Drawing.Graphics
.PaintEventArgs
DrawItemEventArgs
DataGridViewRowPostPaintEventArgs
DrawListViewItemEventArgs
DrawToolTipEventArgs
DrawTreeNodeEventArgs
ToolStripArrowRenderEventArgs
ToolStripContentPanelRenderEventArgs
ToolStripItemRenderEventArgs
ToolStripPanelRenderEventArgs
ToolStripRenderEventArgs
Justification
Windows Forms was designed around GDI+, which
System.Drawing
is a wrapper around.Graphics
was the primary abstraction. Performance of GDI+ wasn't sufficient and internally a number of code paths were changed to utilize the GDIHDC
.Graphics
exposes theHDC
via theIDeviceContext
interface, which WinForms uses internally and also exposes on public APIs (seeVisualStyleRenderer
andTextRenderer
).Exposing
IDeviceContext
allows us to stop unwrapping event args and allows us to extract theHDC
with internal optimizations:Graphics
object if we haven't already.IDeviceContext
APIs without retrievingGraphics
, which can createGraphics
or force us to apply it's (likely unchanged) state.Implementation Details
We'll explicitly implement
IDeviceContext
to avoid having the methods directly visible on the event args. The only real benefit is treating the event args as anIDeviceContext
object.On event args where we don't already have
IDisposable
we'll explicitly defineIDisposable.Dispose()
and we will not dispose passed in arguments (notablyGraphics
).The text was updated successfully, but these errors were encountered: