C#模拟Windows中的默认拖放功能(拖动过程中的透明图像)

时间:2018-11-12 15:52:54

标签: c# datagridview drag-and-drop cursor 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);
        }

    }

1 个答案:

答案 0 :(得分:0)

所以我不是100%解决我的问题,但我会接受。更改光标并避免闪烁的解决方案是自定义光标与默认值进行斗争,这是...

    private void dataGridView1_GiveFeedback(object sender, GiveFeedbackEventArgs e)
    {
        e.UseDefaultCursors = false;
    }

与Windows拖放功能一样,图像上没有箭头指针,但目前仍然有效。