How to clear graphics from a transparent control in windows

So, I'm pretty new to developing Windows forms.

I am trying to create a component β€œhey, I'm busy with business”, which simply rotates the form around itself. I want this control to be temporary and run on top of any other controls. The class is inherited directly from Control.

So, I have this in the constructor:

SetStyle(ControlStyles.Opaque, true); 

and this:

 protected override CreateParams CreateParams { get { CreateParams parms = base.CreateParams; parms.ExStyle |= 0x20; return parms; } } 

Which gets me a control that will be drawn on top of other controls.

Now my problem is this. I redraw the control several times per second to create a smooth animation. However, I cannot figure out how to clear what was drawn in the previous frame. Using e.Graphics.Clear(Color.Transparent) in OnPaint turns the entire black control.

Is there a way to clear the drawn content of a control?

I noticed that resizing the control will clear the background.

Things that don't work

  • Override OnPaintBackground to do nothing. Or just name base.OnPaintBackground. The same results.
+6
c # winforms transparent gdi +
source share
2 answers

Ok, I found a solution here: http://www.bobpowell.net/transcontrols.htm

Parent elements must actually be invalidated to maintain a transparent background.

+4
source share

You may need to redefine OnPaintBackground, which is presented in this article: http://saftsack.fs.uni-bayreuth.de/~dun3/archives/creating-a-transparent-panel-in-net/108.html

You may also need an Invalidate control when you need to clear it to force the OnPaintBackground to be called.

+2
source share

All Articles