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的更多相关文章
- WPF 在绘图控件(Shape)中添加文字 [2018.7.15]
原文:WPF 在绘图控件(Shape)中添加文字 [2018.7.15] Q:使用Shape的子类Ellipse画一个圆,如何在圆中添加文字? A:Shape类中不包含Text属性.可使用Shape类 ...
- MFC入门(三)-- MFC图片/文字控件(循环显示文字和图片的小程序)
惯例附上前几个博客的链接: MFC入门(一)简单配置:http://blog.csdn.net/zmdsjtu/article/details/52311107 MFC入门(二)读取输入字符:http ...
- WPF 曲线图表控件(自制)(二)
原文:WPF 曲线图表控件(自制)(二) 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/koloumi/article/details/775218 ...
- WPF 4 DataGrid 控件(自定义样式篇)
原文:WPF 4 DataGrid 控件(自定义样式篇) 在<WPF 4 DataGrid 控件(基本功能篇)>中我们已经学习了DataGrid 的基本功能及使用方法.本篇将继续 ...
- Windows Community Toolkit 3.0 新功能 在WinForms 和 WPF 使用 UWP 控件
本文告诉大家一个令人震惊的消息,Windows Community Toolkit 有一个大更新,现在的版本是 3.0 .最大的提升就是 WinForm 和 WPF 程序可以使用部分 UWP 控件. ...
- WPF中Ribbon控件的使用
这篇博客将分享如何在WPF程序中使用Ribbon控件.Ribbon可以很大的提高软件的便捷性. 上面截图使Outlook 2010的界面,在Home标签页中,将所属的Menu都平铺的布局,非常容易的可 ...
- WPF 调用WinForm控件
WPF可以使用WindowsFormsHost控件做为容器去显示WinForm控件,类似的用法网上到处都是,就是拖一个WindowsFormsHost控件winHost1到WPF页面上,让后设置win ...
- InteropBitmap指定内存,绑定WPF的Imag控件时刷新问题。
1.InteropBitmap指定内存,绑定WPF的Imag控件的Source属性 创建InteropBitmap的时候,像素的格式必须为PixelFormats.Bgr32, 如果不是的话在绑定到I ...
- 在WPF程序中将控件所呈现的内容保存成图像(转载)
在WPF程序中将控件所呈现的内容保存成图像 转自:http://www.cnblogs.com/TianFang/archive/2012/10/07/2714140.html 有的时候,我们需要将控 ...
随机推荐
- 【转】64位系统下无法使用libpam-mysql的md5
转自:http://superwf.dyndns.info/?p=331 Aug 23 09:05:57 wfoffice saslauthd[7235]: pam_mysql – non-crypt ...
- 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 时导致报这种错误,无法发行,针对自己的项目作出了相关修改, ...
- json替换jsonp实现跨域请求
最近遇到h5前端页面和web后端双方的请求存在跨域,普通的jquery.ajax请求已不能实现(因为js是不允许跨域的(如果可以跨域,那就能随便改别人的网页了),js的原理), 最后经过艰苦奋斗,终于 ...
- LeetCode题解之Diameter of Binary Tree
1.题目描述 2.分析 深度优先. 3.代码 int ans; int diameterOfBinaryTree(TreeNode* root) { ans = ; depth(root); ; } ...
- [20170927]关于hugepages.txt
[20170927]关于hugepages.txt --//今天测试hugepages与内核参数nr_overcommit_hugepages,才发现HugePages_Surp表示什么? --// ...
- 洗礼灵魂,修炼python(27)--异常处理(1)—>了解异常
python学到这,其实你应该是在入门到进阶的中间阶段了,但是还没有到进阶的阶段的,这是肯定的,因为进阶得可以从实际问题中解决问题的,比如写一个自动化的爬虫程序啊,对一件事物作大数据归纳分析,开发一个 ...
- [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 ...
- 高通移植mipi LCD的过程LK代码
lk部分:(实现LCD兼容) 1. 函数定位 aboot_init()来到target_display_init(): 这就是高通原生lk LCD 兼容的关键所在.至于你需要兼容多少LCD 就在whi ...
- SonarQube 配置 LDAP(AD域)
安装插件 1.下载 LDAP Plugin 插件,地址:https://docs.sonarqube.org/display/SONARQUBE67/LDAP+Plugin2.将下载的插件,放到 SO ...
- 4.6Python多版本存在问题
返回总目录 目录: 1.展示效果: 2.操作流程: (一)展示效果: 1.多个版本python运行的情况: 2.多个版本pip运行的情况: (二)操作流程: 1.很关键的一条语句: pythonx.x ...