简体   繁体   English

拖放Windows表单

[英]Drag and Drop Windows Form

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();
}

The code above is what I have so far for drag and drop operation. 到目前为止,上面的代码是我进行拖放操作所需要的。 It works ok until the third (drag drop) method. 直到第三个(拖放)方法都可以。 Basically what im trying to achieve is to drag an item between the names. 基本上,我想实现的目标是在名称之间拖动一个项目。 I could get the index where I drag the item from using 'calendar.SelectedRowIndex' but the problem is getting the index of the destination or where i want to drag it to. 我可以从使用“ calendar.SelectedRowIndex”将项目拖动到的位置获取索引,但是问题是获取目的地的索引或我要将其拖动到的位置。 Allows me to drag items but when i let go of the left button in the mouse it goes back where it came from. 允许我拖动项目,但是当我放开鼠标左键时,它会返回它的来源。 The calendar is an open source and i found it from codeproject and i am using and modifying it to add it in an existing desktop application. 日历是一个开放源代码,我是从codeproject中找到的,并且正在使用和修改它以将其添加到现有的桌面应用程序中。

Is there anyway or an event I could use to get the position of the mouse as soon I release the left button in the mouse? 无论如何,一旦释放鼠标左键,我可以用来获取鼠标的位置吗?

I think, beside the drag and drop operation, you need to track your mouse move(or mouse enter) too, for getting the index of one element, drag'n'drop itself would not help(it get's the very first point), just track and identify the target by mouse move. 我认为,除了拖放操作之外,您还需要跟踪鼠标的移动(或鼠标进入),要获取一个元素的索引,拖放本身无济于事(这是第一点),只需通过鼠标移动即可跟踪并识别目标。

or simply, add mouse enter event for the object(controls) you want targeted by the drop, choose the target position via mouse enter, and complete the oration via drop, and it's better to remove mouse enter event when the drop operation completed, and add it by drop enter again. 或者,简单地,为您希望放置对象定位的对象(控件)添加鼠标进入事件,通过鼠标输入选择目标位置,并通过放置完成操作,最好在放置操作完成后删除鼠标输入事件,并且通过拖放再次添加。

hope I got your question true 希望我能回答你的问题

I'm pretty sure in a desktop application you can get the mouse position without having to rely on passed mouseevent arguments. 我非常确定,在桌面应用程序中,您不必依赖传递的mouseevent参数即可获得鼠标位置。

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

you should just be able to get the 'point' value through 'control'.mouseposition. 您应该只能够通过'control'.mouseposition获得'point'值。 where the control is likely the form you're working with. 控件可能是您正在使用的表单的位置。

Edit 编辑

As stated in the reference, the control.mouseposition method is identical to this.cursor.position. 如参考文献中所述,control.mouseposition方法与this.cursor.position相同。

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

Additionally, you're going to want to locate the controls (plural), which are located at (or have ClientRectangle bounds that encompass) the cursor location point you capture.. 此外,您将要定位控件(多个),它们位于捕获的光标位置点处(或具有ClientRectangle边界围绕)。

control.GetChildAtPoint(Point) .. which you may have to do recursively.. control.GetChildAtPoint(Point) ..您可能必须递归执行。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM