拖放事件返回窗体中的位置,拖放在flowLayoutPanel中
本文关键字:拖放 flowLayoutPanel 位置 窗体 事件 返回 | 更新日期: 2023-09-27 18:01:18
我正在拖放FlowlayoutPanel中的pictureBox,但事件返回表单中的位置…我需要这个位置在FlowLayoutPanel(图片框是在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
}
你必须使用这个方法:
var point = flowLayoutPanel1.PointToClient(new Point(e.X, e.Y));
这将屏幕位置坐标转换为控件1,更多信息在这里。