c # Рисование линий между 2 кнопками - PullRequest
0 голосов
/ 02 декабря 2018

Я надеюсь, что вы можете мне помочь, и это большой код, поэтому я постараюсь объяснить это кратко.Я делаю программу, которая может размещать логические вентили и которая может соединяться друг с другом, я использую метод OnPaint(PaintEventArgs e), и у меня возникают проблемы с соединением вентилей линией.(код также действительно хаотичен) Я добавляю свои ворота в личный список List<Tuple<Point, int>> droppedShapes = new List<Tuple<Point, int>>(); И свои строки в приватный List<Tuple<Point, Point>> allLines = new List<Tuple<Point, Point>>(); Так что я могу сохранить координаты xy.Я использую кнопки для соединения своих линий, поэтому на каждом конце соединения у меня есть кнопка, которую я могу нажать.Но я могу подключить только 2 входа одного компонента друг к другу.Вот мой код, надеюсь, вы мне поможете!

Здесь вы можете увидеть, как я рисую свои компоненты и кнопки.

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

        Pen whitePen = new Pen(Color.White);
        SolidBrush tealBrush = new SolidBrush(Color.Teal);
        SolidBrush whiteBrush = new SolidBrush(Color.White);

        SolidBrush yellowBrush = new SolidBrush(Color.Yellow);

        SolidBrush grayBrush = new SolidBrush(Color.Gray);
        SolidBrush redBrush = new SolidBrush(Color.Red);


        foreach (var line in allLines)
        {
            screen.DrawLine(whitePen, line.Item1, line.Item2);
        }

        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, 20, 20);
                    screen.DrawString("&", new Font(FontFamily.GenericMonospace, (float)18), whiteBrush, location.X - 11, location.Y - 13);
                    screen.DrawLine(whitePen, location.X - 20, location.Y + 6, location.X - 10, location.Y + 6);
                    screen.DrawLine(whitePen, location.X - 20, location.Y - 6, location.X - 10, location.Y - 6);
                    screen.DrawLine(whitePen, location.X + 20, location.Y, location.X + 10, location.Y);

                    newB = new Point(location.X - 25, location.Y + 2);
                    newB2 = new Point(location.X - 25, location.Y - 10);
                    newB3 = new Point(location.X + 20, location.Y - 3);
                    button1 = new Button();
                    button2 = new Button();
                    button3 = new Button();
                    button1.Size = new Size(8, 8);
                    button2.Size = new Size(8, 8);
                    button3.Size = new Size(8, 8);
                    newB.Offset(0, 0);
                    newB2.Offset(0, 0);
                    newB3.Offset(0, 0);
                    button1.Location = newB;
                    button2.Location = newB2;
                    button3.Location = newB3;
                    button1.Click += Button_Click1;
                    button2.Click += Button_Click2;
                    button3.Click += Button_Click3;
                    drawPanel.Controls.Add(button1);
                    drawPanel.Controls.Add(button2);
                    drawPanel.Controls.Add(button3);


                    if (jaOfNee == true)
                    {
                       screen.DrawString(pair.Item1.ToString(), new Font(FontFamily.GenericMonospace, (float) 6), whiteBrush, location.X - 25, location.Y - 25);
                    }
                    break;


                case 1:
                    screen.DrawRectangle(whitePen, location.X - 10, location.Y - 10, 20, 20);
                    screen.DrawString("1", new Font(FontFamily.GenericMonospace, (float)10), whiteBrush, location.X, location.Y - 9);
                    screen.DrawString(">", new Font(FontFamily.GenericMonospace, (float)8), whiteBrush, location.X - 8, location.Y - 10);
                    screen.DrawLine(whitePen, location.X + 1, location.Y + 1, location.X - 5, location.Y + 1);
                    screen.DrawLine(whitePen, location.X - 20, location.Y + 6, location.X - 10, location.Y + 6);
                    screen.DrawLine(whitePen, location.X - 20, location.Y - 6, location.X - 10, location.Y - 6);
                    screen.DrawLine(whitePen, location.X + 20, location.Y, location.X + 10, location.Y);
                    newB = new Point(location.X - 25, location.Y + 2);
                    newB2 = new Point(location.X - 25, location.Y - 10);
                    newB3 = new Point(location.X + 20, location.Y - 3);
                    button1 = new Button();
                    button2 = new Button();
                    button3 = new Button();
                    button1.Size = new Size(8, 8);
                    button2.Size = new Size(8, 8);
                    button3.Size = new Size(8, 8);
                    newB.Offset(0, 0);
                    newB2.Offset(0, 0);
                    newB3.Offset(0, 0);
                    button1.Location = newB;
                    button2.Location = newB2;
                    button3.Location = newB3;
                    button1.Click += Button_Click1;
                    button2.Click += Button_Click2;
                    button3.Click += Button_Click3;
                    drawPanel.Controls.Add(button1);
                    drawPanel.Controls.Add(button2);
                    drawPanel.Controls.Add(button3);

                    if (jaOfNee == true)
                    {
                        screen.DrawString(pair.Item1.ToString(), new Font(FontFamily.GenericMonospace, (float) 6), whiteBrush, location.X - 25, location.Y - 25);

                    }
                    break;

                default:
                    break;

            }

        }


        for (int i = drawPanel.Left - 5; i < drawPanel.Right + 10; i += 5)
        {
            for (int j = drawPanel.Top - 50; j < drawPanel.Bottom - 5; j += 5)
            {
                screen.FillEllipse(redBrush, i, j, 2, 2); //fills screen with a grid
            }
        }



        //  base.OnPaint(e);

        // draw current location
        if (currentLocation != null)
        {
            Point p = currentLocation.Value;
            screen.FillEllipse(tealBrush, p.X + 1, p.Y, -4, -4);
        }

        drawPanel.CreateGraphics().DrawImage(backBuffer, 0, 0); //allows you to draw
    }
}

(Вот так я пытаюсь соединить свои 2 кнопки)

 foreach (var line in allLines)
    {
        screen.DrawLine(whitePen, line.Item1, line.Item2);
    }

Я еще не использую кнопку 3, потому что сначала хочу подключить свои входы, поэтому не против кнопки 3. Вот как я пытаюсь сохранить свои координаты XY.

private Point point1;
    private Point point2;

    private void Button_Click1(object sender, EventArgs e)
    {
        Console.WriteLine("button1");
        point1 = button1.Location;
    }

    private void Button_Click2(object sender, EventArgs e)
    {
        Console.WriteLine("button2");
        point2 = button2.Location;

    }

    private void Button_Click3(object sender, EventArgs e)
    {
        Console.WriteLine("button3");
    }

    private void DrawLine_Click(object sender, EventArgs e)
    {
        allLines.Add(new Tuple<Point, Point>(point1, point2));
    }

После того, как я нажал кнопку DrawLine, он добавляет местоположение в список.Извините за ужасный код.Я надеюсь, что вы поняли мой вопрос немного больше, мне действительно сложно объяснить мою проблему.

...