在XAML中,添加如下代码:
<ScrollViewer HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden" BorderThickness="0" Height="36"> <Canvas Loaded="MarqueeControl_Loaded" Width="592" HorizontalAlignment="Left"> <Canvas.Resources> <Storyboard x:Name="sb"> <DoubleAnimation x:Name="da" BeginTime="00:00:02" Storyboard.TargetName="txtLorem" Storyboard.TargetProperty="(Canvas.Left)" From="0" RepeatBehavior="Forever"/> </Storyboard> </Canvas.Resources> <TextBlock x:Name="txtLorem" Width="596" Height="33" Text="滚动文字内容..."/> </Canvas> </ScrollViewer>
在c#里创建相应的方法:
void MarqueeControl_Loaded(object sender,RoutedEventArgs e) { var canvas = sender as Canvas; if (txtLorem.ActualWidth <= canvas.Width) return; const double speed = 50; da.To = -txtLorem.ActualWidth; da.Duration = new Duration(TimeSpan.FromSeconds(txtLorem.ActualWidth / speed)); sb.Begin(); }
窗体加载的时候:
给把要显示的文字赋值给txtLorem: this.txtLorem.Text = "滚动文字。。。。。";
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。