今天开始整理电脑中的胡乱文件,发现有很多自己用来练手用的小文件非常有意思,里面有个这样的程序。有次系里面搞活动,需要对现场观众进行抽奖,当时胡自己用flash设计了个,我觉得有意思就想自己写一个,但是当时我还原后还没有装vs,只有个.net framework。我一直就很喜欢用记事本来写一些简单的数据结构和算法题目,但是winform的程序还没试过,但是我觉得应该是可以的。所以琢磨了一下午,完成了程序。当时心里还是挺高兴的,所以现在还记得这件事,但是当时没有记录下来,结果到前几天我为了生成一个没有dos窗口的后台程序试了很久才想起它。
我不是自虐狂,放着犀利的VS不用,而用记事本去写程序。只是觉得有时候没有必要用到VS这把牛刀来砍蚂蚁,而且我喜欢研究VS它本身到底是怎么来实现某些意想不到的功能的,我也只能做些粗浅的探索。
程序的功能很简单,只是对14排30列的座位进行抽奖,主持人点击开始或停止来抽取几名幸运观众,因为这只是个模拟,我很简单的调用Threading.timer然后直接用随机数来抽取,也许会出现多次中奖的“幸运儿”:) 。主要问题是winform程序的编写,首先要引用System.Windows.Forms命名空间,然后在类中声明需要使用的控件变量,然后在构造函数中初始化控件,然后将控件添加到容器中。其实我也不知道该怎么正确的表述,看代码吧。基础知识看来还得加强啊。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Drawing;
using System.Drawing.Drawing2D;
public class Lottery : Form
{
private Panel panel1;
private Label lblRow;
private Label lblSeat;
private Label numFront;
private Label numRear;
private Button btnStart;
private readonly int interval = 25; //timer的触发时段
private System.Threading.Timer timer = null;
private bool flag=true;
public Lottery()
{
panel1 =new Panel();
lblRow = new Label();
numFront = new Label();
lblSeat = new Label();
numRear = new Label();
btnStart=new Button();
this.panel1.SuspendLayout();
this.SuspendLayout();
this.AutoSize =true;
panel1.BackColor = System.Drawing.Color.Transparent;
this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
panel1.Controls.Add(lblRow);
panel1.Controls.Add(numFront);
panel1.Controls.Add(lblSeat);
panel1.Controls.Add(numRear);
panel1.Controls.Add(btnStart);
this.Controls.Add(panel1);
lblRow.Text="排";
lblRow.Location = new Point(0,20);
lblRow.Size = new Size(20,20);
numFront.Text="*";
numFront.Location = new Point(20,20);
numFront.Size = new Size(20,20);
lblSeat.Text="座";
lblSeat.Location = new Point(40,20);
lblSeat.Size = new Size(20,20);
numRear.Text="*";
numRear.Location = new Point(60,20);
numRear.Size = new Size(20,20);
btnStart.Text = "Start";
btnStart.Location = new Point(100,60);
btnStart.Size = new Size(60,30);
btnStart.Click+=new System.EventHandler(this.btnStart_Click);
}
public void btnStart_Click(object sender,System.EventArgs e)
{
if(btnStart.Text.Equals("Start"))
{
Start();
btnStart.Text = "Pause";
}
else
{
Stop();
btnStart.Text ="Start";
}
}
public void Start()
{
timer = new System.Threading.Timer(new TimerCallback(Walke), null, 100, interval);
}
public void Walke(object o)
{
int tmp;
Random r = new Random();
tmp = r.Next(14);
numFront.Text = tmp.ToString();
tmp = r.Next(30);
numRear.Text = tmp.ToString();
}
public void Stop()
{
timer.Dispose();
}
[STAThread]
public static void Main(string[] args)
{
Application.Run(new Lottery());
}
}
还有个很重要的编译命令:
编辑form时,消掉dos界面
csc /t:winexe Program.cs