ボタン付きのツールストリップメニューを備えたwinformsのプログラムがあります:+、-。+をクリックするたびに楕円が大きくなり、-をクリックすると消えます。今、私は成長する楕円を描くことができましたが、もう一度 + をクリックすると新しい楕円が描かれます。前に描いた楕円の内側で新しい楕円を開始する必要があります。たとえば、3回クリックすると、3つの楕円が内側に表示されます。- をクリックすると、最後に描いた楕円が消えます。draImage() を使用する制約があります。私の質問は、+ をもう一度クリックしたときに、前の楕円の中に新しい楕円を作成するにはどうすればよいですか? +をクリックすると、前のものを消去して新しい楕円の描画を開始します。私のコードは次のようになります。
Form.cs :
public partial class TheBalls : Form
{
private Ball yourBall;
public TheBalls()
{
InitializeComponent();
DoubleBuffered = true;
yourBall = new Ball(this);
}
Graphics flagGraphics;
List<Ball> list = new List<Ball> { };
private void plusButton_MouseDown(object sender, MouseEventArgs e)
{
yourBall = new Ball(this);
list.Add(yourBall);
yourBall.Growing();
}
private void minusButton_MouseDown(object sender, MouseEventArgs e)
{
if (list.Count > 0)
{
list[list.Count - 1].clr = Color.Transparent;
list.RemoveAt(list.Count -1);
}
}
private void TheBalls_Paint(object sender, PaintEventArgs e)
{
Bitmap bitmap = new Bitmap(yourBall.Center.X * 2, yourBall.Center.Y * 2);
flagGraphics = Graphics.FromImage(bitmap);
yourBall.Update(flagGraphics);
e.Graphics.DrawImage(bitmap, 0, 0);
}
}
ボールクラス: (楕円)
class Ball
{
private Control canvas;
private Timer t = new Timer();
private int dir = 1;
public Point Center { get; set; }
public Color clr;
public int Radius { get; set; }
public Control Canvas
{
get { return canvas; }
set
{
canvas = value;
if (canvas != null)
{
//canvas.SizeChanged -= Canvas_SizeChanged;
//canvas.SizeChanged += Canvas_SizeChanged;
Center = new Point(canvas.ClientSize.Width / 2, canvas.ClientSize.Height/ 2);
}
}
}
private static readonly Random rand = new Random();
public Ball(Control canvas)
{
clr = Color.FromArgb(rand.Next(255), rand.Next(255), rand.Next(255));
if (canvas != null)
{
this.canvas = canvas;
canvas.SizeChanged -= Canvas_SizeChanged;
canvas.SizeChanged += Canvas_SizeChanged;
Center = new Point(canvas.ClientSize.Width / 2, canvas.ClientSize.Height / 2);
}
t.Interval = 10;
t.Tick += timer_Tick;
}
private void Canvas_SizeChanged(object sender, EventArgs e)
{
Center = new Point(canvas.ClientSize.Width / 2, canvas.ClientSize.Height / 2);
}
public void Growing()
{
dir = 1;
t.Enabled = true;
}
public void timer_Tick(object sender, EventArgs e)
{
if (canvas == null)
{
//t.Stop();
clr = Color.Transparent;
return;
}
Radius += dir;
if (Radius > Math.Min(canvas.ClientSize.Width, canvas.ClientSize.Height) / 2)
{
Radius = Math.Min(canvas.ClientSize.Width, canvas.ClientSize.Height) / 2;
clr = Color.Transparent;
}
canvas.Invalidate();
}
public void Update(Graphics g)
{
g.FillEllipse(new SolidBrush(clr), new Rectangle(Center.X - Radius, Center.Y - Radius, Radius * 2, Radius * 2));
}
}