views:

604

answers:

2

So, I am pretty unfamiliar with windows forms development.

I'm trying to create a "hey I'm busy doing stuff" component that just spins a shape around. I want this control to be transient and draw on top of any other controls. The class inherits 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 draw on top of other controls.

Now my problem is this. I repaint the control a few times a second to give the appearane of a smooth animation. However, I can't figure out how to clear what was drawn in the previous frame. Using e.Graphics.Clear(Color.Transparent) in OnPaint turns the whole control black.

Is there a way to just clear the drawn contents of a control?

I've noticed that Resizing the the control will clear the background.

Things that Don't Work

  1. Overriding OnPaintBackground to do nothing. Or just calling base.OnPaintBackground. Same results.
+2  A: 

You may have to override OnPaintBackground that this article presents: http://saftsack.fs.uni-bayreuth.de/~dun3/archives/creating-a-transparent-panel-in-net/108.html

You may also need to Invalidate the control when it needs to be cleared to force OnPaintBackground to be called.

popester
That doesn't correct the problem. And I was invalidating the control before. Thanks though.
snicker
But I did find the link for the real solution in the comments of that page, so +1 and thank you.
snicker
+2  A: 

Okay, I found the solution here: http://www.bobpowell.net/transcontrols.htm

The Parent controls actually must be invalidated in order to retain the transparent background.

snicker
You don't have to invalidate the entire parent control, but just the area corresponding to the user control: this.Parent.Invalidate(this.ClientRectangle, true);
Eric J.
@Eric: Thanks for the tip. I think I may have done just that!
snicker