How can I make a Rectangle on a canvas which is draggable?

前端 未结 1 1005
灰色年华
灰色年华 2021-01-03 05:15

I have these three functions to trigger the events. I already have a static version of my needs, but I need a dynamically version of it.

    bool ca         


        
相关标签:
1条回答
  • 2021-01-03 05:48

    I'm sure this sample code will help you.

    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;
        }
    
    0 讨论(0)
提交回复
热议问题