前不久,公司舉辦了15週年慶,其中添加了一個抽獎環節,要從在讀學員中隨機抽取幸運學員,固然,這個任務就分到了我這裏。html
最後的效果以下,啓動有個歡迎頁面,數據是來自Excel的,點擊開始則上面的學號及姓名等信息開始隨機滾動,顯示區域自適應長度變化等。git
點擊中止則中止滾動,將抽取的學員信息用Graphics繪製到當前窗體結果區域中:sql
用到的知識點:
1. DevExpress的GaugeControl的使用數據庫
3. 輸入漢字獲取其拼音顯示dom
4. 讀取Excelide
5. 示例代碼post
原本我的就很在乎本身寫的程序,因此決定,必須高大上!呵呵。this
開始想製做成一個大轉盤的形式,先轉班級,再轉學員。可班級倒好說,可是學員,一個班三十左右學員,一個轉盤也放不下啊。即便放進去了,轉盤顯示也不太好看。因此最後仍是放棄了。url
正躊躇間,餘光瞄到了旁邊的LED燈上面,這種顯示方式也不錯啊。因而想法就轉向了這種形式上面。
但是直接顯示學號 名字 班級的話,界面也太。。。。。那怎麼辦?唉。。。。。。。哎?!DevExpress中貌似有一個GaugeControl這個組件!哈哈。
果斷打開Visual Studio、找一下,沒錯,GaugeControl組件中包含很是很是多種標尺控件,以下:
是否是感受挺炫?呵呵。在這裏面,咱們用的就是GaugeControl裏面的的第一個組件形式:Digital。選擇後添加到窗體中。
到設計視圖中發現:Digital是由兩部分組成:DigitalControl容器和裏面用於顯示的DigitalBackgroundLayerComponent:
裏面的DigitalBackgroundLayerComponent能夠經過ShapeType設置其顯示的外觀樣式,在這裏,咱們使用的是Style 16:
咱們能夠經過DigitalControl的DigitCount屬性和Text屬性,來設置顯示的長度和顯示的文本內容:
TreeList的使用方式和WinForms的TreeView使用基本一致,我說一下怎麼添加列就OK了。
拖拽一個TreeList到窗體中,而後點擊右上角菜單-選擇Run Designer,而後打開TreeList的設計器,咱們先添加如圖三列:
使用代碼添加列的方式很是簡單,模仿WinForms的TreeView的方式添加就好了,在這裏我直接添加的是一個按照順序的String數組:
1 this.treeList_LuckyUser.Nodes.Add(new String[]{ 第一列, 第2列, 第三列 });
是否是和TreeView的同樣?呵呵。這個就很少說了。
這個我前面發過一篇博客有詳細介紹,請查看《漢字轉拼音 - 輸入漢字獲取其拼音》。
讀取Excel的方式和鏈接SQL Server讀取數據是基本一致的,只不過使用的是 using System.Data.OleDb; 命名空間,大概代碼以下:
1 DataTable dt = new DataTable(); // 初始化DataTable,用於存儲數據 2 using (OleDbConnection conn = new OleDbConnection(excelPath)) 3 { 4 string sql = "select * from [表名$]"; 5 OleDbDataAdapter sda = new OleDbDataAdapter(sql, conn); 6 sda.Fill(dt); 7 }
是否是基本一致?詳細的你們能夠上網搜索相關資料,很是多的示例程序,必定注意:表名後面加$。
在這裏我把主窗體的全部代碼貼出來吧:
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 using System.Configuration; 11 using Microsoft.Office.Interop.Excel; 12 using System.Data.OleDb; 13 using System.Threading; 14 using DevExpress.XtraEditors; 15 using Microsoft.International.Converters.PinYinConverter; 16 17 namespace Guying.LuckyApp 18 { 19 public partial class FrmMain : XtraForm 20 { 21 // Excel數據源 22 private static readonly string ConnectionString = ConfigurationManager.AppSettings["ConnectionString"]; // 數據庫鏈接字符串 23 private static readonly string DBPath = ConfigurationManager.AppSettings["DBPath"]; // 數據庫路徑 24 private static readonly string FormBottomCopyRights = ConfigurationManager.AppSettings["FormBottomCopyRights"]; // 窗體底部版權信息 25 private static readonly string FormTitleText = ConfigurationManager.AppSettings["FormTitleText"]; // 窗體標題文本 26 27 ChineseChar chineseChar = null; 28 Random _Random = new Random(); 29 30 private List<int> luckyNumbers = new List<int>(); // 幸運號 31 32 private System.Data.DataTable datas = null; // 全部數據 33 private bool isStart = false; 34 35 #region 窗體構造 36 /// <summary> 37 /// 窗體構造 38 /// </summary> 39 public FrmMain() 40 { 41 InitializeComponent(); 42 this.Text = FormTitleText; 43 44 datas = new System.Data.DataTable(); // 初始化數據集 45 46 string excelPath = ConnectionString + System.Windows.Forms.Application.StartupPath + "\\" + DBPath; // 構造完整的數據庫鏈接字符串 47 48 // 建立鏈接並讀取數據 49 using (OleDbConnection conn = new OleDbConnection(excelPath)) 50 { 51 string sql = "select * from [Student$]"; 52 OleDbDataAdapter sda = new OleDbDataAdapter(sql, conn); 53 sda.Fill(datas); 54 } 55 } 56 #endregion 57 58 #region 窗體加載 59 /// <summary> 60 /// 窗體加載 61 /// </summary> 62 /// <param name="sender"></param> 63 /// <param name="e"></param> 64 private void FrmMain_Load(object sender, EventArgs e) 65 { 66 this.toolStripStatusLabel_CopyRights.Text = FormBottomCopyRights; 67 } 68 #endregion 69 70 #region 點擊搜索按鈕 71 /// <summary> 72 /// 點擊搜索按鈕 73 /// </summary> 74 bool starting = false; 75 private void btnGo_Click(object sender, EventArgs e) 76 { 77 this.isStart = true; 78 starting = !starting; 79 80 if (starting) 81 { 82 int ranNum = 0; // 隨機產生幸運號 83 84 ranNum = _Random.Next(0, this.datas.Rows.Count); // 產生隨機數 85 // 若是已經存在 86 while (this.luckyNumbers.Contains(ranNum)) 87 { 88 ranNum = _Random.Next(0, this.datas.Rows.Count); // 從新生成隨機數 89 } 90 luckyNumbers.Add(ranNum); 91 92 this.timer.Interval = 10; 93 this.timer.Start(); // 開始滾動顯示 94 } 95 } 96 #endregion 97 98 #region Timer,用於滾動幸運數字 99 private void timer_Tick(object sender, EventArgs e) 100 { 101 string luckyName = string.Empty; 102 string luckyGrade = string.Empty; 103 string luckyNumber = string.Empty; 104 string luckyNamePinYin = string.Empty; 105 106 if (!starting) 107 { 108 this.timer.Stop(); 109 110 int currLuckyNum = this.luckyNumbers[this.luckyNumbers.Count - 1]; 111 112 luckyName = this.datas.Rows[currLuckyNum][2].ToString(); 113 luckyGrade = this.datas.Rows[currLuckyNum][3].ToString(); 114 luckyNumber = this.datas.Rows[currLuckyNum][1].ToString(); 115 this.digitalGauge_Name.Text = GetPinYin(luckyName); 116 this.digitalGauge_Name.DigitCount = GetPinYin(luckyName).Length; 117 this.digitalGauge_Number.Text = luckyNumber; 118 this.digitalGauge_Number.DigitCount = luckyNumber.Length; 119 120 DrawInformation(luckyName, luckyGrade); // 畫出姓名及班級信息 121 122 this.treeList_LuckyUser.Nodes.Add(new[] { (this.treeList_LuckyUser.Nodes.Count + 1).ToString(), luckyName, luckyGrade }); 123 124 this.btnGo.Enabled = true; 125 return; 126 } 127 int ranNum = _Random.Next(0, this.datas.Rows.Count); // 產生隨機數 128 129 // 生成當前隨機獲得的人員信息 130 luckyNumber = this.datas.Rows[ranNum][1].ToString(); // 學號 131 luckyName = this.datas.Rows[ranNum][2].ToString(); // 姓名 132 luckyGrade = this.datas.Rows[ranNum][3].ToString(); // 班級 133 luckyNamePinYin = GetPinYin(luckyName); 134 this.digitalGauge_Number.Text = luckyNumber; 135 this.digitalGauge_Number.DigitCount = luckyNumber.Length; 136 this.digitalGauge_Name.Text = luckyNamePinYin; 137 this.digitalGauge_Name.DigitCount = luckyNamePinYin.Length; 138 139 DrawInformation(luckyName, luckyGrade); // 畫出姓名及班級信息 140 } 141 #endregion 142 143 #region 畫出姓名及班級信息 144 /// <summary> 145 /// 畫出姓名及班級信息 146 /// </summary> 147 /// <param name="luckyName">姓名</param> 148 /// <param name="luckyGrade">班級</param> 149 private void DrawInformation(string luckyName, string luckyGrade) 150 { 151 Graphics graphics = this.panelControl_LuckyResult.CreateGraphics(); 152 System.Drawing.Font fontName = new System.Drawing.Font("華文行楷", 100, FontStyle.Bold); 153 System.Drawing.Font fontGrade = new System.Drawing.Font("微軟雅黑", 30, FontStyle.Bold); 154 Brush brush = new SolidBrush(Color.FromArgb(113, 132, 186)); 155 graphics.Clear(this.panelControl_LuckyResult.BackColor); 156 graphics.DrawString(luckyName, fontName, brush, new PointF(10, 60)); 157 graphics.DrawString(luckyGrade, fontGrade, brush, new PointF(150, 230)); 158 } 159 #endregion 160 161 #region Timer:用於動態顯示當前時間 162 /// <summary> 163 /// Timer:用於動態顯示當前時間 164 /// </summary> 165 /// <param name="sender"></param> 166 /// <param name="e"></param> 167 private void timer_TimeNow_Tick(object sender, EventArgs e) 168 { 169 string time = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"); 170 this.toolStripStatusLabel_CopyRights.Text = FormBottomCopyRights + " " + time; 171 if (!this.isStart) 172 { 173 this.digitalGauge_Number.Text = time; 174 175 DrawInformation("智創英傑", "15週年慶 抽獎活動"); 176 } 177 } 178 #endregion 179 180 #region 漢字轉化爲拼音 181 /// <summary> 182 /// 漢字轉化爲拼音 183 /// </summary> 184 /// <param name="source">漢字</param> 185 /// <returns>全拼</returns> 186 private string GetPinYin(string source) 187 { 188 string result = string.Empty; 189 foreach (char item in source) 190 { 191 try 192 { 193 chineseChar = new ChineseChar(item); 194 string t = chineseChar.Pinyins[0].ToString(); 195 t = t.Substring(0, t.Length - 1); 196 result += t.Substring(0, 1).ToUpper() + (t.Length > 1 ? t.Substring(1).ToLower() : ""); 197 } 198 catch 199 { 200 result += item.ToString(); 201 } 202 result += " "; 203 } 204 return result.Remove(result.Length - 1, 1); 205 } 206 #endregion 207 } 208 }
好了,差很少了,其實GaugeControl裏面還有不少標尺組件,你們本身下去玩玩吧,代碼已經貼出來了,有什麼不懂的或者須要程序源碼的 留言郵箱就OK了~
最後,謝謝你們的支持,以爲好的話,不要忘了點贊哈。O(∩_∩)O~