【问题标题】:Drag and Drop Windows Form拖放 Windows 窗体
【发布时间】:2013-06-25 08:04:41
【问题描述】:
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 中找到它,我正在使用和修改它以将其添加到现有的桌面应用程序中。

我释放鼠标左键后是否有任何事件或事件可以用来获取鼠标的位置?

【问题讨论】:

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


    【解决方案1】:

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

    或者简单的说,为你想要drop的对象(控件)添加鼠标进入事件,通过鼠标进入选择目标位置,通过drop完成演说,最好在drop操作时去掉鼠标进入事件完成,然后再次通过drop enter添加。

    希望你的问题是真的

    【讨论】:

      【解决方案2】:

      我很确定在桌面应用程序中,您无需依赖传递的 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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-02-14
        • 2016-11-08
        • 1970-01-01
        • 2021-01-03
        • 1970-01-01
        • 1970-01-01
        • 2011-12-09
        • 1970-01-01
        相关资源
        最近更新 更多