DragDrop Event返回表单中的位置,拖放在flowLayoutPanel中

时间:2013-02-27 12:14:37

标签: c# .net winforms visual-studio

我在FlowlayoutPanel中拖放pictureBox但事件返回表单中的位置...我需要在FlowLayoutPanel中使用此位置(pictureBoxes在FlowLayoutPanel中并拖放到那里)

   public Form1()
    {
        InitializeComponent();

        flowLayoutPanel1.AllowDrop = true;
    }


    private void pictureBox_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            picBox = (PictureBox)sender;
            if (picBox.Image != null)
            {
                var dragImage = new Bitmap((Bitmap)picBox.Image, picBox.Size);
                IntPtr icon = dragImage.GetHicon();
                Cursor.Current = new Cursor(icon);
                DoDragDrop(picBox.Image, DragDropEffects.Copy);
            }
        }
    }


        void Form1_DragEnter(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(typeof(Bitmap)))
            e.Effect = DragDropEffects.Copy;
    }

    // Occurs when the user releases the mouse over the drop target 
    void Form1_DragDrop(object sender, DragEventArgs e)
    {
       MessageBox.Show("Posicao x " + e.Y + "Posicao Y " + e.Y);//reutrn the position in form, not in flowLayoutPanel
    }

1 个答案:

答案 0 :(得分:3)

您必须使用此方法:

var point = flowLayoutPanel1.PointToClient(new Point(e.X, e.Y));

这将屏幕位置坐标转换为控制一个,更多信息here