MouseDown事件将光标移动到控件的左上角

本文关键字:控件 左上角 移动 事件 光标 MouseDown | 更新日期: 2023-09-27 17:52:39

我有一个简单的UserControl放在画布上的窗口控件。我为MouseDown、MouseUp和MouseMove附加了事件,以便在Canvas周围拖动控件。我已经尝试使用PreviewMouseDown, PreviewMouseUp和PreviewMouseMove,以及,但得到相同的结果。

这部分工作,但有一个令人讨厌的行为的UserControl移动,这样的左上角(点0,0)被移动到光标的位置,只要我点击UserControl。下面的代码仅限于我认为对这个问题至关重要的部分。

Window.xaml

<Window x:Class="DragNDropBetweenControls.MainWindow"
        xmlns:local="clr-namespace:DragNDropBetweenControls"
        Title="MainWindow" MinHeight="350" MinWidth="525">
    <Canvas>
        <local:UserControl2/>
    </Canvas>
</Window>

UserControl.xaml

 <UserControl x:Class="DragNDropBetweenControls.UserControl2" >
     <Canvas Background="AntiqueWhite">
         <Grid Height="25" Width="100" AllowDrop="True" Name="B2"
             MouseDown="B2_OnMouseDown"
             MouseUp="B2_OnMouseUp"
             MouseMove="ui2_MouseMove" >
             <Rectangle Fill="Aqua" />
             <TextBlock Text="Move Control" VerticalAlignment="Center" HorizontalAlignment="Center" />
         </Grid>
     </Canvas>
</UserControl>

UserControl.xaml.cs

public partial class UserControl2 : UserControl
{
    private bool IsMouseDown = false;
    public UserControl2()
    {
        InitializeComponent();
    }
    private void B2_OnMouseDown(object sender, MouseButtonEventArgs e)
    {
        UIElement ui = (UIElement)sender;
        IsMouseDown = true;
        ui.CaptureMouse();
    }
            void ui2_MouseMove (object sender, MouseEventArgs e)
    {
        Grid b = (Grid)sender;
        if (!IsMouseDown)
            return;
        UIElement parent = (UIElement)b.Parent;
        Canvas.SetLeft(b, e.GetPosition(parent).X);
        Canvas.SetTop(b, e.GetPosition(parent).Y);
        e.Handled = true;
    }
    private void B2_OnMouseUp(object sender, MouseButtonEventArgs e)
    {
        IsMouseDown = false;
        UIElement ui = (UIElement)sender;
        ui.ReleaseMouseCapture();
        e.Handled = true;
    }
}

所以我想要的是光标和控件在点击和整个移动过程中保持在相同的位置。

非常感谢你的想法/帮助!

MouseDown事件将光标移动到控件的左上角

这可能有帮助:

public partial class UserControl2 : UserControl
{
    bool _pinned;
    Point _offset;
    public UserControl2()
    {
        InitializeComponent();
    }
    private void B2_OnMouseDown(object sender, MouseButtonEventArgs e)
    {
        var control = (UIElement)sender;
        control.CaptureMouse();
        _offset = e.GetPosition(control);
        _pinned = true;
    }
    private void ui2_MouseMove(object sender, MouseEventArgs e)
    {
        if (!_pinned)
            return;
        var control = (FrameworkElement)sender;
        var parent = (FrameworkElement)control.Parent;
        Canvas.SetLeft(control, e.GetPosition(parent).X - _offset.X);
        Canvas.SetTop(control, e.GetPosition(parent).Y - _offset.Y);
        e.Handled = true;
    }
    private void B2_OnMouseUp(object sender, MouseButtonEventArgs e)
    {
        _pinned = false;
        var control = (UIElement)sender;
        control.ReleaseMouseCapture();
        e.Handled = true;
    }
}

免责声明:我甚至没有尝试编译它。很抱歉,但希望你能明白。

这将工作我确定:

     private bool IsMouseDown = false;
        Point CPos;
private void B2_OnMouseDown(object sender, MouseButtonEventArgs e)
{
    UIElement ui = (UIElement)sender;
    IsMouseDown = true;
    ui.CaptureMouse();
    CPos = e.GetPosition(ui);
}
        void ui2_MouseMove (object sender, MouseEventArgs e)
{
    Grid b = (Grid)sender;
    if (!IsMouseDown)
        return;
    UIElement parent = (UIElement)b.Parent;
    Canvas.SetLeft(b, Mouse.GetPosition(parent).X - CPos.X);
    Canvas.SetTop(b, Mouse.GetPosition(parent).Y - CPos.Y);
    e.Handled = true;
}
private void B2_OnMouseUp(object sender, MouseButtonEventArgs e)
{
    IsMouseDown = false;
    UIElement ui = (UIElement)sender;
    ui.ReleaseMouseCapture();
    e.Handled = true;
}