[英]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();
}
到目前為止,上面的代碼是我進行拖放操作所需要的。 直到第三個(拖放)方法都可以。 基本上,我想實現的目標是在名稱之間拖動一個項目。 我可以從使用“ calendar.SelectedRowIndex”將項目拖動到的位置獲取索引,但是問題是獲取目的地的索引或我要將其拖動到的位置。 允許我拖動項目,但是當我放開鼠標左鍵時,它會返回它的來源。 日歷是一個開放源代碼,我是從codeproject中找到的,並且正在使用和修改它以將其添加到現有的桌面應用程序中。
無論如何,一旦釋放鼠標左鍵,我可以用來獲取鼠標的位置嗎?
我認為,除了拖放操作之外,您還需要跟蹤鼠標的移動(或鼠標進入),要獲取一個元素的索引,拖放本身無濟於事(這是第一點),只需通過鼠標移動即可跟蹤並識別目標。
或者,簡單地,為您希望放置對象定位的對象(控件)添加鼠標進入事件,通過鼠標輸入選擇目標位置,並通過放置完成操作,最好在放置操作完成后刪除鼠標輸入事件,並且通過拖放再次添加。
希望我能回答你的問題
我非常確定,在桌面應用程序中,您不必依賴傳遞的mouseevent參數即可獲得鼠標位置。
control.mouseposition - http://msdn.microsoft.com/zh-cn/library/system.windows.forms.control.mouseposition.aspx
您應該只能夠通過'control'.mouseposition獲得'point'值。 控件可能是您正在使用的表單的位置。
如參考文獻中所述,control.mouseposition方法與this.cursor.position相同。
cursor.position - http://msdn.microsoft.com/zh-cn/library/system.windows.forms.cursor.position.aspx
此外,您將要定位控件(多個),它們位於捕獲的光標位置點處(或具有ClientRectangle邊界圍繞)。
control.GetChildAtPoint(Point) ..您可能必須遞歸執行。
GetChildAtPoint- http://msdn.microsoft.com/zh-CN/library/ms158404.aspx
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.