背景:我有一个 C# winforms 应用程序。我正在将信息从一个数据 GridView 拖动到另一个数据 GridView 。对于目标网格上的拖动事件,我有以下代码:
private void grid_DragOver(object sender, DragEventArgs e)
{
if(e.Data.GetDataPresent(typeof(SelectedRecordsCollection)))
{
e.Effect = DragDropEffects.Move;
}
}
我想限制仅当鼠标悬停在特定行(例如,具有奇数索引号的行)上时才允许放置。目前,我限制了在拖放事件中实际添加到目标网格的内容。但是,由于上述代码,一旦鼠标悬停在目标控件上的任意位置,我的光标就会变为“移动”图标。
问题:如何使光标在目标网格上的任何位置都是“Cursor.No”图标,除了当鼠标悬停在一行上时将其设置为“移动”图标之外索引奇数?
谢谢。
编辑:Aseem 的解决方案最终对我有用。
最佳答案
使用 HitTest 获取行索引。试试这个,虽然没有测试过 -
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;
}
}
关于c# - 如何在拖放过程中改变鼠标光标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32391655/