C#将绘制的椭圆的位置保存在List <point>中吗?

时间:2018-12-04 15:26:20

标签: c# list point onpaint

这里有个小问题,我找不到合适的答案。 我想保存一个椭圆的绘制位置,以便以后可以在两个椭圆之间绘制一条线,因此,如果可能的话,我想将其保存为点。

 protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        screen.Clear(Color.Black);

        using (var p = new Pen(Color.White, 2))
        {
            for (int x = 0; x < p1List.Count; x++)
            {
                screen.DrawLine(p, p1List[x], p2List[x]);
            }
        }

        List<Point> punten = new List<Point>();


        foreach (var pair in this.droppedShapes)
        {
            var shapeType = pair.Item2; // Reveal your own shape object here
            var location = pair.Item1;


            switch (shapeType) // Reveal your own shape object here
            {
                case 0:
                    screen.DrawRectangle(whitePen, location.X - 10, location.Y - 10, 40, 40);
                    screen.DrawString("&", new Font(FontFamily.GenericMonospace, (float)28), whiteBrush, location.X - 11, location.Y - 13);
                    //input       
                    screen.DrawLine(whitePen, location.X - 25, location.Y, location.X - 10, location.Y );
                    screen.FillEllipse(greenBrush, location.X - 30, location.Y - 5, 10, 10);
                    punten.Add(); //add the location of the "ellipse"
                    //input       
                    screen.DrawLine(whitePen, location.X - 25, location.Y + 20, location.X - 10, location.Y + 20);
                    screen.FillEllipse(greenBrush, location.X - 30, location.Y + 15, 10, 10);

                    //output      
                    screen.DrawLine(whitePen, location.X + 30, location.Y + 10, location.X + 45, location.Y + 10);
                    screen.FillEllipse(greenBrush, location.X + 40, location.Y + 5, 10, 10);

我已经添加了“ punten.Add()”行,因为这是我要保存位置的行。 如果有更好的方法,请打我!

1 个答案:

答案 0 :(得分:1)

在我看来,只需替换您的

punten.Add();

使用

punten.Add(new Point(location.X, location.Y));

或在位置之间绘制线条的类似方法。 请注意,如果在switch语句中遇到case 0,则只会将点添加到列表中,否则列表中可能不包含任何点(或者在其他case部分中将点添加到列表中)。