如何使用c#在wpf中通过鼠标事件在运行时移动动态标签?

时间:2016-06-15 04:55:01

标签: c# wpf

我想通过使用Mouse_Move / Mouse_Down事件来移动标签。

我试着这样做:

 private void control_MouseDown(object sender, MouseButtonEventArgs e)
 {
     Label l = e.Source as Label;
     if (l != null)
     {
         l.CaptureMouse();
         moving = true;
         PositionInLabel = e.GetPosition(l);
     }
 }

 private void control_MouseMove(object sender, MouseEventArgs e)
 {
     if (moving)
     {
         Point p = e.GetPosition(null);
         DeltaX = p.X - BasePoint.X - PositionInLabel.X;
         DeltaY = p.Y - BasePoint.Y - PositionInLabel.Y;
         RaisePropertyChanged("XPosition");
         RaisePropertyChanged("YPosition");
     }
}

3 个答案:

答案 0 :(得分:0)

containerCanvas成为包含标签的Canvas;然后你可以利用_MouseMove(object sender, MouseEventArgs e)移动标签和鼠标指针:

Xaml代码:

 <Canvas Name="containerCanvas" MouseMove="containerCanvas_MouseMove" Background="Aqua" Width="525" Height="350" >
      <Label Name="floatingLabel" Height="28" Width="161" Background="AliceBlue"></Label>
 </Canvas>

C#代码:

private void containerCanvas_MouseMove(object sender, MouseEventArgs e)
{
    Point p = Mouse.GetPosition(Application.Current.MainWindow);
    floatingLabel.Margin = new Thickness(p.X, p.Y, 0, 0); 
}

答案 1 :(得分:0)

我做了一个示例代码。检查一下。它应该工作。

XAML:

<UserControl HorizontalAlignment="Left" x:Class="WPFDiagramDesignerControl.Components.TestApp"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="100" Width="100" IsEnabled="True">
<Grid >
<Canvas x:Name="MyDesigner">
<TextBox x:Name="txtBox" IsEnabled="True"   Background="AntiqueWhite"  Margin="10,10,10,10" TextWrapping="Wrap"> </TextBox>
</Canvas>
</Grid>

代码背后:

public TestApp()
{
    InitializeComponent();
    txtBox.MouseDoubleClick+=new MouseButtonEventHandler(control_MouseDoubleClick);
    txtBox.MouseMove+=new MouseEventHandler(control_MouseMove);
    txtBox.PreviewMouseDown+=new MouseButtonEventHandler(control_PreviewMouseDown);
    txtBox.PreviewMouseUp+=new MouseButtonEventHandler(control_PreviewMouseUp);
    txtBox.Cursor = Cursors.SizeAll;
}

private void control_MouseMove(object sender, RoutedEventArgs e)
{
    if (isClicked)
    {
        Point mousePos = Mouse.GetPosition(parentCanvas);

        parentItem = this.Parent as DesignerItem;
        parentCanvas = parentItem.Parent as DesignerCanvas;
        Point relativePosition = Mouse.GetPosition(parentCanvas);
        DesignerCanvas.SetLeft(this, DesignerCanvas.GetLeft(this) -  (startPoint.X - mousePos.X));
        DesignerCanvas.SetTop(this, DesignerCanvas.GetTop(this) - (startPoint.Y - mousePos.Y));
    }

}


private void control_PreviewMouseDown(object sender, RoutedEventArgs e)
{
    if (!isClicked)
    {
        isClicked = true;
        parentItem = this.Parent as DesignerItem;
        parentCanvas = parentItem.Parent as DesignerCanvas;
        startPoint = Mouse.GetPosition(parentCanvas);
    }
}


private void control_PreviewMouseUp(object sender, RoutedEventArgs e)
{
    isClicked = false;
}

答案 2 :(得分:0)

假设您的标签位于画布上:

    public MainWindow()
    {
        InitializeComponent();
        this.label.MouseLeftButtonDown += control_MouseLeftButtonDown;
        this.label.MouseMove += control_MouseMove;
        this.label.MouseLeftButtonUp += control_MouseLeftButtonUp;
    }

    // Keep track of the Canvas where this element is placed.
    private Canvas canvas;
    // Keep track of when the element is being dragged.
    private bool isDragging = false;
    // When the element is clicked, record the exact position
    // where the click is made.
    private Point mouseOffset;
    private void control_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        Label l = e.Source as Label;
        if (l == null)
            return;
        // Find the Canvas.
        if (canvas == null)
            canvas = (Canvas)VisualTreeHelper.GetParent(l);
        // Dragging mode begins.
        isDragging = true;
        // Get the position of the click relative to the element
        // (so the top-left corner of the element is (0,0).
        mouseOffset = e.GetPosition(l);
        // Capture the mouse. This way you'll keep receiving
        // the MouseMove event even if the user jerks the mouse
        // off the element.
        l.CaptureMouse();
    }

    private void control_MouseMove(object sender, MouseEventArgs e)
    {
        Label l = e.Source as Label;
        if (l == null)
            return;
        if (isDragging)
        {
            // Get the position of the element relative to the Canvas.
            Point point = e.GetPosition(canvas);
            // Move the element.
            l.SetValue(Canvas.TopProperty, point.Y - mouseOffset.Y);
            l.SetValue(Canvas.LeftProperty, point.X - mouseOffset.X);
        }
    }

    private void control_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        Label l = e.Source as Label;
        if (l == null)
            return;
        if (isDragging)
        {
            l.ReleaseMouseCapture();
            isDragging = false;
        }
    }