【发布时间】:2010-10-29 06:08:29
【问题描述】:
有谁知道如何在 WPF 中进行拖放操作时获得正确的鼠标位置?我用过Mouse.GetPosition(),但是返回的值不正确。
【问题讨论】:
标签: wpf drag-and-drop mouse position
有谁知道如何在 WPF 中进行拖放操作时获得正确的鼠标位置?我用过Mouse.GetPosition(),但是返回的值不正确。
【问题讨论】:
标签: wpf drag-and-drop mouse position
没关系,我已经找到了解决方案。使用 DragEventArgs.GetPosition() 返回正确的位置。
【讨论】:
AllowDrop = false 的部分时,我没有得到 DragOver -Event 这是唯一给我的DragEventArgs
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));
}
}
【讨论】: