在拖放过程中如何更改鼠标光标? [英] How to change mouse cursor during drag and drop?
问题描述
背景:我有一个C#winforms应用程序.我正在将信息从一个datagridview拖到另一个.对于目标网格上的拖动事件,我具有以下代码:
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.
问题:如何使光标成为目标网格上所有位置的"Cursor.No"图标,但将其设置为"Mouse"图标(当鼠标悬停在一行上时)索引为奇数?
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?
谢谢.
编辑:Aseem的解决方案最终对我有用.
Aseem's solution ended up working for me.
推荐答案
使用HitTest获取行索引.尝试一下,尽管未测试-
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;
}
}
这篇关于在拖放过程中如何更改鼠标光标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!