我有一个长期使用过程中的自定义控件。 这种控制已经旋转点周围的圈子。 要做到这一点,我使用的计时器,正在设计时间和运行时间。 当添加一个控制,使得形成是没有问题的。 但是他们两个相加形成,设计师减慢这么多。 为什么会出现这个问题,怎么解决呢?
从这个项目我的代码:
public class SpinningCircles : Control
{
bool fullTransparency = true;
int increment = 1;
int radius = 4;
int n = 8;
int next = 0;
int k = 0;
Timer timer;
public SpinningCircles()
{
timer = new Timer();
timer.Tick += timer_Tick;
timer.Enabled = true;
SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.UserPaint | ControlStyles.SupportsTransparentBackColor, true);
BackColor = Color.Transparent;
}
void timer_Tick(object sender, EventArgs e)
{
Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
{
if (fullTransparency)
{
Transparencer.MakeTransparent(this, e.Graphics);
}
e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
int length = Math.Min(Width, Height);
PointF center = new PointF(length / 2, length / 2);
int bigRadius = length / 2 - radius - (n - 1) * increment;
float unitAngle = 360 / n;
next++;
next = next >= n ? 0 : next;
int a = 0;
for (int i = next; i < next + n; i++)
{
int factor = i % n;
float c1X = center.X + (float)(bigRadius * Math.Cos(unitAngle * factor * Math.PI / 180));
float c1Y = center.Y + (float)(bigRadius * Math.Sin(unitAngle * factor * Math.PI / 180));
int currRad = radius + a * increment;
PointF c1 = new PointF(c1X - currRad, c1Y - currRad);
e.Graphics.FillEllipse(Brushes.Black, c1.X, c1.Y, 2 * currRad, 2 * currRad);
using (Pen pen = new Pen(Color.White, 2))
e.Graphics.DrawEllipse(pen, c1.X, c1.Y, 2 * currRad, 2 * currRad);
a++;
}
}
protected override void OnVisibleChanged(EventArgs e)
{
timer.Enabled = Visible;
base.OnVisibleChanged(e);
}
public bool FullTransparent
{
get
{
return fullTransparency;
}
set
{
fullTransparency = value;
}
}
}
public class Transparencer
{
public static void MakeTransparent(Control cont, Graphics g)
{
if (cont.Parent != null)
{
Bitmap behind = new Bitmap(cont.Parent.Width, cont.Parent.Height);
foreach (Control c in cont.Parent.Controls)
if (c.Bounds.IntersectsWith(c.Bounds) & c != cont)
c.DrawToBitmap(behind, c.Bounds);
g.DrawImage(behind, -cont.Left, -cont.Top);
behind.Dispose();
}
}
}