拖放Windows窗体

时间:2013-06-25 08:04:41

标签: c# .net winforms drag-and-drop

private void calendarPlanner1_ItemClick(object sender, WeekPlannerItemEventArgs e)
{
    DoDragDrop(calendarPlanner1.SelectedItem, DragDropEffects.Move);
}

private void calendarPlanner1_DragEnter(object sender, DragEventArgs e)
{
    e.Effect = DragDropEffects.Move;
}

private void calendarPlanner1_DragDrop(object sender, DragEventArgs e)
{
    SRRepair repairDrag = new SRRepair();
    var rows = calendarPlanner1.Rows;
    var row = rows.ElementAt(calendarPlanner1.SelectedRowIndex);
    row.Items.Add((WeekPlannerItem)e.Data.GetData(typeof(WeekPlannerItem)));
    repairDrag = assignedRepairsList[assignedItemCollection.IndexOf(calendarPlanner1.SelectedItem)];
    repairDrag.AssignedToUser = engineerList[calendarPlanner1.SelectedRowIndex];
    repairDrag.Update();
}

上面的代码是我到目前为止的拖放操作。它工作正常,直到第三个(拖放)方法。基本上我想要实现的是在名称之间拖动项目。我可以使用'calendar.SelectedRowIndex'来获取我拖动项目的索引,但问题是获取目标的索引或我想将其拖动到的位置。允许我拖动项目但是当我放开鼠标中的左按钮时它会返回它来自的位置。日历是一个开源的,我从codeproject找到它,我正在使用和修改它以将其添加到现有的桌面应用程序中。

当我释放鼠标左键时,我是否可以用它来获取鼠标的位置?

2 个答案:

答案 0 :(得分:0)

我认为,除了拖放操作之外,你还需要跟踪你的鼠标移动(或鼠标输入),为了获得一个元素的索引,拖拽自己也无济于事(它是第一个点),只需通过鼠标移动跟踪和识别目标。

或者简单地说,为你想要的对象(控件)添加鼠标输入事件,通过鼠标输入选择目标位置,并通过drop完成演示,最好在放置操作时删除鼠标输入事件完成后,再按一次输入即可添加。

希望我的问题是真的

答案 1 :(得分:0)

我很确定在桌面应用程序中你可以获得鼠标位置,而不必依赖传递的mouseevent参数。

control.mouseposition - http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mouseposition.aspx

你应该能够通过'control'.mouseposition获得'point'值。控制可能是你正在使用的形式。

修改

如参考文献中所述,control.mouseposition方法与this.cursor.position相同。

cursor.position - http://msdn.microsoft.com/en-us/library/system.windows.forms.cursor.position.aspx

此外,您将要找到控件(复数),它们位于(或具有包含的ClientRectangle边界)您捕获的光标位置点。

control.GetChildAtPoint(Point) ..您可能需要递归...

GetChildAtPoint - http://msdn.microsoft.com/en-us/library/ms158404.aspx