原文:WPF 滚动文字控件MarqueeControl

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

XAML部分:

<UserControl x:Class="UIControl.MarqueeControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="30" d:DesignWidth="300" Loaded="UserControl_Loaded">
<Canvas ClipToBounds="True" x:Name="canvas">
<Canvas.Resources>
<Storyboard x:Key="stdUp">
<DoubleAnimation Duration="0:0:1.5" Storyboard.TargetName="content" Storyboard.TargetProperty="RenderTransform.Y"/>
</Storyboard>
<Storyboard x:Key="stdLeft">
<DoubleAnimation Duration="0:0:1.5" Storyboard.TargetName="content" Storyboard.TargetProperty="RenderTransform.X"/>
</Storyboard>
</Canvas.Resources>
<StackPanel x:Name="content">
<StackPanel.RenderTransform>
<TranslateTransform/>
</StackPanel.RenderTransform>
<TextBlock x:Name="txtItem" Foreground="Black"/>
</StackPanel>
</Canvas>
</UserControl>

后台部分:

 public partial class MarqueeControl : UserControl
{
Storyboard std = null;
DoubleAnimation animation = null;
int index, total; public MarqueeControl()
{
InitializeComponent();
} public MarqueeType ShowType
{
get { return (MarqueeType)this.GetValue(ShowTypeProperty); }
set { this.SetValue(ShowTypeProperty, value); }
}
public static readonly DependencyProperty ShowTypeProperty = DependencyProperty.Register("ShowType", typeof(MarqueeType), typeof(MarqueeControl), new PropertyMetadata(MarqueeType.Up)); public double Speed
{
get { return (double)this.GetValue(SpeedProperty); }
set { this.SetValue(SpeedProperty, value); }
}
public static readonly DependencyProperty SpeedProperty = DependencyProperty.Register("Speed", typeof(double), typeof(MarqueeControl), new PropertyMetadata(1.5)); private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
if (ShowType == MarqueeType.Up || ShowType == MarqueeType.Down)
{
std = (Storyboard)canvas.Resources["stdUp"];
content.Width = canvas.ActualWidth;
txtItem.TextWrapping = TextWrapping.Wrap;
}
if (ShowType == MarqueeType.Left || ShowType == MarqueeType.Right)
{
std = (Storyboard)canvas.Resources["stdLeft"];
content.Height = canvas.ActualHeight;
} animation = (DoubleAnimation)std.Children[0];
std.Completed += (t, r) => changeItem();
} private List<string> itemsSource;
public List<string> ItemsSource
{
get { return itemsSource; }
set
{
this.Dispatcher.BeginInvoke(new Action(() =>
{
if (std != null)
{
std.Stop();
txtItem.Text = "";
itemsSource = value; if (itemsSource != null && itemsSource.Count > 0)
{
index = 0;
total = value.Count;
changeItem();
}
}
}));
}
} private void changeItem()
{
txtItem.Text = itemsSource[index].ToString();
txtItem.UpdateLayout();
double canvasWidth = canvas.ActualWidth;
double canvasHeight = canvas.ActualHeight;
double txtWidth = txtItem.ActualWidth;
double txtHeight = txtItem.ActualHeight; if (ShowType == MarqueeType.Up)
{
animation.From = canvasHeight;
animation.To = -txtHeight;
}
else if (ShowType == MarqueeType.Down)
{
animation.From = -txtHeight;
animation.To = canvasHeight;
}
else if (ShowType == MarqueeType.Left)
{
animation.From = canvasWidth;
animation.To = -txtWidth;
}
else if (ShowType == MarqueeType.Right)
{
animation.From = -txtWidth;
animation.To = canvasWidth;
}
int time = 0;
if (ShowType == MarqueeType.Up || ShowType == MarqueeType.Down)
{
time = (int)(txtHeight / canvasHeight * Speed);
}
if (ShowType == MarqueeType.Left || ShowType == MarqueeType.Right)
{
time = (int)(txtWidth / canvasWidth * Speed);
}
if (time < 2) time = 2;
animation.Duration = new Duration(new TimeSpan(0, 0, time)); index++;
if (index == total) index = 0; if (std != null)
{
std.Begin();
}
}
} public enum MarqueeType
{
Up,
Down,
Left,
Right
}

用法:

 <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. NoHttp封装--03 cookie

    NoHttp请求自动维持Cookie:   1.支持Session.Cookie.临时Cookie的位置.   2.支持App重启.关机开机后继续持久化维持.   3.提供了接口,允许开发者监听Coo ...

  2. redis介绍 (8) window 下redis的集群(cluster命令)

    前言: 前段时间我在centos上搭建过一次redis集群,那是借助ruby搭建,这次我介绍一种纯redis集群命令的方式去搭建[最后我会简单介绍ruby搭建]. redis集群搭建(三主三备): 准 ...

  3. (后台)详细了解java中的null(转)

    转自CSDN: 相信大家对于NullPointException 这个让人又爱又恨的不陌生吧..对于Java程序员来说,null是令人头痛的东西.时常会受到空指针异常(NPE)的骚扰 .今天我们就来谈 ...

  4. centos7安装rabbitmq 总结

    centos7下安装rabbitmq 折腾了三天最后做了以下总结 先查看一电脑名  :示例 #hostname name 查看一下hosts配置文件:如果如下结果,就要修改下 #cat /etc/ho ...

  5. 如何用vmware workstation来做虚拟化实验

    前言 以前做用vmare只是简单的实验,但是随着现在虚拟化的兴起,我们的开始要开始虚拟化的实验了. 我们看到有些windows 2012的书上面说用hyper-v来实验,但是hyper-v只能做一些列 ...

  6. oracle 数据库 导出与导入 expdb和impdb使用方法 (服务器本机)

    expdb 与exp 导出数据有区异,exp 无法导出空值表,用于客户端,expdb 只用于服务器端.备份出来的数据可再远程传输到另外一台linux 实现异地备份! 一  关于expdp和impdp ...

  7. CRM lookup筛选

    function Loadcouse() { var type; var id; retrieveRecord(Xrm.Page.getAttribute("ownerid").g ...

  8. tkinter学习系列(三)之Label控件

    目录 目录 前言 (一)基本用法和可选属性 ==1.基本用法== ==2.可选属性== (二)属性的具体使用 ==1.常用属性== ==2.边距与文本对齐方式== ==案例一== ==案例一的效果== ...

  9. ccf--20150903--模板生成系统

    本题思路:首先,使用一个map来存储所有需要替换的关键词,然后,再逐行的替换掉其中的关键词,记住,find每次的其实位置不一样,否则会出现递归生成没有出现关键词就清空掉.最后输出. 题目和代码如下: ...

  10. 反射型XSS+文件上传+CSRF—DVWA

    在学习的过程中,想到将几种漏洞进行组合练习,记录下学习过程.大佬请绕过!谢谢!! 测试环境:DVWA,安装方法参考上一篇:https://www.cnblogs.com/aq-ry/p/9220584 ...