Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi all,

iam able to transform single shape at a time but how can we transform multiple shapes at the same time. I hope we can do this in WPF.

I have two buttons, first button will create rectangle shape, second button i want to transform the shape from one location to another location;

Code:
void Button1_Click(object sender, RoutedEventArgs e)
{		
  Point pt=new Point(100,90);//some point
  rect =new Rectangle
    {
	Stroke=Brushes.Blue,
	StrokeThickness=1
    };		
	rect.Width=90;
	rect.Height=60;		
	rect.Fill =Brushes.Coral;		
	Canvas.SetLeft(rect,pt.X);
	Canvas.SetTop(rect,pt.Y);				
	canvas1.Children.Add(rect);	      
}

void Button2_Click(object sender, RoutedEventArgs e)
{				
    GeneralTransform myTransform=rect.TransformToDescendant (rect);               
    Point pt=myTransform .Transform (new Point(100,50));
    Canvas.SetLeft(rect,pt.X-rect.Width/2);
    Canvas.SetTop(rect,pt.Y-rect.Height/2);                 
}


each time when i click on button1 rectangle shape is created. and when i click on button 2 only one rectangle is transforming.

I want multiple rectangles transform to another location.
plz some one give me idea to transform multiple shapes at a time.

Thanks,
Posted
Updated 2-Nov-11 23:54pm
v2

1 solution

First, define a List destined to hold all the rectangles you create on Button1 press


List<rectangle> rectList = new List<rectangle>();
</rectangle></rectangle>


Don't copy the last two /rectangle with the brackets, I gave up trying to make the editor display it appropriately...

Then:

private void Button1_Click(object sender, RoutedEventArgs e)
{
    Point pt = new Point(100, 90);//some point
    Rectangle rect = new Rectangle
    {
       Stroke = Brushes.Blue,
       StrokeThickness = 1
    };
   rect.Width = 90;
   rect.Height = 60;
   rect.Fill = Brushes.Coral;
   Canvas.SetLeft(rect, pt.X);
   Canvas.SetTop(rect, pt.Y);
   canvas1.Children.Add(rect);

   rectList.Add(rect);
}

private void Button2_Click(object sender, RoutedEventArgs e)
{
    foreach (Rectangle rect in rectList)
    {
        
            GeneralTransform myTransform = rect.TransformToDescendant(rect);
            Point pt = myTransform.Transform(new Point(100, 50));
            Canvas.SetLeft(rect, pt.X - rect.Width / 2);
            Canvas.SetTop(rect, pt.Y - rect.Height / 2);
        
    }
}
 
Share this answer
 
v7

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900