Visualizzare testo scorrevole con C#
A volte capita di voler visualizzare del testo con qualche "effetto" grafico; Ecco uno stralcio di codice C# che vi permette di visualizzarlo (per esempio) in una picturebox.
La classe About.cs:
using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; using System.Threading; namespace AboutProject { class About { FontFamily myFontFamily = new FontFamily("Times New Roman"); Font myFont; PictureBox pBox; double y = 0; public About(PictureBox p) { myFont = new Font(myFontFamily, 20, FontStyle.Regular, GraphicsUnit.Pixel); Linee = new List<string>(); pBox = p; } public List<String> Linee { get; set; } //Avvia il thread public void Start() { Thread t = new Thread(tStart); t.Start(); } private void tStart() { for (int l = 0; l < Linee.Count; l++) { y = 0; for (int i = 0; i <= 36; i++) { Thread.Sleep(30); if (pBox.Created == false) return; pBox.CreateGraphics().Clear(Color.White); float tmpY = (float)Math.Pow(float.Parse((10 * Math.Sin(y) / y).ToString()), 2); pBox.CreateGraphics().DrawString(Linee[l], myFont, Brushes.Blue, 10, tmpY); y = y + 0.5; Thread.Sleep(30); } } tStart(); } } } |
Utilizzo:
Nell'evento load di un form è possibile incollare il seguente codice C#:
About a = new About(pictureBox1);
a.Linee.Add("Questo");
a.Linee.Add("è");
a.Linee.Add("un");
a.Linee.Add("esempio");
a.Linee.Add("creato da");
a.Linee.Add("Giuseppe Iemma");
a.Linee.Add("e disponibile su");
a.Linee.Add("www.myvirtuallab.org");
a.Start(); |