C#在面板周围拖动控件

本文关键字:拖动 控件 周围 | 更新日期: 2023-09-27 18:28:14

我正在开发一个允许用户在同一面板内拖动对象的系统,我进行了一些研究,发现我应该使用鼠标事件,如mouse_up、mouse_down和mouse_move。

该程序将生成3个picturebox,并允许用户在面板内的每个picturebox周围拖动,但我编写的程序并不完美,因为当我在picturebox上拖动时,picturebox会移动,但不会根据我的鼠标光标位置,它在其他地方,此外,当拖动时,面板中有picturebox阴影,我已经尝试过更新()、刷新(),和invalidate(),但它似乎对我没有用处。下面是我的代码,感谢你帮助

public partial class Form1 : Form
{
    List<PictureBox> pictureBoxList = new List<PictureBox>();
    private bool isDragging = false;
    public Form1()
    {
        InitializeComponent();
        for (int i = 0; i < 3; i++)
        {
            PictureBox picture = new PictureBox
            {
                Name = "pictureBox" + i,
                Size = new Size(20, 20),
                Location = new Point(i * 40, i * 40),
                BorderStyle = BorderStyle.FixedSingle,
                SizeMode = PictureBoxSizeMode.Zoom,
                ImageLocation = "A.jpg"
            };
            pictureBoxList.Add(picture);

            foreach (PictureBox p in pictureBoxList)
            {
                p.MouseDown += new MouseEventHandler(c_MouseDown);
                p.MouseMove += new MouseEventHandler(c_MouseMove);
                p.MouseUp += new MouseEventHandler(c_MouseUp);
                pnlDisplayImage.Controls.Add(p);
                pnlDisplayImage.Refresh();
            }
        }
    }

    void c_MouseDown(object sender, MouseEventArgs e)
    {
        isDragging = true;
    }
    void c_MouseMove(object sender, MouseEventArgs e)
    {
        if (isDragging == true) {
            Control c = sender as Control;
            for (int i = 0; i < pictureBoxList.Count(); i++)
            {
                if (c.Equals(pictureBoxList[i]))
                {
                    pictureBoxList[i].Location = new Point(e.X, e.Y);
                }
            }
        }
    }
    void c_MouseUp(object sender, MouseEventArgs e)
    {
        PictureBox c = sender as PictureBox;
        isDragging = false;
        for (int i = 0; i < pictureBoxList.Count(); i++) { 
            if (c.Equals(pictureBoxList[i])){
                pictureBoxList[i].Location = new Point(e.X, e.Y);
            }
        }
    }
    private void pnlDisplayImage_Paint(object sender, PaintEventArgs e)
    {
        foreach (PictureBox p in pictureBoxList)
        {
            pnlDisplayImage.Controls.Add(p);
        }
    }
}

C#在面板周围拖动控件

最后,我发现是什么问题导致我的程序没有按预期运行。主要问题是我不小心把foreach循环放在了我用来创建pictureBox的for循环中,这个问题导致pictureBox在运行时拖动时出现了一些阴影效果,因为很少有相同的pictureBox。此外,我更改了一些代码,现在它按我的预期运行。下面是我想要回答的代码。

public partial class Form1 : Form
{
    List<PictureBox> pictureBoxList = new List<PictureBox>();
    private bool isDragging = false;
    Point move;
    public Form1()
    {
        InitializeComponent();
        for (int i = 0; i < 3; i++)
        {
            PictureBox picture = new PictureBox
            {
                Name = "pictureBox" + i,
                Size = new Size(20, 20),
                Location = new Point(i * 40, i * 40),
                BorderStyle = BorderStyle.FixedSingle,
                SizeMode = PictureBoxSizeMode.Zoom,
                ImageLocation = "A.jpg"
            };
            pictureBoxList.Add(picture);
        }
        foreach (PictureBox p in pictureBoxList)
        {
                p.MouseDown += new MouseEventHandler(c_MouseDown);
                p.MouseMove += new MouseEventHandler(c_MouseMove);
                p.MouseUp += new MouseEventHandler(c_MouseUp);
                pnlDisplayImage.Controls.Add(p);
                pnlDisplayImage.Refresh();
         }
    }
    void c_MouseDown(object sender, MouseEventArgs e)
    {
        Control c = sender as Control;
        isDragging = true;
        move = e.Location;
    }
    void c_MouseMove(object sender, MouseEventArgs e)
    {
        if (isDragging == true) {
            Control c = sender as Control;
            for (int i = 0; i < pictureBoxList.Count(); i++)
            {
                if (c.Equals(pictureBoxList[i]))
                {
                    pictureBoxList[i].Left += e.X - move.X;
                    pictureBoxList[i].Top += e.Y - move.Y;
                }
            }
        }
    }
    void c_MouseUp(object sender, MouseEventArgs e)
    {
        isDragging = false;
    }
}

试试这样的东西(它是带有重写的自定义控件,但应该很容易转换为事件):

    private bool _isMoved = false;  // true if move mode on
    private Point _pointMove = new Point(0);    // for moving
    protected override void OnMouseDown(MouseEventArgs e)
    {
        // if left button pressed
        if(e.Button == MouseButtons.Left) 
        {
            _pointMove.X = e.X;
            _pointMove.Y = e.Y;
            _isMoved = true;
            Cursor = Cursors.SizeAll;
            Capture = true;
        }
        base.OnMouseDown (e);
    }
    protected override void OnMouseUp(MouseEventArgs e)
    {
        // if move mode on
        if(_isMoved) 
        {
            _isMoved = false;
            Cursor = Cursors.Default;
            Capture = false;
        }
        base.OnMouseUp (e);
    }
    protected override void OnMouseMove(MouseEventArgs e)
    {
        // if move mode on
        if (_isMoved)
        {
            Left += e.X - _pointMove.X;
            Top += e.Y - _pointMove.Y;
        }
        base.OnMouseMove (e);
    }