wpf - 在 WPF 中模拟拖/放事件

标签 wpf drag-and-drop

我想在 WPF 中模拟拖/放事件。
为此,我需要访问存储在“拖/放缓冲区”中的数据,并且还需要创建一个 DragEventArgs。 我注意到 DragEventArgs 是密封的并且没有公共(public) ctor。

所以我的问题是:
1. 如何创建 DragEventArgs 的实例?
2. 如何访问拖/放缓冲区?

最佳答案

我最近这样做了!我用 MouseDown、MouseMove 和 MouseUp 事件模拟了拖/放。例如,对于我的应用程序,我有一些 Canvas ,我想拖放它们。每个 Canvas 都有一个 id。在 MouseDown 事件中,我缓冲其 id 并在 MouseMove 和 MouseUp 事件中使用它。 Desktop_Canvas 是我的主要 Canvas ,其中包含一些 Canvas 。这些 Canvas 在我的字典(dic)中。

这是我的代码:

private Dictionary<int, Win> dic = new Dictionary<int, Win>();
    private Point downPoint_Drag = new Point(-1, -1);
    private int id_Drag = -1;
    private bool flag_Drag = false;

    public class Win
    {
        public Canvas canvas = new Canvas();
        public Point downpoint = new Point();

        public Win()
        {
            canvas.Background = new SolidColorBrush(Colors.Gray);
        }
    }

    private void Desktop_Canvas_MouseMove(object sender, MouseEventArgs e)
    {
        try
        {
            Point movePoint = e.GetPosition(Desktop_Canvas);

                    if (flag_Drag && downPoint_Drag != new Point(-1, -1))
                    {
                        double dy1 = movePoint.Y - downPoint_Drag.Y, x = -1, dx1 = movePoint.X - downPoint_Drag.X, y = -1;
                        downPoint_Drag = movePoint;
                        if (x == -1)
                            x = Canvas.GetLeft(dic[id_Drag].canvas) + dx1;
                        if (y == -1)
                            y = Canvas.GetTop(dic[id_Drag].canvas) + dy1;
                        Canvas.SetLeft(dic[id_Drag].canvas, x);
                        Canvas.SetTop(dic[id_Drag].canvas, y);
                    }
        }
        catch
        {
            MouseEventArgs ee = new MouseEventArgs((MouseDevice)e.Device, 10);
            Desktop_Canvas_MouseLeave(null, ee);
        }
    }

    private void Desktop_Canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        try
        {
  downPoint_Drag  = new Point(-1, -1);
            id_Drag =-1;
            flag_Drag = false;
        }
        catch
        {
            MouseEventArgs ee = new MouseEventArgs((MouseDevice)e.Device, 10);
            Desktop_Canvas_MouseLeave(null, ee);
        }
    }

    private void Desktop_Canvas_MouseLeave(object sender, MouseEventArgs e)
    {
        MouseButtonEventArgs ee = new MouseButtonEventArgs((MouseDevice)e.Device, 10, MouseButton.Left);
            Desktop_Canvas_MouseLeftButtonUp(null, ee);
    }

    void canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {            
        downPoint_Drag = e.GetPosition(Desktop_Canvas);
        int hoverId = HoverWin(downPoint_Drag);
        flag_Drag = true;
        id_Drag = hoverId;
        dic[id_Drag].downpoint = new Point(downPoint_Drag.X, downPoint_Drag.Y);
    }

    private int HoverWin(Point p)
    {
        foreach (int i in dic.Keys)
        {
                if (dic[i].canvas.IsMouseOver)
                    return i;
        }
        return -1;
    }

关于wpf - 在 WPF 中模拟拖/放事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15130712/

相关文章:

javascript - 为什么 ondrop 不起作用?

javascript - 使用 react-beautiful-dnd 嵌套拖放

c# - 在管理员帐户中运行 Visual Studio 时无法触发拖放事件

wpf - 如何将 System.Windows.Media.DrawingImage 转换为 Stream?

c# - 合并字典中的 WPF 覆盖样式

c# - 如何在 Window 启动 C# 时将 wpf 应用程序移动到最小化托盘?

javascript - HTML5 拖放——setDragImage() 有哪些事件可用?

c# - 在 WPF 的代码隐藏中为 ListBox 创建 ItemTemplate - 第 II 部分

c# - 将 WCF 数据服务与 Windows 应用程序结合使用

Java 拖放和可克隆