Refresh UserControl drawing elements

  • Thread starter Thread starter Executor
  • Start date Start date
E

Executor

Hi you all,

I have created a usercontrol with a filledEllipses on a
filledRectangle, using this Paint event:

private void myControl_Paint(object sender,
System.Windows.Forms.PaintEventArgs e)
{
SolidBrush aBrush = new SolidBrush(this.BackColor);
e.Graphics.FillRectangle(aBrush, 10,10, this.Width - 10, this.Height
- 10);
if(m_fOn)
aBrush.Color = m_cOn;
else
aBrush.Color = m_cOff;
e.Graphics.FillEllipse(aBrush, 20, 20, this.Width - 20, this.Width -
20);
aBrush.Dispose();
}

using a Timer I switch m_fOn form true to false and back like this

private void myTimer_Tick(object sender, System.EventArgs e)
{
m_fOn = ! m_fOn;
this.Refresh();
}

Now the control is completly redrown.

For testing I have set the myTimer.interval to 10, and created a
testing application.
I discovered that the memory used by the testing application is
constand increasing.
So I would like to repaint the FillEllipse only.

Any advice on this?

Thanks in advance..

Executor
 
Yes you are right because timer is constantly refreshing the control no
matter from is visible or not or form is minimized or not. I think you dont
need to refresh your control when your form is minimized or when its not
visible at all. So what you can do is if the form is visible (this.Visible =
True) and then invalidate the form.
The Invalidates method invalidates a specific region of the control and
causes a paint message to be sent to the control. It will cause the Paint
event of the control to fire and in paint event call ReDraw function of this
control.
 
Back
Top