views:

377

answers:

3

I have a C++/CLI System::Windows::Forms::UserControl derived control which should only redraw (a small portion of) itself as new data is fed into it. For some reason though, the OnPaint mechanism is being called even when there's nothing to cause it external to the app.

Here's a snippet:

void Spectrogram::OnPaint(System::Windows::Forms::PaintEventArgs ^e)
{
 // Overidden to stop the background being painted(?)
}

void Spectrogram::AddNewFFTData( float* data, int fataWidth )
{
 Graphics^ gfx = CreateGraphics();

 //now do some drawing

 gfx->Dispose();
}

So I call the add data method to add some new data which should in theory write over the previous entry (which clears some highlighting) and write the new entry.

Back in the day I used to develop MFC/OpenGL apps and one of the first things I'd do would be to override the OnEraseBackground method. As far as I can see though, there's no obvious way of stopping it being erased. What have I missed?

+2  A: 

Maybe it's a statement like this in the Form's constructor:

//do own background painting
this.SetStyle(ControlStyles.Opaque, true);

I think that prevents OnPaintBackground being invoked at all: so you don't need to override OnPaintBackground, and instead you can erase the background (or not) yourself in your OnPaint.

ChrisW
+1: I just tested and you're right, setting that stops the background being erased - I wish I could accept both answers!
Jon Cage
+1: Cool, didn't know about that one. :)
Greg D
+2  A: 

You may be looking for Control.OnPaintBackground(). I've had to override that to do nothing for a custom control I wrote to bring a legacy MFC control into a Winforms project. Otherwise it would paint the background on top of the MFC control's paint job.

Essentially, in the .cpp:

void MyControl::OnPaintBackground(System::Windows::Forms::PaintEventArgs ^pevent)
{
    // Do nothing, we don't want to paint over the native control.
    // You may want to do something a little fancier for DesignMode
    // if you use the winforms designer, though.
}

On the prototype:

protected: 
    void virtual OnPaintBackground(System::Windows::Forms::PaintEventArgs ^pevent) override;

What rectangle is being passed in to you via the event args? Is the entire control being invalidated, or just a portion of it?

Greg D
+1: That's excellent - exactly what I was looking for.
Jon Cage
One more thing to remember though; make sure you've disabled double-buffering or the buffers will occasionally swap and erase everything.
Jon Cage
Oh, and it was the entire control being invalidated.
Jon Cage
Awesome, I'm glad that helped. :) I was just wondering what was being invalidated because that can be a clue as to what was causing the invalidation.
Greg D
A: 

I did some stuff with the OnPaint lately (C#, if that matters), and I noticed it literally is drawn when a area of the control is revealed.

A better solution is to draw on a cached Bitmap, and draw it to the control every time dotNet asks for it.

Dykam