文字选框从右到左

时间:2014-03-04 10:25:50

标签: wpf textblock

我使用以下代码将TextBlock中的文本从右向左移动: -

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(40);
TextBlockMarquee.BeginAnimation(TextBlock.MarginProperty, ThickAnimation);

我遇到的问题是这个代码适用于宽度小于屏幕宽度的文本。但对于宽度较大的文本,屏幕文本会闪烁很多(当文本包含unicode字符时,闪烁会增加,如:“\ u25Bc”)。

这里: - TextGraphicalWidth是原始文本的宽度。 TextLenghtGraphicalWidth是将文本附加到原始文本后的文本宽度。

我该如何处理?

的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 />
                <RowDefinition />
                <RowDefinition />
            </Grid.RowDefinitions>
            <TextBlock Grid.Row="0" Name="TextBlockMarquee" Text="This is my animated text with special characters:" />
            <Border Grid.Row="1" BorderBrush="Black" BorderThickness="1">
                <TextBlock ClipToBounds="True" FontSize="22" Name="TextBoxMarquee" FontFamily="Segoe UI" Text="" />
            </Border>
            <TextBlock Grid.Row="2" Name="TextBlockMarqueenew" Text="This is my animated text without special characters:" />
            <Border Grid.Row="3" BorderBrush="Black" BorderThickness="1">
                <TextBlock ClipToBounds="True" FontSize="22" Name="TextBoxMarquee1" FontFamily="Segoe UI" Text="" />
            </Border>
        </Grid>
    </Canvas>
</Grid>

代码背后: -

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace TickerDemoNew
{
/// <summary>
/// Interaction logic for TickerMaxTextBlock.xaml
/// </summary>
public partial class TickerMaxTextBlock : Window
{
    StringBuilder s,s1;
    string TexttoScroll, TexttoScroll1;
    public TickerMaxTextBlock()
    {

        InitializeComponent();
        TextOptions.SetTextFormattingMode(TextBoxMarquee,TextFormattingMode.Ideal);
        UseLayoutRounding = true;
        TextBoxMarquee.Inlines.Add(new Run("I have attached herewith the full implementation of Custom TextBox class (TextBoxEx) in a word file. In the implementation when Tooltip is opening, it is checking whether text has truncated or not.  If text has not truncated then tooltip won’t be displayed (as it is not necessary because full text is anyway visible). Also, in order to show the tooltip, text property of the Textblock should be bound with the tooltip. ") { Foreground = Brushes.Red });
        TextBoxMarquee.Inlines.Add(new Run(" Despite of all the interop support available sometimes you will still come across some tricky issues which will be difficult to overcome. One such problem I faced recently. I had to configure the Dialog with MFC PropertySheet control. This PropertySheet control was holding few Property pages. These property pages didn’t had any MFC controls as content but hosting the user controls developed in WPF. First I will write very briefly about how to load the WPF user control inside the MFC container and then I will explain about my tricky problem and how I fixed it.") { Foreground = Brushes.Green });
    }

    private void Window_Loaded_1(object sender, RoutedEventArgs e)
    {


        double TextGraphicalWidth = 0;
        foreach (var str in TextBoxMarquee.Inlines)
        {
            TextGraphicalWidth = TextGraphicalWidth + new FormattedText(((Run)str).Text, System.Threading.Thread.CurrentThread.CurrentCulture, System.Windows.FlowDirection.LeftToRight, new Typeface(str.FontFamily.ToString()), str.FontSize, str.Foreground).WidthIncludingTrailingWhitespace;
        }

        double TextLenghtGraphicalWidth = 0;

        while (TextLenghtGraphicalWidth < TextBoxMarquee.ActualWidth)
        {

            TextBoxMarquee.Inlines.Add(new Run("I have attached herewith the full implementation of Custom TextBox class (TextBoxEx) in a word file. In the implementation when Tooltip is opening, it is checking whether text has truncated or not.  If text has not truncated then tooltip won’t be displayed (as it is not necessary because full text is anyway visible). Also, in order to show the tooltip, text property of the Textblock should be bound with the tooltip. ") { Foreground = Brushes.Red });
            TextBoxMarquee.Inlines.Add(new Run(" Despite of all the interop support available sometimes you will still come across some tricky issues which will be difficult to overcome. One such problem I faced recently. I had to configure the Dialog with MFC PropertySheet control. This PropertySheet control was holding few Property pages. These property pages didn’t had any MFC controls as content but hosting the user controls developed in WPF. First I will write very briefly about how to load the WPF user control inside the MFC container and then I will explain about my tricky problem and how I fixed it.") { Foreground = Brushes.Green });

            foreach (var str in TextBoxMarquee.Inlines)
            {
                TextLenghtGraphicalWidth = TextLenghtGraphicalWidth + new FormattedText(((Run)str).Text, System.Threading.Thread.CurrentThread.CurrentCulture, System.Windows.FlowDirection.LeftToRight, new Typeface(str.FontFamily.ToString()), str.FontSize, str.Foreground).WidthIncludingTrailingWhitespace; 
            }


        }




        TextBoxMarquee.Inlines.Add(new Run("I have attached herewith the full implementation of Custom TextBox class (TextBoxEx) in a word file. In the implementation when Tooltip is opening, it is checking whether text has truncated or not.  If text has not truncated then tooltip won’t be displayed (as it is not necessary because full text is anyway visible). Also, in order to show the tooltip, text property of the Textblock should be bound with the tooltip. ") { Foreground = Brushes.Red });
        TextBoxMarquee.Inlines.Add(new Run(" Despite of all the interop support available sometimes you will still come across some tricky issues which will be difficult to overcome. One such problem I faced recently. I had to configure the Dialog with MFC PropertySheet control. This PropertySheet control was holding few Property pages. These property pages didn’t had any MFC controls as content but hosting the user controls developed in WPF. First I will write very briefly about how to load the WPF user control inside the MFC container and then I will explain about my tricky problem and how I fixed it.") { Foreground = Brushes.Green });


        double totalWidth = 0;
        foreach (var newStr in TextBoxMarquee.Inlines)
        {
            totalWidth = totalWidth + new FormattedText(((Run)newStr).Text, System.Threading.Thread.CurrentThread.CurrentCulture, System.Windows.FlowDirection.LeftToRight, new Typeface(newStr.FontFamily.ToString()), newStr.FontSize, newStr.Foreground).WidthIncludingTrailingWhitespace; 
        }
        MessageBox.Show((TextLenghtGraphicalWidth + TextBoxMarquee.ActualWidth).ToString());
        MessageBox.Show((TextLenghtGraphicalWidth + TextGraphicalWidth).ToString());
        MessageBox.Show(totalWidth.ToString());

        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((50 * (totalWidth)) / TextBoxMarquee.ActualWidth));
        TextBoxMarquee.BeginAnimation(TextBlock.MarginProperty, ThickAnimation);


    }
}

}

0 个答案:

没有答案