Advertisement
Guest User

Untitled

a guest
Oct 20th, 2014
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.63 KB | None | 0 0
  1. <Grid x:Name="LayoutRoot">
  2. <Canvas x:Name="canvas" Background="Transparent" Width="{Binding Width}" Height="{Binding Height}">
  3. <Canvas.Clip>
  4. <RectangleGeometry RadiusX="0" RadiusY="0" />
  5. </Canvas.Clip>
  6.  
  7.  
  8. <TextBlock x:Name="textBlock1"
  9. Height="{Binding Height}"
  10. Width="{Binding MWidth}"
  11. TextWrapping="{Binding MTextWrap}"
  12. Foreground="{Binding Foreground}"
  13. Text="{Binding Text}"
  14. TextTrimming="None"
  15. FontSize="{Binding MFontSize}"/>
  16. </Canvas>
  17. </Grid>
  18.  
  19. public partial class MarqueeTextBlock : UserControl
  20. {
  21. // storyboard
  22. Storyboard storyboard;
  23.  
  24. // offset when textBlock1 is long, because user want to see smooth scrolling
  25. private double offset =0;
  26.  
  27. // default offset
  28. private double defaultOffset =100;
  29.  
  30. // default velocity
  31. int velocity = 0;
  32.  
  33. // whether control has beed loaded
  34. // because it's when control is loaded do we start marquee
  35. bool isLoaded = false;
  36.  
  37. public MarqueeTextBlock()
  38. {
  39. InitializeComponent();
  40.  
  41. Loaded += MarqueeTextBlock_Loaded;
  42. Unloaded += MarqueeTextBlock_Unloaded;
  43. }
  44.  
  45. // unload event
  46. void MarqueeTextBlock_Unloaded(object sender, RoutedEventArgs e)
  47. {
  48. // stop marquee when control is unloaded
  49. StopMarquee();
  50. }
  51.  
  52. // set data context in Loaded
  53. void MarqueeTextBlock_Loaded(object sender, RoutedEventArgs e)
  54. {
  55. if (!isLoaded)
  56. {
  57. // control is loaded
  58. isLoaded = true;
  59.  
  60. LayoutRoot.DataContext = this;
  61.  
  62. // resize the clipping
  63.  
  64. Rect rect = new Rect(0, 0, 10000, canvas.ActualHeight);
  65.  
  66. RectangleGeometry reo = new RectangleGeometry();
  67. reo.Rect = rect;
  68. this.canvas.Clip = reo;
  69.  
  70. // always vertical alignment
  71. ChangeOffsetTop();
  72.  
  73. // this is OK for marquee to start
  74. StartMarquee();
  75. }
  76. else
  77. {
  78. // just run it, after user come to this page again
  79. StartMarquee();
  80. }
  81. }
  82.  
  83.  
  84. public void StartMarquee()
  85. {
  86. // first stop all marquee
  87. StopMarquee();
  88.  
  89. if (ShouldStartMarquee())
  90. {
  91. // change offset
  92. ChangeOffsetLeft(true);
  93.  
  94. DoAnimationFirst();
  95. }
  96. else
  97. {
  98. // change offset
  99. ChangeOffsetLeft(false);
  100. }
  101. }
  102.  
  103. public void StopMarquee()
  104. {
  105. if (storyboard != null)
  106. {
  107. storyboard.Stop();
  108. }
  109. }
  110.  
  111. bool ShouldStartMarquee()
  112. {
  113. return textBlock1.ActualWidth > LayoutRoot.ActualWidth;
  114. }
  115.  
  116.  
  117. // after
  118. void storyboard_Completed(object sender, EventArgs e)
  119. {
  120. DoAnimationAfter();
  121.  
  122. // remeber to unsubscribe
  123. storyboard.Completed += storyboard_Completed;
  124. }
  125.  
  126.  
  127. ////////////////////////////////////////////////////////////////////////////
  128. // calculate offset
  129. void ChangeOffsetLeft(bool isLong)
  130. {
  131. if (isLong)
  132. {
  133. offset = defaultOffset;
  134. Canvas.SetLeft(textBlock1, offset);
  135. }
  136. else
  137. {
  138. offset = (canvas.ActualWidth - textBlock1.ActualWidth)/2;
  139. Canvas.SetLeft(textBlock1, offset);
  140. }
  141. }
  142.  
  143. // center vertical
  144. void ChangeOffsetTop()
  145. {
  146. double topOffset = (canvas.ActualHeight - textBlock1.ActualHeight) / 2;
  147. Canvas.SetTop(textBlock1, topOffset);
  148.  
  149. }
  150.  
  151. int CalculateDurationFirst()
  152. {
  153. int duration = 50000;
  154.  
  155.  
  156.  
  157. return duration;
  158. }
  159.  
  160. int CalculateDurationAfter()
  161. {
  162. int duration = 50000;
  163.  
  164.  
  165.  
  166. return duration;
  167. }
  168.  
  169.  
  170. void DoAnimationFirst()
  171. {
  172. storyboard = new Storyboard();
  173. TranslateTransform trans = new TranslateTransform() { X = 5.0, Y = 1.0 };
  174. textBlock1.RenderTransformOrigin = new Point(0.5,0.5);
  175. textBlock1.RenderTransform = trans;
  176.  
  177. // we must calculate the Duration
  178. DoubleAnimation moveAnim = new DoubleAnimation();
  179. int durationFirst = CalculateDurationFirst();
  180. moveAnim.Duration = TimeSpan.FromMilliseconds(durationFirst);
  181.  
  182. moveAnim.From = 0;
  183.  
  184. moveAnim.To = -textBlock1.ActualWidth - offset;
  185. // MessageBox.Show((-textBlock1.ActualWidth-offset).ToString());
  186.  
  187. Storyboard.SetTarget(moveAnim, textBlock1);
  188. Storyboard.SetTargetProperty(moveAnim, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.X)"));
  189.  
  190. // subscribe to Completed event
  191. storyboard.Completed += new EventHandler(storyboard_Completed);
  192.  
  193. storyboard.Children.Add(moveAnim);
  194.  
  195. storyboard.FillBehavior = FillBehavior.HoldEnd;
  196.  
  197. storyboard.Begin();
  198. }
  199.  
  200.  
  201.  
  202. void DoAnimationAfter()
  203. {
  204. storyboard = new Storyboard();
  205. TranslateTransform trans = new TranslateTransform() { X = 5.0, Y = 1.0 };
  206. textBlock1.RenderTransformOrigin = new Point(0.5, 0.5);
  207. textBlock1.RenderTransform = trans;
  208.  
  209. // we must calculate the Duration
  210. DoubleAnimation moveAnim = new DoubleAnimation();
  211. int durationAfter = CalculateDurationAfter();
  212. moveAnim.Duration = TimeSpan.FromMilliseconds(durationAfter);
  213.  
  214. moveAnim.From = canvas.ActualWidth;
  215. moveAnim.To = (-textBlock1.ActualWidth - offset);
  216.  
  217. Storyboard.SetTarget(moveAnim, textBlock1);
  218. Storyboard.SetTargetProperty(moveAnim, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.X)"));
  219.  
  220. storyboard.Children.Add(moveAnim);
  221. storyboard.RepeatBehavior = RepeatBehavior.Forever;
  222. storyboard.FillBehavior = FillBehavior.HoldEnd;
  223.  
  224. storyboard.Begin();
  225. }
  226.  
  227.  
  228. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  229. // dependency property
  230. public static readonly DependencyProperty TextProperty =
  231. DependencyProperty.Register("Text", typeof(string), typeof(MarqueeTextBlock), null);
  232.  
  233. public string Text
  234. {
  235. get { return (string)this.GetValue(TextProperty); }
  236. set
  237. {
  238. this.SetValue(TextProperty, value);
  239.  
  240. // this doesnot marquee for the first time
  241. // because textBlock1.ActualWidth is 0 !!
  242. if (isLoaded)
  243. {
  244. StartMarquee();
  245. }
  246.  
  247. }
  248. }
  249.  
  250. public static readonly DependencyProperty MFontSizeProperty = DependencyProperty.Register("MFontSize",
  251. typeof(int), typeof(MarqueeTextBlock), null);
  252.  
  253. public int MFontSize
  254. {
  255. get { return (int)this.GetValue(MFontSizeProperty); }
  256. set
  257. {
  258. this.SetValue(MFontSizeProperty, value);
  259. }
  260. }
  261.  
  262. public static readonly DependencyProperty MTextWrapProperty = DependencyProperty.Register("MTextWrap",
  263. typeof(TextWrapping), typeof(MarqueeTextBlock), null);
  264.  
  265. public TextWrapping MTextWrap
  266. {
  267. get { return (TextWrapping)this.GetValue(MTextWrapProperty); }
  268. set
  269. {
  270. this.SetValue(MTextWrapProperty, value);
  271. }
  272. }
  273.  
  274. public static readonly DependencyProperty MWidthProperty = DependencyProperty.Register("MWidth",
  275. typeof(int), typeof(MarqueeTextBlock), null);
  276.  
  277. public int MWidth
  278. {
  279. get { return (int)this.GetValue(MWidthProperty); }
  280. set
  281. {
  282. this.SetValue(MWidthProperty, value);
  283. }
  284. }
  285. }
  286.  
  287. <controls2:MarqueeTextBlock x:Name="txtT"
  288. MFontSize="15"
  289.  
  290. Height="80" Margin="0,-15,0,0" FontSize="1"
  291. />
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement