page 1 of 1
Comments: 12 | Views: 2647

how to make Rectangle movement( left to right)  animation with wpf c# code

thanks

evildictaitor
evildictaitor
if( !succeed( try() ) ) { while(true) try(); }
When it is at the left, it's x position is 0. When it is at right, it's x position is (Container.Width - item.Width).

Just animation the two together. There's more than enough stuff like this on the internet.
erik_
erik_
Tablet Power
Sample from msdn:

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Shapes;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace SDKSample
{

    public class RectangleOpacityFadeExample : Page
    {
        private Storyboard myStoryboard;

        public RectangleOpacityFadeExample()
        {
            NameScope.SetNameScope(this, new NameScope());

            this.WindowTitle = "Fading Rectangle Example";
            StackPanel myPanel = new StackPanel();
            myPanel.Margin = new Thickness(10);

            Rectangle myRectangle = new Rectangle();
            myRectangle.Name = "myRectangle";
            this.RegisterName(myRectangle.Name, myRectangle);
            myRectangle.Width = 100;
            myRectangle.Height = 100;
            myRectangle.Fill = Brushes.Blue;

            DoubleAnimation myDoubleAnimation = new DoubleAnimation();
            myDoubleAnimation.From = 1.0;
            myDoubleAnimation.To = 0.0;
            myDoubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(5));
            myDoubleAnimation.AutoReverse = true;
            myDoubleAnimation.RepeatBehavior = RepeatBehavior.Forever;

            myStoryboard = new Storyboard();
            myStoryboard.Children.Add(myDoubleAnimation);
            Storyboard.SetTargetName(myDoubleAnimation, myRectangle.Name);
            Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(Rectangle.OpacityProperty));

            // Use the Loaded event to start the Storyboard.
            myRectangle.Loaded += new RoutedEventHandler(myRectangleLoaded);  

            myPanel.Children.Add(myRectangle);
            this.Content = myPanel;
        }

        public void myRectangleLoaded(object sender, RoutedEventArgs e)
        {
            myStoryboard.Begin(this);
        }

    }
}


See http://msdn2.microsoft.com/en-us/library/ms752312(VS.85).aspx for more.

Modify it to target the position instead of Rectangle.OpacityProperty..

Let me know if you need a cut & paste sample +)
Lloyd_Humph
Lloyd_Humph
If Blackberrys are addictive cellphones, Channel9 is the ultimate addictive website.
I reckon this would be better placed in the Techoff Smiley
Lloyd_Humph
Lloyd_Humph
If Blackberrys are addictive cellphones, Channel9 is the ultimate addictive website.
CompGuy101 wrote:

vennag wrote:

how to change x position of Rectangle using this code ,
please give me the sample code

thanks



erik_ just gave you all the code you need.




*sigh*
Elements in WPF don't have a position property, or an "x" value directly.  Instead, if the element is child of a Canvas - several attached properties are utilized.  These are Canvas.Left, Canvas.Right, Canvas.Top, and Canvas.Bottom.  Using the animation code above, animate either the Canvas.Left or Canvas.Right attached property on the rectangle to move it.
evildictaitor
evildictaitor
if( !succeed( try() ) ) { while(true) try(); }
vennag wrote:
please give me the sample code


This is a friendly help forum, not a free code bonanza. People here will help you learn to write your own code. They won't write it for you.
I think nightski probably nailed it, but google suggests these examples of absolute positioning: 1 and 2.

I'm ignoring the request for C# instead of XAML since it's relatively easy to translate between the two. Also, my XAML targets Silverlight, but it's easy enough to change to WPF by changing the Canvas' xmlns attribute.

<?xml version="1.0"?>
<Canvas xmlns="http://schemas.microsoft.com/client/2007">
  <Rectangle Fill="Orange" Name="rectangle1" Width="50" Height="50">
    <Rectangle.Triggers>
      <EventTrigger RoutedEvent="Rectangle.Loaded">
        <EventTrigger.Actions>
          <BeginStoryboard>
            <Storyboard Storyboard.TargetProperty="(Canvas.Left)" Storyboard.TargetName="rectangle1">
              <DoubleAnimation By="250" Duration="0:0:1"/>
            </Storyboard>
          </BeginStoryboard>
        </EventTrigger.Actions>
      </EventTrigger>
    </Rectangle.Triggers>
  </Rectangle>
</Canvas>
ddl
ddl
hey
evildictaitor

he was n't asked your vista OS source which was developed by you:P
he just asked your help
if know answer please try to help other then Embarassed



===========================

ple try following code ::ven::Cool

============================


NameScope.SetNameScope(this, new NameScope());

RectangleGeometry myRectangleGeometry = new RectangleGeometry();

myRectangleGeometry.Rect = new Rect(0, 200, 100, 100);

// Assign the geometry a name so that

// it can be targeted by a Storyboard.

this.RegisterName(

"MyAnimatedRectangleGeometry", myRectangleGeometry);

Path myPath = new Path();

myPath.Fill = Brushes.LemonChiffon;

myPath.StrokeThickness = 1;

myPath.Stroke = Brushes.Black;

myPath.Data = myRectangleGeometry;

RectAnimation myRectAnimation = new RectAnimation();

myRectAnimation.Duration = TimeSpan.FromSeconds(2);

myRectAnimation.FillBehavior = FillBehavior.HoldEnd;

// Set the animation to repeat forever.

myRectAnimation.RepeatBehavior = RepeatBehavior.Forever;

// Set the From and To properties of the animation.

myRectAnimation.From = new Rect(0, 200, 100, 100);

myRectAnimation.To = new Rect(600, 50, 200, 50);

// Set the animation to target the Rect property

// of the object named "MyAnimatedRectangleGeometry."

Storyboard.SetTargetName(myRectAnimation, "MyAnimatedRectangleGeometry");

Storyboard.SetTargetProperty(

myRectAnimation, new PropertyPath(RectangleGeometry.RectProperty));

// Create a storyboard to apply the animation.

Storyboard ellipseStoryboard = new Storyboard();

ellipseStoryboard.Children.Add(myRectAnimation);

// Start the storyboard when the Path loads.

myPath.Loaded += delegate(object sender, RoutedEventArgs e)

{

ellipseStoryboard.Begin(this);

};

Canvas containerCanvas = new Canvas();

containerCanvas.Children.Add(myPath);

Content = containerCanvas;

evildictaitor
evildictaitor
if( !succeed( try() ) ) { while(true) try(); }
ddl wrote:
hey
evildictaitor

he was n't asked your vista OS source which was developed by you


I developed all of the Vista codebase? News to me.
Lloyd_Humph
Lloyd_Humph
If Blackberrys are addictive cellphones, Channel9 is the ultimate addictive website.
evildictaitor wrote:

ddl wrote:hey
evildictaitor

he was n't asked your vista OS source which was developed by you


I developed all of the Vista codebase? News to me.


In your sleep, durr?
page 1 of 1
Comments: 12 | Views: 2647
Microsoft Communities