2016-06-15 6 views
1

Mouse_Move/Mouse_Downイベントを使用してラベルを移動したいとします。cpを使用してwpfでマウスイベントによって実行時に動的ラベルを移動する方法は?

私はこのようにそれを実行しようとしました:

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"); 
    } 
} 

答えて

-1

あなたのラベルがキャンバスの上にあると仮定します

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; 
     } 
    } 
+0

ありがとうニュートン・ゾウ...うまく働いています。 – Basha

0

containerCanvasはラベルが含まれているキャンバスとします。あなたは、あなたのマウスポインタと一緒にラベルを移動する_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); 
} 
+0

それは働いていない..私がする必要があります実行時にマウスのevntを使って動的ラベルを移動します。 – Basha

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; 
} 
関連する問題