C# WPF dragdrop控件光标将退出该控件

C# WPF dragdrop控件光标将退出该控件,c#,wpf,C#,Wpf,我已经创建了自己的可拖动控件。拖动非常简单: bool moving = false; Point click = new Point(0, 0); private void _MouseDown(object sender, MouseButtonEventArgs e) { moving = true; click = Mouse.GetPosition(this); } private void _MouseUp(object sender, MouseButto

我已经创建了自己的可拖动控件。拖动非常简单:

    bool moving = false; Point click = new Point(0, 0);

private void _MouseDown(object sender, MouseButtonEventArgs e)
{
    moving = true;
    click = Mouse.GetPosition(this);
}
private void _MouseUp(object sender, MouseButtonEventArgs e) { moving = false; }
private void _MouseMove(object sender, MouseEventArgs e)
{
    if (moving == true)
    {
        Point po = Mouse.GetPosition(this);
        this.Margin = new Thickness(this.Margin.Left + (po.X - click.X), this.Margin.Top + (po.Y - click.Y), 0, 0);
    }
}
我的问题是,如果拖得太快,光标就会“逃脱”我的控制。原因很明显,但是如何解决这个问题并不太明显,因为我无法轻松订阅窗口中其他每个控件的mousemove,而且我的控件很小(大约35,15 px),所以经常发生这种情况。我认为,如果我可以轻松地强制鼠标光标停留在控件中,这将是一个解决方案(尽管不理想)。 那么,解决这个问题的最佳方法是什么?professinoal控件如何处理此问题


另外,我正在学习WPF,所以我可能做错了一些事情

您的光标在快速移动时离开用户控件,MouseMove事件将不再触发


正如作者在使用周围画布的MouseMove事件时的评论中所说,应该会有所帮助。

您的光标在快速移动时离开用户控件,MouseMove事件将不再被触发


正如作者在使用周围画布的MouseMove事件时的评论所说的,应该会有所帮助。

我已经弄明白了,使用计时器非常简单

    bool moving = false; Point click = new Point(0, 0); 
System.Timers.Timer _MOVER = new System.Timers.Timer();

public PersonControl() 
{
    InitializeComponent();



    _MOVER.Elapsed += new System.Timers.ElapsedEventHandler((o, v) => { Dispatcher.Invoke(Move); });
    _MOVER.Enabled = true;
    _MOVER.Interval = 10;
}

private void _MouseDown(object sender, MouseButtonEventArgs e)
{
    moving = true;
    click = Mouse.GetPosition(this);
    Canvas.SetZIndex(this, 100);
    _MOVER.Start();
}
private void _MouseUp(object sender, MouseButtonEventArgs e) 
{ 
    moving = false;
    Canvas.SetZIndex(this, 0);
    _MOVER.Stop();
}
private void Move()
{
    if (moving == true)
    {
        Point po = Mouse.GetPosition(this);
        this.Margin = new Thickness(this.Margin.Left + (po.X - click.X), this.Margin.Top + (po.Y - click.Y), 0, 0);
    }
}

我已经弄明白了,很简单,用定时器

    bool moving = false; Point click = new Point(0, 0); 
System.Timers.Timer _MOVER = new System.Timers.Timer();

public PersonControl() 
{
    InitializeComponent();



    _MOVER.Elapsed += new System.Timers.ElapsedEventHandler((o, v) => { Dispatcher.Invoke(Move); });
    _MOVER.Enabled = true;
    _MOVER.Interval = 10;
}

private void _MouseDown(object sender, MouseButtonEventArgs e)
{
    moving = true;
    click = Mouse.GetPosition(this);
    Canvas.SetZIndex(this, 100);
    _MOVER.Start();
}
private void _MouseUp(object sender, MouseButtonEventArgs e) 
{ 
    moving = false;
    Canvas.SetZIndex(this, 0);
    _MOVER.Stop();
}
private void Move()
{
    if (moving == true)
    {
        Point po = Mouse.GetPosition(this);
        this.Margin = new Thickness(this.Margin.Left + (po.X - click.X), this.Margin.Top + (po.Y - click.Y), 0, 0);
    }
}

是的,我知道这一点,在winforms中我会订阅。家长的mousemove,它会工作,但由于WPF的性质,它不确定家长是否比控件本身大。是的,我知道这一点,在winforms中我会订阅。家长的mousemove,它会工作,但由于WPF的性质,不确定父项是否大于控件本身。。