我需要使用自定义光标进行拖放。最初,我使用了装饰器,但是现在我希望能够在窗口之间移动时甚至在只有一个窗口的情况下也能显示它,以便不要在窗口的边缘处剪切光标,因此我尝试使用答案here。
虽然我将光标移到该测试应用程序中唯一的Window窗口之外,但不会引发MouseMove
事件。当我将光标移到窗口内时,MouseMove
事件再次引发。
我尝试了this和this,但没有成功。我也读过the official documentation。
<Window x:Class="cs_wpf_test_9.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:cs_wpf_test_9"
mc:Ignorable="d"
Title="MainWindow" Height="127" Width="269"
Loaded="Window_Loaded"
MouseMove="Window_MouseMove">
<Label HorizontalAlignment="Center" VerticalAlignment="Center">Testing</Label>
</Window>
namespace cs_wpf_test_9
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public static Point GetMousePositionWindowsForms()
{
System.Drawing.Point point = System.Windows.Forms.Control.MousePosition;
return new Point(point.X, point.Y);
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Mouse.Capture(this);
}
internal void UpdateCursorPos()
{
Point p = GetMousePositionWindowsForms();
// Tracepoint here to show {p}
}
private void Window_MouseMove(object sender, MouseEventArgs e)
{
UpdateCursorPos();
}
}
}
期望:在“输出”面板中,每当鼠标移动时(包括鼠标移出窗口的时间),都应该有MouseMove事件处理程序的输出。
实际:仅当鼠标在窗口内移动时才调用事件处理程序。
更新:如果将Window上的AllowDrop设置为true,并且在MouseDown上我将其称为DoDragDrop,则尽管鼠标被强制捕获,但仅当鼠标光标位于Window上时,就会调用DragOver处理程序。使用UIElement的CaptureMouse方法。对我来说,使DragOver事件像上面解释的那样工作就足够了,因为我想要实现的是更好的拖放。我还尝试过:
Mouse.Capture(this, CaptureMode.Element);
CaptureMouse();
就在DoDragDrop调用之前,但是发生了同样的事情。