C#winforms拖放 [英] C# winforms drag and drop

查看:62
本文介绍了C#winforms拖放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi, trying to implement drag and drop
I have custom ToolBox class that simply iherits FlowLayoutPanel, and some picture boxes within. When user clicks on any of these picture boxes, it got copied and added to form's controls just on top of original. Then when user moves mouse around with left button pressed, that picture box should move along, but it don't. It works only if you release mouse button and then click again and move.





我尝试过:





What I have tried:

//want to mentioned that that's not the full code, only what is related to the question
 public partial class FigureToolBox : FlowLayoutPanel
 {

        public delegate void FigureSelected(PictureBox figurePicture,Point location);

        public event FigureSelected OnFigureSelect;
       
      //on mouse down of each picture box
      public void Figure_mouse_down(object c, MouseEventArgs e)
        {
            PictureBox pictureBox = c as PictureBox;

            PictureBox clonedPicture = new PictureBox();
            clonedPicture.SizeMode = PictureBoxSizeMode.AutoSize;
            clonedPicture.Image = pictureBox.Image;
            
            OnFigureSelect(clonedPicture, e.Location);

        }

     //some code
}
public partial class GameForm : Form
{
   Point point = new Point(); //start point of moving
   PictureBox draggedFig;


   public GameForm()
   {
     InitializeComponent();
     figureToolBox1.OnFigureSelect += FigureSelected;
   }

   private void FigureSelected(PictureBox box, Point location)
   {
            draggedFig = box;
            //just calculating  location of picture box in form's coords
            Point point = PointToClient(Cursor.Position);
            box.Left = (point.X- location.X);
            box.Top = (point.Y- location.Y);

            this.point = location;
            
            this.Controls.Add(draggedFig);
            draggedFig.BringToFront();

            draggedFig.MouseMove += DraggedFig_Move;
            draggedFig.Focus(); //thought it will help, but it won't
     }

    private void DraggedFig_Move(object sender, MouseEventArgs e){

            if (e.Button == MouseButtons.Left)
            {
                draggedFig.Left += e.X - point.X;
                draggedFig.Top += e.Y - point.Y;
            }
     }
}

推荐答案

查看焦点的位置。如果您可以将焦点设置为新的PictureBox(这可能是第二次点击的作用),它可能会起作用。
See where the "focus" is. If you can set the focus to the new PictureBox (which is probably what the second click does) it might work.


请参阅此处的CodeProject文章:使用FlowLayoutPanel并通过拖放重新排序 [ ^ ]

并在这里回答: c# - 如何拖动从列表视图(连同图像视图)到图片框的图像? - 堆栈溢出 [ ^ ]
See CodeProject article here: Using the FlowLayoutPanel and Reordering with Drag and Drop[^]
And answer here: c# - How to drag images from a listview (together with an imageview) to pictureboxes? - Stack Overflow[^]


首先,请注意您没有使用具有某些复杂功能的WinForms拖放工具拖动等时提供反馈



看看你是否从这个例子中得到了一些想法,就像你的一样,使用一个简单的方法
First, be aware you are not using the WinForms Drag/Drop facility that has some sophisticated features for giving feedback while dragging, etc.

See if you get some ideas from this example, which like yours, is using a simple method
using System;
using System.Windows.Forms;

namespace YourNameSpace
{
    public partial class ToolBoxPanel : UserControl
    {
        public ToolBoxPanel()
        {
            InitializeComponent();
        }


        private void ToolBoxPanel_Load(object sender, EventArgs e)
        {
            parent = this.Parent as Form;
        }

        private PictureBox currentPB;

        private bool IsMouseUp = true;

        private CheckBox cb;

        private int cbCount = 0;

        private Form parent;

        // all PicturewBoxes in the UserControl use this Handler
        private void pictureBox_MouseDown(object sender, MouseEventArgs e)
        {
            currentPB = sender as PictureBox;

            switch (currentPB.Name)
            {
                case "pictureBox1":

                    cb = new CheckBox();

                    parent.Controls.Add(cb);
                    cb.Text = "CheckBox " + cbCount++;
                    cb.Show();
                    cb.BringToFront();

                    break;
            }

            IsMouseUp = false;
        }

        // all PicturewBoxes in the UserControl use this Handler
        private void pictureBox_MouseUp(object sender, MouseEventArgs e)
        {
            IsMouseUp = true;

            // cancel the drop if it's not over the Form ?
            if(this.DisplayRectangle.Contains
              (this.PointToClient
                (currentPB.PointToScreen(e.Location))))
            {
                parent.Controls.Remove(cb);
                cb.Dispose();
            }
        }

        // all PicturewBoxes in the UserControl use this Handler
        private void pictureBox_MouseMove(object sender, MouseEventArgs e)
        {
            if (IsMouseUp) return;

            // map the mouse location to the Parent Form
            cb.Location = parent.PointToClient(this.PointToScreen(e.Location));
        }
    }
}

注意:此示例中移动的控件一旦位于父窗体上就不可移动。

Note: the moved Control in this example is not movable once it has been sited on the parent Form.


这篇关于C#winforms拖放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆