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?
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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With