Razan Paul Blog

Explaining thoughts and findings is a great way to learn

Creating marquee/scrolling text in WPF

mar1

A WPF marquee is a scrolling piece of text displayed either horizontally across or vertically down your container. Here i show you how to make a scrolling marquee using Canvas and TextBlock. I will show only left to right in code. However, you can achieve right to left or vertical marquee text just by changing some portion of code using the same technique.

The following three techniques are used to achieve marquee animation:

  • Negative Positioning in canvas: If we set negative value to any of the positioning property of Canvas: Left, Right, Top and Bottom for an UIelement , Canvas draws that Uielement outside of Canvas area.
  • ClipToBounds property of canvas: if you set the value of the ClipToBounds property to true, Canvas will clip its children when they go outside of Canvas.
  • Changing Position of Uielement continuously: By changing continuously any of the canvas positioning property Left, right, Top, Bottom we can get moving text.

How is Left to right marquee text implemented in this sample?

Here a canvas is used as the container of marquee text. To move the text outside of canvas negative Left property of canvas is used. However, if someone uses negative left property of canvas, it will go outside of its parent container but will still remain visible outside of the parent container. To make marquee text invisible outside of canvas CliptoBound property is used. Then using double animation left property of canvas for that element is being changed continuously to get moving text. The whole thing is done using the following code.

   1: void Window1_Loaded(object sender, RoutedEventArgs e)
   2: {
   3:     DoubleAnimation doubleAnimation = new DoubleAnimation();
   4:     doubleAnimation.From = -tbmarquee.ActualWidth;
   5:     doubleAnimation.To = canMain.ActualWidth;
   6:     doubleAnimation.RepeatBehavior = RepeatBehavior.Forever;
   7:     doubleAnimation.Duration = new Duration(TimeSpan.Parse("0:0:10"));
   8:     tbmarquee.BeginAnimation(Canvas.LeftProperty, doubleAnimation);
   9: }

For Left to right marquee: use Left Property of Canvas

For right to left marquee: use Right Property of Canvas

For upward marquee: use Bottom Property of Canvas

For Downward marquee: use Top Property of Canvas

The used XAML for this is in the following

   1: <Window x:Class="MarqueText.Window1"
   2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   4:     Title="Window1" Height="500" Width="700" >
   5:     <Canvas Background="White" >
   6:         <Canvas Margin="50" Canvas.Left="150" Canvas.Top="100" ClipToBounds="True" Name="canMain" Background="Red"   Height="100" Width="300"  >
   7:             <TextBlock Margin="0,40" FontSize="25" Name="tbmarquee">Bangladesh</TextBlock>
   8:         </Canvas>
   9:     </Canvas>
  10: </Window>

You can download the sample code from here. Hope this will save some of your time.

Posted: Oct 01 2009, 02:13 AM by RazanPaul | with 5 comment(s)
Filed under: , ,

Comments

Martin de Jong said:

So little code, and still good looking. Nice job. Maybe you have a control version of it too?

# December 9, 2009 4:21 AM

RazanPaul said:

Yes, i have a control version. I will post it to Code Project shortly. Thanks..............

# December 10, 2009 6:14 AM

Rob said:

Awesome article!  You've reduced the complexity to the minimum needed for functioning.  Well done!!!

# January 19, 2010 4:56 PM

Rob said:

One quick point: for scrolling toward the right, you not only use the RightProperty of Canvas, but you must also inverse both the To and From values (compared to the LeftPropoerty as shown in the sample code).  I haven't experimented with up/down yet but I suspect you need to use something other than ActualWidth...

# January 19, 2010 5:18 PM

RazanPaul said:

For Left, right, Up and down text marquee, you can look at www.codeproject.com/.../WPFAnimation1.aspx

# January 20, 2010 8:27 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)