How to change mouse cursor during drag and drop?

▼魔方 西西 提交于 2021-02-19 06:35:10

问题


Background: I have a C# winforms application. I am dragging information from one datagridview to another. For my drag over event on the destination grid, I have the following code:

private void grid_DragOver(object sender, DragEventArgs e)
{
      if(e.Data.GetDataPresent(typeof(SelectedRecordsCollection)))
      {
          e.Effect = DragDropEffects.Move; 
      }
}

I want to limit the drop to only be allowed when the mouse is hovered over particular rows (say, rows with an odd index number). I currently limit what I actually add to the destination grid in the dragdrop event. However, because of the above code, my cursor changes to a Move icon as soon as the mouse hovers anywhere on the destination control.

Question: How do I make it so that the cursor is a "Cursor.No" icon everywhere on the destination grid, except set it to the Move icon for when the mouse is over a row with an odd index?

Thank you.

Edit: Aseem's solution ended up working for me.


回答1:


Get the row index using HitTest. Try this, not tested though -

private void grid_DragOver(object sender, DragEventArgs e)
{
    // Get the row index of the item the mouse is below. 
    Point clientPoint = dataGridView1.PointToClient(new Point(e.X, e.Y));
    DataGridView.HitTestInfo hit = dataGridView1.HitTest(clientPoint.X, clientPoint.Y);
    if (hit.Type == DataGridViewHitTestType.Cell) {
        e.Effect = (hit.RowIndex%2 == 0)  //move when odd index, else none
            ? DragDropEffects.None
            : DragDropEffects.Move;
    }
}


来源:https://stackoverflow.com/questions/32391655/how-to-change-mouse-cursor-during-drag-and-drop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!