原文:WPF 滚动文字控件MarqueeControl

WPF使用的滚动文字控件,支持上下左右滚动方式,支持设置滚动速度

XAML部分:

  1. <UserControl x:Class="UIControl.MarqueeControl"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  5. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  6. mc:Ignorable="d"
  7. d:DesignHeight="30" d:DesignWidth="300" Loaded="UserControl_Loaded">
  8. <Canvas ClipToBounds="True" x:Name="canvas">
  9. <Canvas.Resources>
  10. <Storyboard x:Key="stdUp">
  11. <DoubleAnimation Duration="0:0:1.5" Storyboard.TargetName="content" Storyboard.TargetProperty="RenderTransform.Y"/>
  12. </Storyboard>
  13. <Storyboard x:Key="stdLeft">
  14. <DoubleAnimation Duration="0:0:1.5" Storyboard.TargetName="content" Storyboard.TargetProperty="RenderTransform.X"/>
  15. </Storyboard>
  16. </Canvas.Resources>
  17. <StackPanel x:Name="content">
  18. <StackPanel.RenderTransform>
  19. <TranslateTransform/>
  20. </StackPanel.RenderTransform>
  21. <TextBlock x:Name="txtItem" Foreground="Black"/>
  22. </StackPanel>
  23. </Canvas>
  24. </UserControl>

后台部分:

  1. public partial class MarqueeControl : UserControl
  2. {
  3. Storyboard std = null;
  4. DoubleAnimation animation = null;
  5. int index, total;
  6. public MarqueeControl()
  7. {
  8. InitializeComponent();
  9. }
  10. public MarqueeType ShowType
  11. {
  12. get { return (MarqueeType)this.GetValue(ShowTypeProperty); }
  13. set { this.SetValue(ShowTypeProperty, value); }
  14. }
  15. public static readonly DependencyProperty ShowTypeProperty = DependencyProperty.Register("ShowType", typeof(MarqueeType), typeof(MarqueeControl), new PropertyMetadata(MarqueeType.Up));
  16. public double Speed
  17. {
  18. get { return (double)this.GetValue(SpeedProperty); }
  19. set { this.SetValue(SpeedProperty, value); }
  20. }
  21. public static readonly DependencyProperty SpeedProperty = DependencyProperty.Register("Speed", typeof(double), typeof(MarqueeControl), new PropertyMetadata(1.5));
  22. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  23. {
  24. if (ShowType == MarqueeType.Up || ShowType == MarqueeType.Down)
  25. {
  26. std = (Storyboard)canvas.Resources["stdUp"];
  27. content.Width = canvas.ActualWidth;
  28. txtItem.TextWrapping = TextWrapping.Wrap;
  29. }
  30. if (ShowType == MarqueeType.Left || ShowType == MarqueeType.Right)
  31. {
  32. std = (Storyboard)canvas.Resources["stdLeft"];
  33. content.Height = canvas.ActualHeight;
  34. }
  35. animation = (DoubleAnimation)std.Children[0];
  36. std.Completed += (t, r) => changeItem();
  37. }
  38. private List<string> itemsSource;
  39. public List<string> ItemsSource
  40. {
  41. get { return itemsSource; }
  42. set
  43. {
  44. this.Dispatcher.BeginInvoke(new Action(() =>
  45. {
  46. if (std != null)
  47. {
  48. std.Stop();
  49. txtItem.Text = "";
  50. itemsSource = value;
  51. if (itemsSource != null && itemsSource.Count > 0)
  52. {
  53. index = 0;
  54. total = value.Count;
  55. changeItem();
  56. }
  57. }
  58. }));
  59. }
  60. }
  61. private void changeItem()
  62. {
  63. txtItem.Text = itemsSource[index].ToString();
  64. txtItem.UpdateLayout();
  65. double canvasWidth = canvas.ActualWidth;
  66. double canvasHeight = canvas.ActualHeight;
  67. double txtWidth = txtItem.ActualWidth;
  68. double txtHeight = txtItem.ActualHeight;
  69. if (ShowType == MarqueeType.Up)
  70. {
  71. animation.From = canvasHeight;
  72. animation.To = -txtHeight;
  73. }
  74. else if (ShowType == MarqueeType.Down)
  75. {
  76. animation.From = -txtHeight;
  77. animation.To = canvasHeight;
  78. }
  79. else if (ShowType == MarqueeType.Left)
  80. {
  81. animation.From = canvasWidth;
  82. animation.To = -txtWidth;
  83. }
  84. else if (ShowType == MarqueeType.Right)
  85. {
  86. animation.From = -txtWidth;
  87. animation.To = canvasWidth;
  88. }
  89. int time = 0;
  90. if (ShowType == MarqueeType.Up || ShowType == MarqueeType.Down)
  91. {
  92. time = (int)(txtHeight / canvasHeight * Speed);
  93. }
  94. if (ShowType == MarqueeType.Left || ShowType == MarqueeType.Right)
  95. {
  96. time = (int)(txtWidth / canvasWidth * Speed);
  97. }
  98. if (time < 2) time = 2;
  99. animation.Duration = new Duration(new TimeSpan(0, 0, time));
  100. index++;
  101. if (index == total) index = 0;
  102. if (std != null)
  103. {
  104. std.Begin();
  105. }
  106. }
  107. }
  108. public enum MarqueeType
  109. {
  110. Up,
  111. Down,
  112. Left,
  113. Right
  114. }

用法:

  1. <UIControl:MarqueeControl x:Name="scrollingTextControl" ShowType="Left" Speed="2"/>

后台设置ItemSource:scrollingTextControl.ItemsSource = new List<string>() { ... };

WPF 滚动文字控件MarqueeControl的更多相关文章

  1. WPF 在绘图控件(Shape)中添加文字 [2018.7.15]

    原文:WPF 在绘图控件(Shape)中添加文字 [2018.7.15] Q:使用Shape的子类Ellipse画一个圆,如何在圆中添加文字? A:Shape类中不包含Text属性.可使用Shape类 ...

  2. MFC入门(三)-- MFC图片/文字控件(循环显示文字和图片的小程序)

    惯例附上前几个博客的链接: MFC入门(一)简单配置:http://blog.csdn.net/zmdsjtu/article/details/52311107 MFC入门(二)读取输入字符:http ...

  3. WPF 曲线图表控件(自制)(二)

    原文:WPF 曲线图表控件(自制)(二) 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/koloumi/article/details/775218 ...

  4. WPF 4 DataGrid 控件(自定义样式篇)

    原文:WPF 4 DataGrid 控件(自定义样式篇)      在<WPF 4 DataGrid 控件(基本功能篇)>中我们已经学习了DataGrid 的基本功能及使用方法.本篇将继续 ...

  5. Windows Community Toolkit 3.0 新功能 在WinForms 和 WPF 使用 UWP 控件

    本文告诉大家一个令人震惊的消息,Windows Community Toolkit 有一个大更新,现在的版本是 3.0 .最大的提升就是 WinForm 和 WPF 程序可以使用部分 UWP 控件. ...

  6. WPF中Ribbon控件的使用

    这篇博客将分享如何在WPF程序中使用Ribbon控件.Ribbon可以很大的提高软件的便捷性. 上面截图使Outlook 2010的界面,在Home标签页中,将所属的Menu都平铺的布局,非常容易的可 ...

  7. WPF 调用WinForm控件

    WPF可以使用WindowsFormsHost控件做为容器去显示WinForm控件,类似的用法网上到处都是,就是拖一个WindowsFormsHost控件winHost1到WPF页面上,让后设置win ...

  8. InteropBitmap指定内存,绑定WPF的Imag控件时刷新问题。

    1.InteropBitmap指定内存,绑定WPF的Imag控件的Source属性 创建InteropBitmap的时候,像素的格式必须为PixelFormats.Bgr32, 如果不是的话在绑定到I ...

  9. 在WPF程序中将控件所呈现的内容保存成图像(转载)

    在WPF程序中将控件所呈现的内容保存成图像 转自:http://www.cnblogs.com/TianFang/archive/2012/10/07/2714140.html 有的时候,我们需要将控 ...

随机推荐

  1. 【转】64位系统下无法使用libpam-mysql的md5

    转自:http://superwf.dyndns.info/?p=331 Aug 23 09:05:57 wfoffice saslauthd[7235]: pam_mysql – non-crypt ...

  2. couldn't locate lint-gradle-api-26.1.2.jar for flutter project

    Could not find com.android.tools.lint:lint-gradle:26.1.2 当我尝试构建发行版本APK 时导致报这种错误,无法发行,针对自己的项目作出了相关修改, ...

  3. json替换jsonp实现跨域请求

    最近遇到h5前端页面和web后端双方的请求存在跨域,普通的jquery.ajax请求已不能实现(因为js是不允许跨域的(如果可以跨域,那就能随便改别人的网页了),js的原理), 最后经过艰苦奋斗,终于 ...

  4. LeetCode题解之Diameter of Binary Tree

    1.题目描述 2.分析 深度优先. 3.代码 int ans; int diameterOfBinaryTree(TreeNode* root) { ans = ; depth(root); ; } ...

  5. [20170927]关于hugepages.txt

    [20170927]关于hugepages.txt --//今天测试hugepages与内核参数nr_overcommit_hugepages,才发现HugePages_Surp表示什么? --// ...

  6. 洗礼灵魂,修炼python(27)--异常处理(1)—>了解异常

    python学到这,其实你应该是在入门到进阶的中间阶段了,但是还没有到进阶的阶段的,这是肯定的,因为进阶得可以从实际问题中解决问题的,比如写一个自动化的爬虫程序啊,对一件事物作大数据归纳分析,开发一个 ...

  7. [SQL SERVER] The CHECK_POLICY and CHECK_EXPIRATION options cannot be turned OFF when MUST_CHANGE is ON. (Microsoft SQL Server, Error: 15128)

    The CHECK_POLICY and CHECK_EXPIRATION options cannot be turned OFF when MUST_CHANGE is ON. (Microsof ...

  8. 高通移植mipi LCD的过程LK代码

    lk部分:(实现LCD兼容) 1. 函数定位 aboot_init()来到target_display_init(): 这就是高通原生lk LCD 兼容的关键所在.至于你需要兼容多少LCD 就在whi ...

  9. SonarQube 配置 LDAP(AD域)

    安装插件 1.下载 LDAP Plugin 插件,地址:https://docs.sonarqube.org/display/SONARQUBE67/LDAP+Plugin2.将下载的插件,放到 SO ...

  10. 4.6Python多版本存在问题

    返回总目录 目录: 1.展示效果: 2.操作流程: (一)展示效果: 1.多个版本python运行的情况: 2.多个版本pip运行的情况: (二)操作流程: 1.很关键的一条语句: pythonx.x ...