From 13d7fe2a01d10559f134de6e03d51cbf364485c9 Mon Sep 17 00:00:00 2001 From: adri Date: Fri, 25 Jan 2019 18:25:04 +0100 Subject: [PATCH] Rotate TextBlock /!\ Resize not suported --- .../ProjetTheAlone_g1bpa4xt_wpftmp.csproj | 268 ++++++++++++++++++ ProjetTheAlone/ViewModel/TextDefilant.cs | 87 ++++++ 2 files changed, 355 insertions(+) create mode 100644 ProjetTheAlone/ProjetTheAlone_g1bpa4xt_wpftmp.csproj create mode 100644 ProjetTheAlone/ViewModel/TextDefilant.cs diff --git a/ProjetTheAlone/ProjetTheAlone_g1bpa4xt_wpftmp.csproj b/ProjetTheAlone/ProjetTheAlone_g1bpa4xt_wpftmp.csproj new file mode 100644 index 0000000..30586ad --- /dev/null +++ b/ProjetTheAlone/ProjetTheAlone_g1bpa4xt_wpftmp.csproj @@ -0,0 +1,268 @@ + + + + + Debug + AnyCPU + {F8032333-16C0-49D9-A5F6-D64E6044A580} + WinExe + ProjetTheAlone + ProjetTheAlone + v4.6.1 + 512 + {60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + 4 + true + false + publish\ + true + Disk + false + Foreground + 7 + Days + false + false + true + 0 + 1.0.0.%2a + false + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + Settings2.settings + True + True + + + + + + + + + + + + + + + FicDetailEvent.xaml + + + FicEncodePlat.xaml + + + + + + + + + + + + + + + + + + + + + + + + + + True + True + Settings1.settings + + + + + + + + + + + + + + + + + + EventFutur.xaml + + + EventPasse.xaml + + + Plat.xaml + + + Repa.xaml + + + + Accueil.xaml + + + DashBoard.xaml + + + FicBeneficiaireEncode.xaml + + + FicEncodage.xaml + + + FicEvenementEncode.xaml + + + FicNewEquipe.xaml + + + FicNewLieu.xaml + + + FicNewtypeEvent.xaml + + + FicRepasEncode.xaml + + + App.xaml + Code + + + MainWindow.xaml + Code + + + + + Code + + + True + True + Resources.resx + + + True + Settings.settings + True + + + ResXFileCodeGenerator + Resources.Designer.cs + + + SettingsSingleFileGenerator + Settings2.Designer.cs + + + + SettingsSingleFileGenerator + Settings.Designer.cs + + + SettingsSingleFileGenerator + Settings1.Designer.cs + + + + + + + + + + False + Microsoft .NET Framework 4.6.1 %28x86 and x64%29 + true + + + False + .NET Framework 3.5 SP1 + false + + + + + Always + + + Always + ProjetThe.mdf + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ProjetTheAlone/ViewModel/TextDefilant.cs b/ProjetTheAlone/ViewModel/TextDefilant.cs new file mode 100644 index 0000000..ab3867b --- /dev/null +++ b/ProjetTheAlone/ViewModel/TextDefilant.cs @@ -0,0 +1,87 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Globalization; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Timers; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Media; + +namespace ProjetTheAlone.ViewModel +{ + class TextDefilant : BasePropriete + { + Timer aTimer; + + TextBlock textContainer; + char[] textAfficherBuffer; + public string TextAfficher { set => fillBuffer(value); get { return new string(textAfficherBuffer); } } + + + public TextDefilant(TextBlock tb, string text, int timeMs) + { + this.textContainer = tb; + TextAfficher = text; + SetTimer(timeMs); + aTimer.Enabled = true; + } + + private void SetTimer(int timeMs) + { + // Create a timer with a two second interval. + aTimer = new System.Timers.Timer(timeMs); + // Hook up the Elapsed event for the timer. + aTimer.Elapsed += OnTimedEvent; + aTimer.AutoReset = true; + } + + private void OnTimedEvent(Object source, ElapsedEventArgs e) + { + textAfficherBuffer = RotateChar(textAfficherBuffer); + OnPropertyChanged("TextAfficher"); + } + + + private void fillBuffer(string text) + { + char[] charToTrim = { '\r', '\n' }; + text = text.Trim(charToTrim); + double WTB = textContainer.ActualWidth; //With of textBlock container + double WT = MeasureString(text).Width; //With of Text + double WS = MeasureString(" ").Width; //With of whiteSpace + int nbSp = (int)Math.Ceiling((WTB - WT) / WS); //number of space need to fill textBlock + char[] spText = new string(' ', nbSp).ToCharArray(); + this.textAfficherBuffer = new char[nbSp + text.ToCharArray().Length]; + Array.Copy(text.ToCharArray(), 0, this.textAfficherBuffer, 0, text.Length); + Array.Copy(spText, 0, this.textAfficherBuffer, text.Length, spText.Length); + } + private char[] RotateChar(char[] txt) + { + char[] oldTxt = txt; + char[] newTxt = new char[oldTxt.Length]; + char fc = txt[0]; + for (int i = oldTxt.Length - 1; i > 0; i--) + newTxt[((i+1)%oldTxt.Length)] = oldTxt[i]; + newTxt[1] = fc; + return newTxt; + } + + //https://stackoverflow.com/questions/9264398/how-to-calculate-wpf-textblock-width-for-its-known-font-size-and-characters + private Size MeasureString(string candidate) + { + var formattedText = new FormattedText( + candidate, + CultureInfo.CurrentCulture, + FlowDirection.LeftToRight, + new Typeface(this.textContainer.FontFamily, this.textContainer.FontStyle, this.textContainer.FontWeight, this.textContainer.FontStretch), + this.textContainer.FontSize, + Brushes.Black, + new NumberSubstitution(), (TextFormattingMode)1); + + return new Size(formattedText.WidthIncludingTrailingWhitespace, formattedText.Height); + } + } +}