移动一个矩形围绕画布 [英] Move a rectangle around a canvas

查看:204
本文介绍了移动一个矩形围绕画布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我与周围的控制应用程序的中间的画布。
我有临危点并以列表保存它们的插座。

I have a canvas in the middle of my application with controls around it. I have a socket that recieves Points and saves them in a list.

我画小4x4的矩形的画布点在我的列表中的号码上。
说theres 4分..那里有4矩形。

I draw small 4x4 rectangles on the canvas for the number of points in my list.. say theres 4 points.. theres 4 rectangles.

我希望能够当点,更改代码,移动矩形。
这是可能没有故事板​​或任何动画类? ?我怎么会去这样做什么,我需要

I want to be able to move the rectangles when the points change with code. is this possible without storyboards or any 'animation' class? and how would I go about doing what I need?

我曾尝试:

        'cMap.Children.Remove(r)

        'Dim nr As Rectangle = New Rectangle() With {.Width = 4, .Height = 4, .Name = "r" & P.Name, .Fill = Brushes.Red}
        'r.RenderTransform = New TranslateTransform(P.Position.X, P.Position.Y)

        Canvas.SetTop(cMap.Children(cMap.Children.IndexOf(r)), (512 / 2) + P.Position.Y)
        Canvas.SetLeft(cMap.Children(cMap.Children.IndexOf(r)), (512 / 2) + P.Position.X)
        'nr.SetValue(Canvas.TopProperty, (512 / 2) + P.Position.Y)
        'nr.SetValue(Canvas.LeftProperty, (512 / 2) + P.Position.X) ' P.Position.X)
        'cMap.Children.Add(nr)

所有这些,但没有做的矩形移动。
是的,我已经确保数据被改变。

all of those but none make the rectangles move. and yes I have made sure that the data is changing.

感谢了。

推荐答案

我觉得更漂亮的解决方案都可以通过Canvas.Left和Canvas.Top附加属性绑定到的ObservableCollection <点>,但当你问一个老式的WinForms风格在这里解决方案,您有什么,做什么,我想你需要(我道歉,在C#编写本):

I think more nifty solutions are available through binding the Canvas.Left and Canvas.Top attached properties to a ObservableCollection<Point>, but as you asked for an old fashioned WinForms Style solution here you have something that does what I think you need (My apologies for writing this in C#):

XAML:

<Window x:Class="MovingPointsSpike.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="650" Width="525"
        >
    <StackPanel>
        <Border BorderThickness="1" BorderBrush="Gray">
            <Canvas Name="PointCanvas" Width="500" Height="500"/>
        </Border>
        <Button Name="Move" Click="Move_Click">Move Random Point</Button>
        <Button Name="Add" Click="Add_Click">Add Point</Button>
        <Button Name="Remove" Click="Remove_Click">Remove Random Point</Button>
    </StackPanel>
</Window>



后面的代码:

Code behind:

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace MovingPointsSpike
{

    public partial class MainWindow : Window
    {
        private List<Point> m_Points;
        private Random m_Random;

        public MainWindow()
        {
            InitializeComponent();
            m_Points=new List<Point>();
            m_Random=new Random();
        }

        private void Move_Click(object sender, RoutedEventArgs e)
        {
            Rectangle rectangle;
            Point newPoint;
            int index = GetRandomIndex();
            newPoint = GetRandomPoint();

            rectangle =(Rectangle)PointCanvas.Children[index];
            if (index == -1) return;
            Canvas.SetTop(rectangle, newPoint.Y);
            Canvas.SetLeft(rectangle, newPoint.X);
        }

        private void Add_Click(object sender, RoutedEventArgs e)
        {
            Point newPoint;
            Rectangle rectangle;

            newPoint = GetRandomPoint();
            rectangle = new Rectangle {Width = 4, Height = 4, Fill = Brushes.Red};
            m_Points.Add(newPoint);
            PointCanvas.Children.Add(rectangle);
            Canvas.SetTop(rectangle,newPoint.Y);
            Canvas.SetLeft(rectangle,newPoint.X);
        }

        private Point GetRandomPoint()
        {
            int x;
            int y;
            x = m_Random.Next(10, 490);
            y = m_Random.Next(10, 490);
            return new Point(x,y);
        }

        private void Remove_Click(object sender, RoutedEventArgs e)
        {
            int index = GetRandomIndex();
            if (index==-1)return;

            PointCanvas.Children.RemoveAt(index);
            m_Points.RemoveAt(index);

        }

        private int GetRandomIndex()
        {
            int index;
            if (m_Points.Count==0) return -1;
            index = m_Random.Next(m_Points.Count - 1);
            return index;
        }
    }
}

这篇关于移动一个矩形围绕画布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆