{
private const int WM_ERASEBKGND = 0x0014;
private Control _control;
private SolidBrush _brush;
private void OnHandleDestroyed(object sender, EventArgs e)
{
Detach();
}
public BackColorWorkaroundSubclass(Control control)
{
Attach(control);
}
public void Attach(Control control)
{
this._control = control;
this._brush = new SolidBrush(this._control.BackColor);
AssignHandle(this._control.Handle);
this._control.HandleDestroyed += new EventHandler(OnHandleDestroyed);
}
public void Detach()
{
ReleaseHandle();
this._brush = null;
}
protected override void WndProc(ref Message message)
{
if (message.Msg == WM_ERASEBKGND)
{
using (Graphics graphics = Graphics.FromHdc(message.WParam))
{
graphics.FillRectangle(this._brush, this._control.ClientRectangle);
}
}
else
{
base.WndProc(ref message);
}
}
}