您的位置:首页

DevExpress - 使用 GaugeControl 标尺组件制作抽奖程序 附源码

2015-01-12 17:51 429 查看
  前不久,公司举办了15周年庆,其中添加了一个抽奖环节,要从在读学员中随机抽取幸运学员,当然,这个任务就分到了我这里。

  最后的效果如下,启动有个欢迎页面,数据是来自Excel的,点击开始则上面的学号及姓名等信息开始随机滚动,显示区域自适应长度变化等。

  点击停止则停止滚动,将抽取的学员信息用Graphics绘制到当前窗体结果区域中:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using Microsoft.Office.Interop.Excel;
using System.Data.OleDb;
using System.Threading;
using DevExpress.XtraEditors;
using Microsoft.International.Converters.PinYinConverter;

namespace Guying.LuckyApp
{
public partial class FrmMain : XtraForm
{
// Excel数据源
private static readonly string ConnectionString = ConfigurationManager.AppSettings["ConnectionString"]; // 数据库连接字符串
private static readonly string DBPath = ConfigurationManager.AppSettings["DBPath"]; // 数据库路径
private static readonly string FormBottomCopyRights = ConfigurationManager.AppSettings["FormBottomCopyRights"];  // 窗体底部版权信息
private static readonly string FormTitleText = ConfigurationManager.AppSettings["FormTitleText"];   // 窗体标题文本

ChineseChar chineseChar = null;
Random _Random = new Random();

private List<int> luckyNumbers = new List<int>();   // 幸运号

private System.Data.DataTable datas = null;    // 所有数据
private bool isStart = false;

#region 窗体构造
/// <summary>
/// 窗体构造
/// </summary>
public FrmMain()
{
InitializeComponent();
this.Text = FormTitleText;

datas = new System.Data.DataTable();   // 初始化数据集

string excelPath = ConnectionString + System.Windows.Forms.Application.StartupPath + "\\" + DBPath; // 构造完整的数据库连接字符串

// 创建连接并读取数据
using (OleDbConnection conn = new OleDbConnection(excelPath))
{
string sql = "select * from [Student$]";
OleDbDataAdapter sda = new OleDbDataAdapter(sql, conn);
sda.Fill(datas);
}
}
#endregion

#region 窗体加载
/// <summary>
/// 窗体加载
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FrmMain_Load(object sender, EventArgs e)
{
this.toolStripStatusLabel_CopyRights.Text = FormBottomCopyRights;
}
#endregion

#region 点击搜索按钮
/// <summary>
/// 点击搜索按钮
/// </summary>
bool starting = false;
private void btnGo_Click(object sender, EventArgs e)
{
this.isStart = true;
starting = !starting;

if (starting)
{
int ranNum = 0; // 随机产生幸运号

ranNum = _Random.Next(0, this.datas.Rows.Count);    // 产生随机数
// 如果已经存在
while (this.luckyNumbers.Contains(ranNum))
{
ranNum = _Random.Next(0, this.datas.Rows.Count);    // 重新生成随机数
}
luckyNumbers.Add(ranNum);

this.timer.Interval = 10;
this.timer.Start(); // 开始滚动显示
}
}
#endregion

#region Timer,用于滚动幸运数字
private void timer_Tick(object sender, EventArgs e)
{
string luckyName = string.Empty;
string luckyGrade = string.Empty;
string luckyNumber = string.Empty;
string luckyNamePinYin = string.Empty;

if (!starting)
{
this.timer.Stop();

int currLuckyNum = this.luckyNumbers[this.luckyNumbers.Count - 1];

luckyName = this.datas.Rows[currLuckyNum][2].ToString();
luckyGrade = this.datas.Rows[currLuckyNum][3].ToString();
luckyNumber = this.datas.Rows[currLuckyNum][1].ToString();
this.digitalGauge_Name.Text = GetPinYin(luckyName);
this.digitalGauge_Name.DigitCount = GetPinYin(luckyName).Length;
this.digitalGauge_Number.Text = luckyNumber;
this.digitalGauge_Number.DigitCount = luckyNumber.Length;

DrawInformation(luckyName, luckyGrade); // 画出姓名及班级信息

this.treeList_LuckyUser.Nodes.Add(new[] { (this.treeList_LuckyUser.Nodes.Count + 1).ToString(), luckyName, luckyGrade });

this.btnGo.Enabled = true;
return;
}
int ranNum = _Random.Next(0, this.datas.Rows.Count);    // 产生随机数

// 生成当前随机得到的人员信息
luckyNumber = this.datas.Rows[ranNum][1].ToString(); // 学号
luckyName = this.datas.Rows[ranNum][2].ToString();   // 姓名
luckyGrade = this.datas.Rows[ranNum][3].ToString();  // 班级
luckyNamePinYin = GetPinYin(luckyName);
this.digitalGauge_Number.Text = luckyNumber;
this.digitalGauge_Number.DigitCount = luckyNumber.Length;
this.digitalGauge_Name.Text = luckyNamePinYin;
this.digitalGauge_Name.DigitCount = luckyNamePinYin.Length;

DrawInformation(luckyName, luckyGrade); // 画出姓名及班级信息
}
#endregion

#region 画出姓名及班级信息
/// <summary>
/// 画出姓名及班级信息
/// </summary>
/// <param name="luckyName">姓名</param>
/// <param name="luckyGrade">班级</param>
private void DrawInformation(string luckyName, string luckyGrade)
{
Graphics graphics = this.panelControl_LuckyResult.CreateGraphics();
System.Drawing.Font fontName = new System.Drawing.Font("华文行楷", 100, FontStyle.Bold);
System.Drawing.Font fontGrade = new System.Drawing.Font("微软雅黑", 30, FontStyle.Bold);
Brush brush = new SolidBrush(Color.FromArgb(113, 132, 186));
graphics.Clear(this.panelControl_LuckyResult.BackColor);
graphics.DrawString(luckyName, fontName, brush, new PointF(10, 60));
graphics.DrawString(luckyGrade, fontGrade, brush, new PointF(150, 230));
}
#endregion

#region Timer:用于动态显示当前时间
/// <summary>
/// Timer:用于动态显示当前时间
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer_TimeNow_Tick(object sender, EventArgs e)
{
string time = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
this.toolStripStatusLabel_CopyRights.Text = FormBottomCopyRights + "  " + time;
if (!this.isStart)
{
this.digitalGauge_Number.Text = time;

DrawInformation("智创英杰", "15周年庆 抽奖活动");
}
}
#endregion

#region 汉字转化为拼音
/// <summary>
/// 汉字转化为拼音
/// </summary>
/// <param name="source">汉字</param>
/// <returns>全拼</returns>
private string GetPinYin(string source)
{
string result = string.Empty;
foreach (char item in source)
{
try
{
chineseChar = new ChineseChar(item);
string t = chineseChar.Pinyins[0].ToString();
t = t.Substring(0, t.Length - 1);
result += t.Substring(0, 1).ToUpper() + (t.Length > 1 ? t.Substring(1).ToLower() : "");
}
catch
{
result += item.ToString();
}
result += " ";
}
return result.Remove(result.Length - 1, 1);
}
#endregion
}
}


主窗体所有代码

  好了,差不多了,其实GaugeControl里面还有很多标尺组件,大家自己下去玩玩吧,代码已经贴出来了,有什么不懂的或者需要程序源码的 留言邮箱就OK了~

  最后,谢谢大家的支持,觉得好的话,不要忘了点赞哈。O(∩_∩)O~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐