I have two Extented controls.
First one is called LiveControl
which is inherited from PictureBox
which have Resizing,Rotation, Move,... Methods.
Second one is called DrawPanel
which is inherited from Panel
which is meant to be the parent for several LiveControl
s.
I did several things to reduce filckering and also leaving trails when one LiveControl
moved over another. and finally I achieved it even better than I expected with the following codes in LiveControl
:
MouseUp += (s, e) =>
{
foreach (Control c in Parent.Controls.OfType<LiveControl>())
c.Refresh();
};
MouseMove += (s, e) =>
{
if (e.Button == MouseButtons.Left)
{
Control x = (Control)s;
x.SuspendLayout();
x.Location = new Point(x.Left + e.X - cur.X, x.Top + e.Y - cur.Y);
x.ResumeLayout();
foreach (Control c in Parent.Controls.OfType<LiveControl>())
c.Update();
}
};
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
foreach (Control c in Parent.Controls.OfType<LiveControl>())
c.Invalidate();
}
But the problem is that as in the above code, each LiveControl
's OnPaint
invalidates all of the LiveControl
s, it causes infinite calls of OnPaint
which causes problems for other controls on the form (they work very slow with delays).
To solve this I edited the code for OnPaint
like this:
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (!((DrawPanel)Parent).Invalidating)
{
((DrawPanel)Parent).Invalidating = true;
((DrawPanel)Parent).InvalidatedCount = 0;
foreach (Control c in Parent.Controls.OfType<LiveControl>())
c.Invalidate();
}
else
{
((DrawPanel)Parent).InvalidatedCount++;
if (((DrawPanel)Parent).InvalidatedCount == ((DrawPanel)Parent).Controls.OfType<LiveControl>().Count())
{
((DrawPanel)Parent).InvalidatedCount = 0; ((DrawPanel)Parent).Invalidating = false;
}
}
}
But the OnPaint
method is still being called nonstop.
Does anybody has any idea how to solve this?