2016-05-11 52 views
1

我有这三个功能来触发事件。我已经有我的需求的静态版本,但我需要它的一个动态版本。如何在可拖动的画布上制作矩形?

bool captured = false; 
    double x_shape, x_canvas, y_shape, y_canvas; 
    UIElement source = null; 

    private void MouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
    { 
     source = (UIElement)sender; 
     Mouse.Capture(source); 
     captured = true; 
     x_shape = Canvas.GetLeft(source); 
     x_canvas = e.GetPosition(canvasPreview).X; 
     y_shape = Canvas.GetTop(source); 
     y_canvas = e.GetPosition(canvasPreview).Y; 
    } 

    private void MouseMove(object sender, MouseEventArgs e) 
    { 
     //MessageBox.Show("test"); 
     if (captured) 
     { 
      double x = e.GetPosition(canvasPreview).X; 
      double y = e.GetPosition(canvasPreview).Y; 
      x_shape += x - x_canvas; 
      Canvas.SetLeft(source, x_shape); 
      x_canvas = x; 
      y_shape += y - y_canvas; 
      Canvas.SetTop(source, y_shape); 
      y_canvas = y; 
     } 
    } 

    private void MouseLeftButtonUp(object sender, MouseButtonEventArgs e) 
    { 
     Mouse.Capture(null); 
     captured = false; 
    } 

我已在WPF画布称为“canvasPreview”,我想补充的矩形(目前在静态版本我使用的是椭圆)到画布上,它必须是可拖动上面的功能。它已经在工作,但它必须是动态的。

我希望你能帮助我,谢谢你提前!

+0

它是如何工作的,如果不是动态?你究竟意味着什么,必须是动态的?形状是否被拖拽? – Joe

+0

对不起。静态版本工作,我的意思是与静态工作是,在WPF中创建的椭圆是可拖动的,但当我尝试生成一个椭圆(应该是一个矩形),它不是可拖动的,但函数被调用。 –

+0

画布中的功能是?如果你放置一个断点并开始尝试拖动矩形,那么在MouseLeftButtonDown中设置的“源”是什么? – Joe

回答

1

我相信这个示例代码会帮助你。

XAML:

<Grid Margin="12"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="auto"/> 
     <RowDefinition Height="*"/> 
    </Grid.RowDefinitions> 

    <StackPanel Grid.Row="0" Orientation="Horizontal"> 
     <Button x:Name="addRectangleButton" Content="Add Rectngle" Click="addRectangleButton_Click"/> 
    </StackPanel> 

    <Canvas Grid.Row="1" x:Name="canvas" Margin="0,12,0,0"> 
     <Rectangle x:Name="rectangle" Width="100" Height="50" Fill="RoyalBlue" MouseDown="rectangle_MouseDown" MouseMove="rectangle_MouseMove" MouseUp="rectangle_MouseUp" Canvas.Left="0" Canvas.Top="0"/> 
    </Canvas> 
</Grid> 

C#:

bool drag = false; 
    Point startPoint; 

    public MainWindow() 
    { 
     InitializeComponent(); 
    } 

    // this creates and adds rectangles dynamically 
    private void addRectangleButton_Click(object sender, RoutedEventArgs e) 
    { 
     // create new Rectangle 
     Rectangle rectangle = new Rectangle(); 
     // assign properties 
     rectangle.Width = 100; 
     rectangle.Height = 50; 
     rectangle.Fill = new SolidColorBrush(Colors.RoyalBlue); 
     // assign handlers 
     rectangle.MouseDown += rectangle_MouseDown; 
     rectangle.MouseMove += rectangle_MouseMove; 
     rectangle.MouseUp += rectangle_MouseUp; 
     // set default position 
     Canvas.SetLeft(rectangle, 0); 
     Canvas.SetTop(rectangle, 0); 
     // add it to canvas 
     canvas.Children.Add(rectangle); 
    } 

    private void rectangle_MouseDown(object sender, MouseButtonEventArgs e) 
    { 
     // start dragging 
     drag = true; 
     // save start point of dragging 
     startPoint = Mouse.GetPosition(canvas); 
    } 

    private void rectangle_MouseMove(object sender, MouseEventArgs e) 
    { 
     // if dragging, then adjust rectangle position based on mouse movement 
     if (drag) 
     { 
      Rectangle draggedRectangle = sender as Rectangle; 
      Point newPoint = Mouse.GetPosition(canvas); 
      double left = Canvas.GetLeft(draggedRectangle); 
      double top = Canvas.GetTop(draggedRectangle); 
      Canvas.SetLeft(draggedRectangle, left + (newPoint.X - startPoint.X)); 
      Canvas.SetTop(draggedRectangle, top + (newPoint.Y - startPoint.Y)); 

      startPoint = newPoint; 
     } 
    } 

    private void rectangle_MouseUp(object sender, MouseButtonEventArgs e) 
    { 
     // stop dragging 
     drag = false; 
    } 
+1

谢谢!问题已经解决了!它是SetLeft和SetTop属性。 –

+0

非常好,我希望这段代码也能帮上忙。 –

+0

祝你好运,祝你成功。 –