为了账号安全,请及时绑定邮箱和手机立即绑定

ObjectListView 忽略拖动效果

ObjectListView 忽略拖动效果

C#
慕哥9229398 2022-12-04 10:50:24
您好,我正在为我的应用程序使用 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 拖动效果吗?
查看完整描述

1 回答

?
慕桂英546537

TA贡献1848条经验 获得超10个赞

您的解决方案在所选项目的上方/下方画了一条线?


您可以允许在使用之间删除:


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);

        }

    }

}


查看完整回答
反对 回复 2022-12-04
  • 1 回答
  • 0 关注
  • 162 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号