我正在尝试创建一个程序,当您单击鼠标时它会创建多个圆圈。问题是每次我点击另一个地方时它们都会消失(移除)。 我创建了一个名为 Ellipse 的类,其中保存了我的圈子。然后我把所有的都放在一个叫做椭圆的堆栈中。 我看过不同的解决方案,但仍然删除了旧的省略号。 我想知道我做错了什么。
这是我的代码。
public partial class Form1 : Form
{
int x,y;
Queue<MyEllipce> ellipses = new Queue<MyEllipce>();
MyEllipce ellipse;
public Form1()
{
InitializeComponent();
this.MouseClick += new MouseEventHandler(Form1_MouseClick);
}
private void Form1_Load(object sender, EventArgs e)
{
}
void Form1_MouseClick(object sender, MouseEventArgs e)
{
x = e.X;
y = e.Y;
ellipse = new MyEllipce(x, y);
ellipses.Enqueue(ellipse);
Invalidate();
}
private void btnTaBort_Click(object sender, EventArgs e)
{
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
foreach (MyEllipce item in ellipses)
{
Graphics g = e.Graphics;
MyEllipce ellipse= ellipses.Peek();
ellipse.Draw(g, x, y);
}
}
}
class MyEllipce
{
int x = 0;
int y = 0;
int diameter = 5;
//Constructor
public MyEllipce(int x, int y)
{
this.x = x;
this.y = y;
}
public void Draw(Graphics g, int x, int y)
{
SolidBrush brush = new SolidBrush(Color.Red);
g.FillEllipse(brush,x,y,diameter,diameter);
}
}
最佳答案
改变这个
ellipse.Draw(g, x, y);
为此
item.Draw(g, x, y);
并删除“peek”行。
关于c# - 在 Form 上画多个圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43835159/