WPF中的连续文本代码 [英] Continuous text Ticker in WPF
问题描述
高朗高朗高朗高朗高朗高朗高朗高朗高朗高朗高朗高朗
Gaurang Gaurang Gaurang Gaurang Gaurang Gaurang Gaurang Gaurang Gaurang Gaurang Gaurang
我在 WPF 中工作以创建文本自动收报机.我可以永远将文本从右向左移动,但我的问题是,我想无缝移动相同的文本以创建上述效果(就像证券交易所股票代码一样).上面的文字一定是无止境的.
I am working in WPF to create a text ticker. I am able to move the text from right to left forever but my problem is, that I want to move the same text seamlessly to create the above effect(Just like the stock exchange ticker). The above text must move endlessly.
我怎样才能实现它?
推荐答案
所以从阅读您的问题我不确定您想要什么.您想要一个滚动出文本框并重新开始的文本吗,就像大多数选取框控件/功能一样.
So from reading your question i'm not really sure what you want. Do you want a text, that scrolls out of the textbox, and starts over, like most marquee controls/functions.
或者你想要一些在你的文本框中一遍又一遍地运行的文本,从头到尾填写?然后使用无法检测的给定循环?:)
Or do you want some text that runs over and over in your textbox, filling it out from start till end? and then with a given loop that is undectectable? :)
背后的代码:
//单行动画.
private void LeftToRightMarqueeOnTextBox()
{
string Copy = " "+TextBoxMarquee.Text;
double TextGraphicalWidth = new FormattedText(TextBoxMarquee.Text, System.Globalization.CultureInfo.CurrentCulture, System.Windows.FlowDirection.LeftToRight, new Typeface(TextBoxMarquee.FontFamily.Source), TextBoxMarquee.FontSize, TextBoxMarquee.Foreground).WidthIncludingTrailingWhitespace;
//BorderTextBoxMarquee.Width = TextGraphicalWidth + 5;
ThicknessAnimation ThickAnimation = new ThicknessAnimation();
ThickAnimation.From = new Thickness(TextBoxMarquee.ActualWidth, 0, 0, 0);
ThickAnimation.To = new Thickness(-TextGraphicalWidth, 0, 0, 0);
ThickAnimation.RepeatBehavior = RepeatBehavior.Forever;
ThickAnimation.Duration = new Duration(TimeSpan.FromSeconds(3));
TextBoxMarquee.BeginAnimation(TextBox.PaddingProperty, ThickAnimation);
}
或
//没有间隙的句子重复动画.
//Sentence Repeat animation with no gaps.
string Copy = " "+TextBoxMarquee.Text;
double TextGraphicalWidth = new FormattedText(Copy, System.Globalization.CultureInfo.CurrentCulture, System.Windows.FlowDirection.LeftToRight, new Typeface(TextBoxMarquee.FontFamily.Source), TextBoxMarquee.FontSize, TextBoxMarquee.Foreground).WidthIncludingTrailingWhitespace;
double TextLenghtGraphicalWidth = 0;
//BorderTextBoxMarquee.Width = TextGraphicalWidth + 5;
while (TextLenghtGraphicalWidth < TextBoxMarquee.ActualWidth)
{
TextBoxMarquee.Text += Copy;
TextLenghtGraphicalWidth = new FormattedText(TextBoxMarquee.Text, System.Globalization.CultureInfo.CurrentCulture, System.Windows.FlowDirection.LeftToRight, new Typeface(TextBoxMarquee.FontFamily.Source), TextBoxMarquee.FontSize, TextBoxMarquee.Foreground).WidthIncludingTrailingWhitespace;
}
TextBoxMarquee.Text += " "+TextBoxMarquee.Text;
ThicknessAnimation ThickAnimation = new ThicknessAnimation();
ThickAnimation.From = new Thickness(0, 0, 0, 0);
ThickAnimation.To = new Thickness(-TextGraphicalWidth, 0, 0, 0);
ThickAnimation.RepeatBehavior = RepeatBehavior.Forever;
ThickAnimation.Duration = new Duration(TimeSpan.FromSeconds(2));
TextBoxMarquee.BeginAnimation(TextBox.PaddingProperty, ThickAnimation);
XAML:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Canvas ClipToBounds="True" Name="canMain" Background="Transparent">
<Grid Width="{Binding ElementName=canMain, Path=ActualWidth}" >
<Grid.ColumnDefinitions>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Name="TextBlockMarquee" Text="This is my animated text" />
<Border Grid.Row="1" BorderBrush="Black" BorderThickness="1">
<TextBox ClipToBounds="True" Name="TextBoxMarquee" Text="lars er en god skakspiller, der tænker længere end de andre" BorderBrush="Transparent"/>
</Border>
</Grid>
</Canvas>
</Grid>
基本上忽略大部分 XAML.重要的是文本框中文本的长度.还没有找到通用的解决方案,这就是为什么我的 From Thickness 是基于数字的.
Basically ignore most of the XAML. Whats important is the lenght of the Text in the Textbox. Havn't found a generic solution, thats why my From Thickness is number based.
但必须确保这是您要找的东西:)
But just had to be sure that this was what you were looking for :)
编辑/更新:
所以现在尝试一下,我已经更新了代码..希望它有效,但从一些测试来看,它似乎是这样的:)您唯一需要确定的是整个文本框都已填写,然后我正在复制它,我将从字符串图形长度中循环两个文本:)(
So try it out now, i've updated the code.. Hopefully it works, but from a few test it seemed like it :) Only thing that you need to be sure of is that the entire textbox is filled out, then i'm doing a copy of it, and i'll loop the two text from the strings graphical lenght :)(
编辑/更新:我已经添加了我的最终解决方案,希望这对您有所帮助.
EDIT/UPDATE: I've added my final solutions hope this was of any help.
编辑/更新
忘记更新我的重复解决方案,所以它没有切断并使循环可见..现在完成:)
Forgot to update my Repeat solution so it didn't cut off and make the loops visible.. Done now :)
这篇关于WPF中的连续文本代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!