如何连接图片框与连接线

时间:2014-12-25 11:24:31

标签: c# winforms

首先抱歉我的英语。我在Windows窗体中编程应用程序。它就像Packet Tracer。我有四个按钮。当我点击它们时,它们动态地创建带有路由器或交换机图片的图片框....每次我点击按钮,都会创建新的图片框(交换机或路由器......)。我也可以通过鼠标移动这个图片框。 我需要创建一个按钮,用于连接选定的图片框和线(电缆)。应通过单击选择此图片框。它sholud能够移动这个对象(可移动的线)。 这是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{

    public partial class Form1 : Form
    {
        int a = 0;
        int b = 0;
        int c = 0;
        int d = 0;

        PictureBox[] picturebox = new PictureBox[100];



        public Form1()
        {

            InitializeComponent();

        }


        private void router_Click(object sender, EventArgs e)
        {


            ++a;

            picturebox[a] = new PictureBox();
            picturebox[a].Name = "picturebox" + a;
            picturebox[a].Location = new Point(0 + (a-1) *100,100);
            picturebox[a].Size = new Size(70, 70);
            picturebox[a].BorderStyle = BorderStyle.None;
            picturebox[a].SizeMode = PictureBoxSizeMode.StretchImage;
            this.Controls.Add(picturebox[a]);


            picturebox[a].Image = Image.FromFile(@"D:\\router.jpg");
            picturebox[a].Refresh();
            picturebox[a].MouseDown += new MouseEventHandler(picMouseDown);
            picturebox[a].MouseMove += new MouseEventHandler(picMouseMove);
            picturebox[a].MouseUp += new MouseEventHandler(picMouseUp);




        }


        private void Form1_Load(object sender, EventArgs e)
        {

        }

        int x = 0;
        int y = 0;
        bool drag = false;

        private void picMouseDown(object sender, MouseEventArgs e)
        {
            // Get original position of cursor on mousedown
            x = e.X;
            y = e.Y;
            drag = true;
        }

        private void picMouseMove(object sender, MouseEventArgs e)
        {
            if (drag)
            {
                PictureBox pb = (PictureBox)sender;
                // Get new position of picture
                pb.Top += e.Y - y;
                pb.Left += e.X - x;
                pb.BringToFront();
            }
        }

        private void picMouseUp(object sender, MouseEventArgs e)
        {
            drag = false;
        }

        private void switch1_Click(object sender, EventArgs e)
        {
            ++b;
            picturebox[b] = new PictureBox();
            picturebox[b].Name = "picturebox" + b;
            picturebox[b].Location = new Point(0 + (b - 1) * 100, 180);
            picturebox[b].Size = new Size(70, 70);
            picturebox[b].BorderStyle = BorderStyle.None;
            picturebox[b].SizeMode = PictureBoxSizeMode.StretchImage;
            this.Controls.Add(picturebox[b]);


            picturebox[b].Image = Image.FromFile(@"D:\HP ProBook 450\Desktop\Grafika\switch1.png");
            picturebox[b].Refresh();
            picturebox[b].MouseDown += new MouseEventHandler(picMouseDown);
            picturebox[b].MouseMove += new MouseEventHandler(picMouseMove);
            picturebox[b].MouseUp += new MouseEventHandler(picMouseUp);



        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void pc_Click(object sender, EventArgs e)
        {
            ++c;

            picturebox[c] = new PictureBox();
            picturebox[c].Name = "picturebox" + c;
            picturebox[c].Location = new Point(0 + (c - 1) * 100, 260);
            picturebox[c].Size = new Size(70, 70);
            picturebox[c].BorderStyle = BorderStyle.None;
            picturebox[c].SizeMode = PictureBoxSizeMode.StretchImage;
            this.Controls.Add(picturebox[c]);


            picturebox[c].Image = Image.FromFile(@"D:\HP ProBook 450\Desktop\pc.jpg");
            picturebox[c].Refresh();
            picturebox[c].MouseDown += new MouseEventHandler(picMouseDown);
            picturebox[c].MouseMove += new MouseEventHandler(picMouseMove);
            picturebox[c].MouseUp += new MouseEventHandler(picMouseUp);

        }

        private void server_Click(object sender, EventArgs e)
        {
            ++d;

            picturebox[d] = new PictureBox();
            picturebox[d].Name = "picturebox" + d;
            picturebox[d].Location = new Point(0 + (d - 1) * 100, 340);
            picturebox[d].Size = new Size(70, 70);
            picturebox[d].BorderStyle = BorderStyle.None;
            picturebox[d].SizeMode = PictureBoxSizeMode.StretchImage;
            this.Controls.Add(picturebox[d]);


            picturebox[d].Image = Image.FromFile(@"D:\HP ProBook 450\Desktop\server.png");
            picturebox[d].Refresh();
            picturebox[d].MouseDown += new MouseEventHandler(picMouseDown);
            picturebox[d].MouseMove += new MouseEventHandler(picMouseMove);
            picturebox[d].MouseUp += new MouseEventHandler(picMouseUp);

        }




    }
}

感谢你的帮助。

1 个答案:

答案 0 :(得分:1)

添加图片框或移动图片框时,您需要使父项无效: (picMouseMoveclick处理程序中的4次,最好使用1个函数)

    Invalidate();

这是OnPaint的一个示例,它们位于Controls集合中的画框之间绘制线条:(您的图片框数组看起来很奇怪,您总是在索引1处添加控件,总是覆盖之前的条目?!i&#​​39;如果你需要保留对它们的引用,建议使用List。

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        var pictureBoxes = Controls.OfType<PictureBox>().ToArray();
        if (pictureBoxes.Length > 1)
        {
            for (int i = 1; i < pictureBoxes.Length; i++)
            {
                DrawLineBetween(e.Graphics, pictureBoxes[i - 1], pictureBoxes[i]);
            }
        }
    }

此功能可用于在两个方框之间画一条线:

    private void DrawLineBetween(Graphics g, PictureBox from, PictureBox to)
    {
        g.DrawLine(Pens.Black,
            new Point(from.Left + from.Width / 2, from.Top + from.Height / 2),
            new Point(to.Left + to.Width / 2, to.Top + to.Height / 2));
    }

-----以下完整示例-----

我重构了你的完整示例,并添加了上面的代码,以一个有效的例子开始你:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{

    public partial class Form1 : Form
    {
        List<PictureBox> pictureboxes = new List<PictureBox>();

        public Form1()
        {
            InitializeComponent();
        }

        private void AddPictureBox(string imagePath)
        {
            var pb = new PictureBox();
            pb.Name = "picturebox" + pictureboxes.Count;
            pb.Location = new Point(pictureboxes.Count * 100, 100);
            pb.Size = new Size(70, 70);
            pb.BorderStyle = BorderStyle.None;
            pb.SizeMode = PictureBoxSizeMode.StretchImage;
            this.Controls.Add(pb);

            pb.Image = Image.FromFile(imagePath);
            pb.Refresh();
            pb.MouseDown += new MouseEventHandler(picMouseDown);
            pb.MouseMove += new MouseEventHandler(picMouseMove);
            pb.MouseUp += new MouseEventHandler(picMouseUp);

            pictureboxes.Add(pb);

            Invalidate();
        }

        private void router_Click(object sender, EventArgs e)
        {
            AddPictureBox(@"D:\\router.jpg");
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        int x = 0;
        int y = 0;
        bool drag = false;

        private void picMouseDown(object sender, MouseEventArgs e)
        {
            // Get original position of cursor on mousedown
            x = e.X;
            y = e.Y;
            drag = true;
        }

        private void picMouseMove(object sender, MouseEventArgs e)
        {
            if (drag)
            {
                PictureBox pb = (PictureBox)sender;
                // Get new position of picture
                pb.Top += e.Y - y;
                pb.Left += e.X - x;
                pb.BringToFront();

                Invalidate();
            }
        }

        private void picMouseUp(object sender, MouseEventArgs e)
        {
            drag = false;
        }

        private void switch1_Click(object sender, EventArgs e)
        {
            AddPictureBox(@"D:\HP ProBook 450\Desktop\Grafika\switch1.png");
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void pc_Click(object sender, EventArgs e)
        {
            AddPictureBox(@"D:\HP ProBook 450\Desktop\pc.jpg");
        }

        private void server_Click(object sender, EventArgs e)
        {
            AddPictureBox(@"D:\HP ProBook 450\Desktop\server.png");
        }


        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            if (pictureboxes.Count > 1)
            {
                var arr = pictureboxes.ToArray();
                for (int i = 1; i < arr.Length; i++)
                {
                    DrawLineBetween(e.Graphics, arr[i - 1], arr[i]);
                }
            }
        }

        private void DrawLineBetween(Graphics g, PictureBox from, PictureBox to)
        {
            g.DrawLine(Pens.Black,
                new Point(from.Left + from.Width / 2, from.Top + from.Height / 2),
                new Point(to.Left + to.Width / 2, to.Top + to.Height / 2));
        }

    }
}