您好我正在为我的应用程序使用ObjectListView。我尝试制作一个花哨的拖拽效果,因为原来的蓝色效果并不是那么好。这就像我拖动时突出显示第一列。我在正常的listview上使用它
private void lvPlaylist_DragOver(object sender, DragEventArgs e)
{
Point mLoc = lvPlaylist.PointToClient(Cursor.Position);
var hitt = lvPlaylist.HitTest(mLoc);
if (hitt.Item == null) return;
int idx = hitt.Item.Index;
if (idx == prevItem) return;
lvPlaylist.Refresh();
using (Graphics g = lvPlaylist.CreateGraphics())
{
Rectangle rect = lvPlaylist.GetItemRect(idx);
Pen MyPen = new Pen(Color.OrangeRed, 3);
g.DrawLine(MyPen, rect.Left, rect.Top, rect.Right, rect.Top);
}
prevItem = idx;
}
但它不适用于ObjectListView。实际上确实如此,但当我停止拖动而不释放拖动物体时,它会向我显示蓝色默认拖动效果,而我一直在移动,我看到了自己的拖动效果。有没有办法禁用OLV拖动效果?
您的解决方案是否在所选项目的上方/下方绘制一条线?
您可以允许在使用之间删除:
lvPlaylist.IsSimpleDropSink = true;
((SimpleDropSink)lvPlaylist.DropSink).CanDropBetween = true;
如果这还不够好,你可以回复ModelCanDrop
,例如
//((SimpleDropSink)lvPlaylist.DropSink).ModelCanDrop+= ModelCanDrop;
private void ModelCanDrop(object sender, ModelDropEventArgs e)
{
e.DropSink.Billboard.BackColor = Color.GreenYellow;
e.DropSink.FeedbackColor = Color.GreenYellow;
e.InfoMessage = "Hey there";
e.Handled = true;
e.Effect = DragDropEffects.Move;
}
如果你真的讨厌那么多你甚至可以:
e.DropSink.EnableFeedback = false;
ObjectListView站点有一个关于拖放的深入的深入教程:
http://objectlistview.sourceforge.net/cs/blog4.html#blog-rearrangingtreelistview
如果你想寻找真正想要的东西,你可以为SimpleDropSink编写自己的子类:
lvPlaylist.IsSimpleDragSource = true;
lvPlaylist.DropSink = new MyDropSink();
private class MyDropSink : SimpleDropSink
{
public override void DrawFeedback(Graphics g, Rectangle bounds)
{
if(DropTargetLocation != DropTargetLocation.None)
g.DrawString("Heyyy stuffs happening",new Font(FontFamily.GenericMonospace, 10),new SolidBrush(Color.Magenta),bounds.X,bounds.Y );
}
}
为了你想要的行为,你应该尝试这样的事情:
private class MyDropSink : SimpleDropSink
{
private ObjectListView _olv;
public MyDropSink(ObjectListView olv)
{
_olv = olv;
}
public override void DrawFeedback(Graphics g, Rectangle bounds)
{
if(DropTargetLocation != DropTargetLocation.None)
{
Point mLoc = _olv.PointToClient(Cursor.Position);
var hitt = _olv.HitTest(mLoc);
if (hitt.Item == null) return;
int idx = hitt.Item.Index;
Rectangle rect = _olv.GetItemRect(idx);
Pen MyPen = new Pen(Color.OrangeRed, 3);
g.DrawLine(MyPen, rect.Left, rect.Top, rect.Right, rect.Top);
}
}
}