[英]C# emulating the default drag-drop functionality in windows (transparent image during drag)
我有一個帶有圖像單元格(和隱藏的文本單元格)的數據網格,在其中我希望能夠將圖像從一個單元格拖放到另一個單元格,這是我正在工作的部分,在拖動過程中,我無法使用的是光標和下降。 在拖動時,我希望圖像替換光標,類似於在Windows桌面上拖動文件時發生的情況。 當前正在發生的是,光標在我要使用的自定義圖像和默認拖動光標(帶有小矩形的指針)之間閃爍。 我對如何解決這個問題有點迷茫。 請注意,我不是編碼專家,我的大部分代碼都是通過谷歌搜索我想完成的內容並將它們拼湊在一起而組合在一起的。
問題的GIF: https : //gfycat.com/AgileImpressiveHermitcrab請注意,我的錄音機無法捕獲所有內容,它的閃爍速度比顯示的快得多,並且在鼠標沒有很好地移動時也閃爍,並且不停地閃爍(僅當按住鼠標按鈕時) )
private DataGridViewCell drag_Image; //This is the initial image dragged
private object HoldingImage; //Stored target image
private DataGridViewCell drag_ID; //ID tied with initial image
private object HoldingID; //ID tied with stored target image
private Bitmap DragCursor; //This is the initial dragged image, set as the cursor
private string DragIndicator; //Attempting a new trigger for drag event
private void dataGridView1_MouseMove(object sender, MouseEventArgs e)
{//This code is not needed for functionality, what is the point here?
//if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
{
// If the mouse moves outside the rectangle, start the drag.
//if (dragBoxFromMouseDown != Rectangle.Empty && !dragBoxFromMouseDown.Contains(e.X, e.Y))
{
// Proceed with the drag and drop, passing in the list item.
//DragDropEffects dropEffect = dataGridView1.DoDragDrop(dataGridView1.Rows[dataGridView1.CurrentRow.Index],DragDropEffects.Move);
}
}
}
private void dataGridView1_DragOver(object sender, DragEventArgs e)
{
//This is where the cursor begins to flicker in the debugger, it cycles through this code
Bitmap DragCursor = new Bitmap(@"C: \Users\******\Desktop\Items\Organizer\Planner\Planner\1.png");
DragCursor.MakeTransparent(Color.White);
Cursor cur = new Cursor(DragCursor.GetHicon());
Cursor.Current = cur;
e.Effect = DragDropEffects.Move;
DragIndicator = "1";
}
private void dataGridView1_DragDrop(object sender, DragEventArgs e)
{
// The mouse locations are relative to the screen, so they must be
// converted to client coordinates.
Point clientPoint = dataGridView1.PointToClient(new Point(e.X, e.Y));
// Get the row index of the item the mouse is below.
DataGridView.HitTestInfo hti = dataGridView1.HitTest(clientPoint.X, clientPoint.Y);
DataGridViewCell targetCell = dataGridView1[hti.ColumnIndex, hti.RowIndex];
DataGridViewCell targetCellID = dataGridView1[hti.ColumnIndex + 1, hti.RowIndex];
// If the drag operation was a move then remove and insert the row.
//if (e.Effect == DragDropEffects.Move)
if (DragIndicator == "1") //The new trigger works though we still can not removed all DragDropEffect.Move lines, as this disables dragging
{
if (SwapBtn.Enabled == false)
{
//Swap mode
HoldingImage = targetCell.Value;
targetCell.Value = drag_Image.Value;
drag_Image.Value = HoldingImage;
HoldingID = targetCellID.Value;
targetCellID.Value = drag_ID.Value;
drag_ID.Value = HoldingID;
dataGridView1.Refresh();
DragIndicator = "0";
}
else
{
//TODO Insert mode
}
}
}
public void dataGridView1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
DataGridView.HitTestInfo hti = dataGridView1.HitTest(e.X, e.Y);
drag_Image = dataGridView1[hti.ColumnIndex, hti.RowIndex];
drag_ID = dataGridView1[hti.ColumnIndex + 1, hti.RowIndex];
// Proceed with the drag and drop, passing in the list item.
DragDropEffects dropEffect = dataGridView1.DoDragDrop(drag_Image, DragDropEffects.Move);
}
}
所以我沒有100%解決我的問題,但我會接受。 更改光標並避免閃爍的解決方案是自定義光標與默認值進行斗爭。
private void dataGridView1_GiveFeedback(object sender, GiveFeedbackEventArgs e)
{
e.UseDefaultCursors = false;
}
與Windows拖放功能一樣,圖像上沒有箭頭指針,但它現在可以使用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.