Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I stop a System::Windows::Forms::UserControl from erasing it's background?

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?

like image 305
Jon Cage Avatar asked Nov 22 '25 21:11

Jon Cage


2 Answers

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?

like image 73
Greg D Avatar answered Nov 25 '25 15:11

Greg D


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.

like image 23
ChrisW Avatar answered Nov 25 '25 17:11

ChrisW