經理提了個小功能 , 從數據庫讀取出關鍵詞 然后窗體放個背景圖片 關鍵詞從圖片中間一個一個出來 然后慢慢向四周發散的效果 。文字大小和顏色 可以控制
首先設置 Form的 BackgroundImage 屬性 設置窗體的背景圖
然后添加2個timer 控件 timer1和timer2,設置Interval =300 , 其中 timer1 用來隔0.3s通過GDI+ DrawString, timer2 用來隔0.3s 添加關鍵詞
上代碼:
private void timer1_Tick(object sender, EventArgs e) { if (SC != null) { img = Image.FromFile(".\\logo.png"); SC.Reflash(); Graphics g = Graphics.FromImage(img); SC.Print(g); g.Dispose(); this.BackgroundImage = img; } } private void timer2_Tick(object sender, EventArgs e) { List<string> names = str; List<int> list = new List<int>(); if (!list.Contains(chunum)) { if (chunum < names.Count) { list.Add(chunum); lock (SC) { SC.AddKey(names[chunum], chunum); } chunum++; } } } List<string> str = new List<string> { "小胖人", "我的信", "hello", "加油", "你是誰", "張三是", "理順了", "萬城" };
SC是個自定義類 ShowClass 里面有字體大小 顏色
public class ShowClass { public int firstVal = 0; public int secondVal = 0; public Size winsize; public int keylens; public int keylene; public int fontsizes; public int fontsizee; public Color fontcolor; public List<View> lv = new List<View>(); public ShowClass(Size size, int keylens, int keylene, int fontsizes, int fontsizee, Color fontcolor) { this.winsize = size; this.keylens = keylens; this.keylene = keylene; this.fontsizes = fontsizes; this.fontsizee = fontsizee; this.fontcolor = fontcolor; } public void AddKey(string key, int chunum) { if (lv.Count() < keylene) { lv.Add(new View(key, chunum, fontsizes, fontsizee, fontcolor, winsize)); } } internal View IsInView(int x, int y) { foreach (View v in lv) { if (v.isInRec(new Point(x, y))) { return v; } } return null; } internal void Print(Graphics g) { foreach (View x in lv) { x.Print(g); } } internal void Reflash() { for (int i = 0; i < lv.Count; i++) { if (i == 0 && firstVal < 3) { firstVal++; lv[i].Reflash(0); } else if (i > 0 && secondVal < 150) { secondVal++; lv[i].Reflash(i); } } }
Print 里面就是通過GDI+ 來實現在背景圖上畫字 g.DrawString(text, new Font(font, fsize), new SolidBrush(cl), new Point(x, y));
第二個參數可以控制字體大小 最后一個參數Point就是坐標 可以通過隨機數來確定文字發散方向及x, y 的距離
來個最終效果圖 關鍵詞發散方向和 距離都是隨機的
原文地址:https://www.cnblogs.com/alonglonga/p/12288781.html 小赫赫首發
有問題聯系Q: 591811930