C# emulating the default drag-drop functionality in windows (transparent image during drag)










2















I have a datagrid with image cells (and hidden text cells) in which I want to be able to drag and drop images from one cell to another, this part I have working, what I can not get to work is the cursor during the drag and drop. While dragging I would like the image to replace the cursor similar to what occurs when you drag a file on your windows desktop. Currently what is occurring is that the cursor is flickering between the custom image I would like to use and the default drag cursor (pointer with a small rectangle). I'm a bit lost on how to fix this. Note that I'm no expert in coding and much of my code is put together from googling what I am trying to complete and piecing things together.



GIF of issue: https://gfycat.com/AgileImpressiveHermitcrab Note that my recorder didn't capture everything, it flickers much faster than shown and flickers while the mouse is not moving as well, non stop (only when holding down the mouse button)



 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******DesktopItemsOrganizerPlannerPlanner1.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);












share|improve this question


























    2















    I have a datagrid with image cells (and hidden text cells) in which I want to be able to drag and drop images from one cell to another, this part I have working, what I can not get to work is the cursor during the drag and drop. While dragging I would like the image to replace the cursor similar to what occurs when you drag a file on your windows desktop. Currently what is occurring is that the cursor is flickering between the custom image I would like to use and the default drag cursor (pointer with a small rectangle). I'm a bit lost on how to fix this. Note that I'm no expert in coding and much of my code is put together from googling what I am trying to complete and piecing things together.



    GIF of issue: https://gfycat.com/AgileImpressiveHermitcrab Note that my recorder didn't capture everything, it flickers much faster than shown and flickers while the mouse is not moving as well, non stop (only when holding down the mouse button)



     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******DesktopItemsOrganizerPlannerPlanner1.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);












    share|improve this question
























      2












      2








      2








      I have a datagrid with image cells (and hidden text cells) in which I want to be able to drag and drop images from one cell to another, this part I have working, what I can not get to work is the cursor during the drag and drop. While dragging I would like the image to replace the cursor similar to what occurs when you drag a file on your windows desktop. Currently what is occurring is that the cursor is flickering between the custom image I would like to use and the default drag cursor (pointer with a small rectangle). I'm a bit lost on how to fix this. Note that I'm no expert in coding and much of my code is put together from googling what I am trying to complete and piecing things together.



      GIF of issue: https://gfycat.com/AgileImpressiveHermitcrab Note that my recorder didn't capture everything, it flickers much faster than shown and flickers while the mouse is not moving as well, non stop (only when holding down the mouse button)



       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******DesktopItemsOrganizerPlannerPlanner1.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);












      share|improve this question














      I have a datagrid with image cells (and hidden text cells) in which I want to be able to drag and drop images from one cell to another, this part I have working, what I can not get to work is the cursor during the drag and drop. While dragging I would like the image to replace the cursor similar to what occurs when you drag a file on your windows desktop. Currently what is occurring is that the cursor is flickering between the custom image I would like to use and the default drag cursor (pointer with a small rectangle). I'm a bit lost on how to fix this. Note that I'm no expert in coding and much of my code is put together from googling what I am trying to complete and piecing things together.



      GIF of issue: https://gfycat.com/AgileImpressiveHermitcrab Note that my recorder didn't capture everything, it flickers much faster than shown and flickers while the mouse is not moving as well, non stop (only when holding down the mouse button)



       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******DesktopItemsOrganizerPlannerPlanner1.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);









      c# datagridview drag-and-drop cursor drag






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 '18 at 15:52









      C PC P

      161




      161






















          1 Answer
          1






          active

          oldest

          votes


















          0














          So I didn't 100% solve my issue, but I'll take it. The solution to change the cursor and avoid the flickering as the custom cursor fights with the default is...



           private void dataGridView1_GiveFeedback(object sender, GiveFeedbackEventArgs e)

          e.UseDefaultCursors = false;



          No arrow pointer over the image as with windows drag-drop functionality but it works for now.






          share|improve this answer






















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53265701%2fc-sharp-emulating-the-default-drag-drop-functionality-in-windows-transparent-im%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            So I didn't 100% solve my issue, but I'll take it. The solution to change the cursor and avoid the flickering as the custom cursor fights with the default is...



             private void dataGridView1_GiveFeedback(object sender, GiveFeedbackEventArgs e)

            e.UseDefaultCursors = false;



            No arrow pointer over the image as with windows drag-drop functionality but it works for now.






            share|improve this answer



























              0














              So I didn't 100% solve my issue, but I'll take it. The solution to change the cursor and avoid the flickering as the custom cursor fights with the default is...



               private void dataGridView1_GiveFeedback(object sender, GiveFeedbackEventArgs e)

              e.UseDefaultCursors = false;



              No arrow pointer over the image as with windows drag-drop functionality but it works for now.






              share|improve this answer

























                0












                0








                0







                So I didn't 100% solve my issue, but I'll take it. The solution to change the cursor and avoid the flickering as the custom cursor fights with the default is...



                 private void dataGridView1_GiveFeedback(object sender, GiveFeedbackEventArgs e)

                e.UseDefaultCursors = false;



                No arrow pointer over the image as with windows drag-drop functionality but it works for now.






                share|improve this answer













                So I didn't 100% solve my issue, but I'll take it. The solution to change the cursor and avoid the flickering as the custom cursor fights with the default is...



                 private void dataGridView1_GiveFeedback(object sender, GiveFeedbackEventArgs e)

                e.UseDefaultCursors = false;



                No arrow pointer over the image as with windows drag-drop functionality but it works for now.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 13 '18 at 16:26









                C PC P

                161




                161



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53265701%2fc-sharp-emulating-the-default-drag-drop-functionality-in-windows-transparent-im%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Use pre created SQLite database for Android project in kotlin

                    Darth Vader #20

                    Ondo