原文: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. Python自动化开发之python的常用模块

    python常用模块 模块的种类:模块分为三种,分别是自定义模块:内置标准模块(即标准库):开源模块(第三方). 以下主要研究标准模块即标准库:标准库直接导入即可,不需要安装. 时间模块:time , ...

  2. Linux  改动inittab文件及忘记密码等导致无法进入系统的解决办法

    改动inittab文件及忘记密码等导致无法进入系统的解决办法[摘] by:授客 QQ:1033553122 inittab是linux的系统启动模式配置文件,在”/etc“文件目录下没,其内容是: # ...

  3. ios开发GCD(2)-dispatch_semaphore_t信号量计数器

    思考:现在有多个线程异步执行,我们想要同时最多只能执行2个或n个,该怎么办? dispatch_semaphore_t 看代码解析: NSLog(@"开始"); dispatch_ ...

  4. Loading加载页面

    一般页面有四种情况 加载中 :就是滚动页面,后台获取加载的数据,每个页面的数据不同所以就让子类来实现,直接抽象abstract了. 加载失败 :一般都需要点击后重新加载 空页面 :也需要点击后重新加载 ...

  5. Android Studio_更新Gradle

    一.Gradle更新问题 Android Studio每次更新版本都会更新Gradle这个插件,而且有时候提示更新,却一直更新不了,那是因为中国伟大的长城问题.就是下图,我刚刚更新了,提示更新grad ...

  6. OneAlert 携手 BearyChat(倍洽)快速构建 IT 运维 on-call 机制

    OneAlert 是北京蓝海讯通科技股份有限公司旗下产品,中国第⼀个 SaaS 模式的免费的云告警平台,集成国内外主流监控/⽀撑系统,实现⼀个平台上集中处理所有 IT 事件,提升 IT 可靠性.并且能 ...

  7. 利用StopWatch类监控Java代码执行时间并分析性能

    springframework中的StopWatch类可以测量一个时间间隔的运行时间,也可以测量多个时间间隔的总运行时间.一般用来测量代码执行所用的时间或者计算性能数据,在优化代码性能上可以使用Sto ...

  8. sql server 数据导出(入)方法总结

    我们都知道日常在面对数据需求时需要导出数据,比较少量的数据导出我们一般是通过查询后另存即可,当面对数据量比较大的时候我们应该怎么处理?我搜索总结一些几个方法:1.bcp 导出.2.数据库本身自带的导入 ...

  9. sqlserver 将 “用 特定字符 分隔的一个字段” 拆分成多个字段,然后两个表之间数据更新

    将源TXT文件sourceFile_table.txt导入数据库,生成新表dbo.sourceFile_table.新增字段lon.lat.shi.xian 源表dbo.sourceFile_tabl ...

  10. Elasticsearch安装图形化界面工具Head插件

    1.Head插件介绍以及下载 Head插件是Elasticsearch的图形化界面工具,通过此插件可以很方便的对数据进行增删改查等数据交互操作.在Elasticsearch5.x版本以后,head插件 ...