隨機抽獎軟體

來源:互聯網
上載者:User

標籤:space   switch   oid   try   iss   back   http   replace   order   

Form類

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Tools;
namespace Randomdraw
{
public partial class RandomForm : Form
{
ConfigEntity config;
bool isStart = true;
List<string> nameList;
private System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
Random random = new Random();
int nameIndex = 0;
public RandomForm()
{
InitializeComponent();

//配置
config = loadConfig();//讀取設定檔
if (File.Exists(config.Backgroundfile))//設定背景圖片
{
Image image = Image.FromFile(config.Backgroundfile);
this.BackgroundImage = image;
};
if (config.FullScreen)//是否全屏
{
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
}//if
//設定文本字型
lblName.BackColor = Color.Transparent;
lblName.Font = config.Font;
lblName.ForeColor = config.FontColor;
loadList();//讀取人員名單
timer.Interval = config.Span;//定時器每Tick一次的間隔時間
timer.Tick += new EventHandler(timer_Tick);//綁定Timer每Tick一下時調用的處理方法(簽)名(Method)
}//Form1

/// <summary>
/// 按下鍵盤後的處理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
switch (e.KeyChar)
{
case (char)Keys.Escape:
case (char)Keys.Enter:
case (char)Keys.Back:
case (char)Keys.Delete:
case (char)Keys.End:
System.Environment.Exit(0);
break;
case (char)Keys.Space:
working();
break;
}//switch

}//Form1_KeyPress
/// <summary>
/// 滑鼠點擊後的處理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void RandomForm_MouseClick(object sender, MouseEventArgs e)
{
working();
}//RandomForm_MouseClick

/// <summary>
/// 讀取配置
/// </summary>
ConfigEntity loadConfig()
{
ConfigEntity configEn = new ConfigEntity();

if (File.Exists("config.ini")==false) {
MessageBox.Show("設定檔config.ini不存在,使用預設配置.");
return configEn;
}//if
Ini ini = new Ini("config.ini");
string strAgent;
strAgent=ini.ReadValue("REMOVE_WINNER", "BASE");//是否移除獲獎者
if (strAgent != null && strAgent.Trim().Equals("") == false) {
if (strAgent.Trim().ToUpper().Equals("TRUE") == false)
configEn.RemoveWinner = false;
}//if
strAgent = ini.ReadValue("FULLSCREEN", "BASE");
if (strAgent != null && strAgent.Trim().Equals("") == false)
{
if (strAgent.Trim().ToUpper().Equals("TRUE") == false)
configEn.FullScreen = false;
}//if
strAgent = ini.ReadValue("BACKGROUND_FILE", "BASE");
if (strAgent != null && strAgent.Trim().Equals("") == false)
{
configEn.Backgroundfile = strAgent.Trim();
}//if

int span = 1;
strAgent = ini.ReadValue("SPAN", "BASE");
if (strAgent != null && strAgent.Trim().Equals("") == false)
{
bool isOk = int.TryParse(strAgent.Trim(), out span);
if (isOk == false)
span = 1;
}//if
configEn.Span = span;

float fontSize=100;
strAgent = ini.ReadValue("FONT_SIZE", "BASE");
if (strAgent != null && strAgent.Trim().Equals("") == false)
{
bool isOk = float.TryParse(strAgent.Trim(), out fontSize);
if (isOk == false)
fontSize = 100;
}//if

strAgent = ini.ReadValue("FONT_COLOR", "BASE");
if (strAgent != null && strAgent.Trim().Equals("") == false)
{
configEn.FontColor= Color.FromName(strAgent.Trim());
}//if
string fontFamily = "宋體";
strAgent = ini.ReadValue("FONT_FAMILY", "BASE");
if (strAgent != null && strAgent.Trim().Equals("") == false)
{
fontFamily = strAgent.Trim();
}//if

FontStyle fontStyle= FontStyle.Regular;
strAgent = ini.ReadValue("FONT_STYLE", "BASE");
if (strAgent != null && strAgent.Trim().Equals("") == false)
{
try
{
fontStyle = (FontStyle)Enum.Parse(typeof(FontStyle), strAgent);
}
catch (Exception e) {
Console.WriteLine(e.ToString());
fontStyle = FontStyle.Regular;
}//catch
}//if
configEn.Font = new Font(fontFamily, fontSize, fontStyle);//字型

return configEn;
}//loadConfig

/// <summary>
/// 定時器每Tick一下的處理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void timer_Tick(object sender, EventArgs e)
{
if (nameList != null && nameList.Count > 0)
{
int next = random.Next(0, nameList.Count);
lblName.Text = nameList[next];
nameIndex = next;
}//if
}//timer_Tick
/// <summary>
/// 運行(暫停或開始)
/// </summary>
void working()
{
if (isStart)
start();
else
stop();
}//working
/// <summary>
/// 開始
/// </summary>
void start()
{
isStart = false;
timer.Start();
}//start

/// <summary>
/// 停止
/// </summary>
void stop()
{
isStart = true;
timer_Tick(null, null);//再隨機一次後暫停
timer.Stop();
if(config.RemoveWinner)
RemoveWinner();
}//stop

/// <summary>
/// 移除獲獎者
/// </summary>
void RemoveWinner()
{
if (nameList != null && nameList.Count > 0)
{
try
{
nameList.RemoveAt(nameIndex);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
MessageBox.Show("移除名單數組中Index為"+nameIndex+"的資料失敗.");
}//catch
}//if
}//RemoveWinner

/// <summary>
/// 讀取人員名單
/// </summary>
void loadList()
{
if (File.Exists("list.txt") == false)
{
return;
}//if
try
{
nameList = null;
string[] names;
names = System.IO.File.ReadAllLines("list.txt");
lblName.Text = "Ready";
nameList = new List<string>();
foreach (String name in names)
{
nameList.Add(name.Replace(" ",""));
}//foreach
}
catch (Exception e)
{
lblName.Text = "讀取名單失敗:" + e.Message;
}//
}//loadList


}//Form1
}

 

ConfigEntity類

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Randomdraw
{
class ConfigEntity
{
private string backgroundfile= "backgroud.jpg";
private bool removeWinner=true;
private bool fullScreen=true;
private System.Drawing.Font font = new System.Drawing.Font("宋體", 100);
private string readyWord="Ready";
private int span=1;
private Color fontColor = Color.Black;

public string Backgroundfile
{
get
{
return backgroundfile;
}

set
{
backgroundfile = value;
}
}

public bool RemoveWinner
{
get
{
return removeWinner;
}

set
{
removeWinner = value;
}
}

public bool FullScreen
{
get
{
return fullScreen;
}

set
{
fullScreen = value;
}
}

public Font Font
{
get
{
return font;
}

set
{
font = value;
}
}

public string ReadyWord
{
get
{
return readyWord;
}

set
{
readyWord = value;
}
}

public int Span
{
get
{
return span;
}

set
{
span = value;
}
}

public Color FontColor
{
get
{
return fontColor;
}

set
{
fontColor = value;
}
}
}//ConfigEntity
}//Randomdraw

 

ini檔案操作類:(引用參考:http://www.cnblogs.com/uu102/p/4083501.html)

ini.cs

隨機抽獎軟體

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.