本文共 9768 字,大约阅读时间需要 32 分钟。
前不久,公司举办了15周年庆,其中添加了一个抽奖环节,要从在读学员中随机抽取幸运学员,当然,这个任务就分到了我这里。
最后的效果如下,启动有个欢迎页面,数据是来自Excel的,点击开始则上面的学号及姓名等信息开始随机滚动,显示区域自适应长度变化等。
点击停止则停止滚动,将抽取的学员信息用Graphics绘制到当前窗体结果区域中:
用到的知识点:
1.
2.
3.
4.
5.
本来个人就很在意自己写的程序,所以决定,必须高大上!呵呵。
开始想制作成一个大转盘的形式,先转班级,再转学员。可班级倒好说,但是学员,一个班三十左右学员,一个转盘也放不下啊。即使放进去了,转盘显示也不太好看。所以最后还是放弃了。
正踌躇间,余光瞄到了旁边的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 luckyNumbers = new List (); // 幸运号 31 32 private System.Data.DataTable datas = null; // 所有数据 33 private bool isStart = false; 34 35 #region 窗体构造 36 ///37 /// 窗体构造 38 /// 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 ///60 /// 窗体加载 61 /// 62 /// 63 /// 64 private void FrmMain_Load(object sender, EventArgs e) 65 { 66 this.toolStripStatusLabel_CopyRights.Text = FormBottomCopyRights; 67 } 68 #endregion 69 70 #region 点击搜索按钮 71 ///72 /// 点击搜索按钮 73 /// 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 #endregion142 143 #region 画出姓名及班级信息144 ///145 /// 画出姓名及班级信息146 /// 147 /// 姓名148 /// 班级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 #endregion160 161 #region Timer:用于动态显示当前时间162 ///163 /// Timer:用于动态显示当前时间164 /// 165 /// 166 /// 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 #endregion179 180 #region 汉字转化为拼音181 ///182 /// 汉字转化为拼音183 /// 184 /// 汉字 185 ///全拼 186 private string GetPinYin(string source)187 {188 string result = string.Empty;189 foreach (char item in source)190 {191 try192 {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 catch199 {200 result += item.ToString();201 }202 result += " ";203 }204 return result.Remove(result.Length - 1, 1);205 }206 #endregion207 }208 }
好了,差不多了,其实GaugeControl里面还有很多标尺组件,大家自己下去玩玩吧,代码已经贴出来了,有什么不懂的或者需要程序源码的 留言邮箱就OK了~
最后,谢谢大家的支持,觉得好的话,不要忘了点赞哈。O(∩_∩)O~
【来自:张董'Blogs:,转载请注明出处。】
亲们。码字不容易,觉得不错的话记得点赞哦。。