您好,我正在为我的应用程序使用ObjectListView。我尝试制作奇特的拖曳效果,因为原始的蓝色效果不太好。就像我拖动时突出显示第一列。我在普通列表视图中使用了
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拖动效果吗?
答案 0 :(得分:1)
您的解决方案在所选项目的上方/下方绘制了一条线?
您可以允许在使用之间删除:
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);
}
}
}