【问题标题】:Get the mouse position during drag and drop在拖放过程中获取鼠标位置
【发布时间】:2010-10-29 06:08:29
【问题描述】:

有谁知道如何在 WPF 中进行拖放操作时获得正确的鼠标位置?我用过Mouse.GetPosition(),但是返回的值不正确。

【问题讨论】:

    标签: wpf drag-and-drop mouse position


    【解决方案1】:

    没关系,我已经找到了解决方案。使用 DragEventArgs.GetPosition() 返回正确的位置。

    【讨论】:

    • Hi @J W GetPosition() 将 IInputElement 作为参数。你给这个函数传递了什么?
    • @AFgone:我使用 DragEventArgs.GetPosition(this) 或其他任何事件绑定的实例来完成。结果按预期工作。
    • 很抱歉复活了这个旧线程,但是:当我拖过 AllowDrop = false 的部分时,我没有得到 DragOver -Event 这是唯一给我的DragEventArgs
    【解决方案2】:

    DragOver 处理程序是针对一般情况的解决方案。但是,如果您在光标不在可放置表面时需要精确的光标点,您可以使用下面的 GetCurrentCursorPosition 方法。我推荐了Jignesh Beladiya's post

    using System;
    using System.Runtime.InteropServices;
    using System.Windows;
    using System.Windows.Media;
    
    public static class CursorHelper
    {
        [StructLayout(LayoutKind.Sequential)]
        struct Win32Point
        {
            public Int32 X;
            public Int32 Y;
        };
    
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool GetCursorPos(ref Win32Point pt);
    
        public static Point GetCurrentCursorPosition(Visual relativeTo)
        {
            Win32Point w32Mouse = new Win32Point();
            GetCursorPos(ref w32Mouse);
            return relativeTo.PointFromScreen(new Point(w32Mouse.X, w32Mouse.Y));
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多