原文: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. 腾讯Tars环境搭建 ---- centos

    1,安装git yum install git 2,下载脚本 git clone https://github.com/tangramor/Tars_Install.git 注意:会有3个脚本,cen ...

  2. loadrunner 脚本开发-定义全局变量

    脚本开发-定义全局变量 by:授客 QQ:1033553122 如果参数是全局的,在脚本中的任何一个Action中都可以使用,变量一般是局部的,如果跨Action调用会出现未声明的错误. 打开Scri ...

  3. [20180630]truncate table的另类恢复2.txt

    [20180630]truncate table的另类恢复2.txt --//上个星期做了truncate table的另类恢复,通过修改数据块的段号,再通过rowid定位收集数据,达到修复的目的.- ...

  4. apache 访问权限出错,apache selinux 权限问题, (13) Permission Denied

    今天在使用 httpd 做文件服务器的时候,发现 png 图像没有打开,但是原本www/html 文件夹内部的文件就可以打开.后来猜测是selinux 的问题,之前一直想写一篇关于selinux 的博 ...

  5. js获取子节点和修改input的文本框内容

    js获取子节点和修改input的文本框内容 js获取子节点: $("#"+defaultPVItemId).children().eq(3); //获取某个选择器下的第四个子节点 ...

  6. [MapReduce_add_5] MapReduce 实现标签的生成与聚合

    0. 说明 MapReduce 实现标签的生成与聚合 介绍 && 流程图 && 程序编写 1. 介绍 [1.1 原始有效数据] 86913510 {"revi ...

  7. popen()/pclose()阻塞性问题验证

    背景: popen()函数通过创建一个管道,调用fork()产生一个子进程,执行一个shell以运行命令来开启一个进程.这个管道必须由pclose()函数关闭,而不是fclose()函数. pclos ...

  8. SAP CRM 自定义控制器与数据绑定

    当用户从视图离开时,视图将失去它的数据.解决这个问题,需要引入自定义控制器(Custom Controller)(译者注:SAP CRM自定义端中,不同地方的Custom Controller会翻译为 ...

  9. File类_常见的方法(获取目录中指定规则的内容)

    首先定义过滤器 import java.io.File; import java.io.FilenameFilter; public class FileByJava implements Filen ...

  10. C#常见委托のdelegate定义,Func,Action,Predicate总结

    委托,顾名思义,就是让其他代理,本质就是为具有共性方法组定义一个方法模板:(交流可以加qq群:435226676) 委托常见的方式有一般委托显示定义,Func<T,TResult> (T, ...