Winform小软件 —— 摇奖机
Winform小软件 —— 摇奖机
代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace 摇奖机
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
//Form的相关“属性”设置:
this.FormBorderStyle = FormBorderStyle.None;
this.TransparencyKey = Color.Yellow; //将某颜色设置为透明
this.BackgroundImage = new Bitmap("Images/Transball.bmp");
}
bool isFormMove = false; //是否移动
int x, y; //获得鼠标初始位置
//鼠标按下
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isFormMove = true;
x = e.X;
y = e.Y;
}
}
//鼠标移动
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (isFormMove)
{
//获得鼠标当前位置
Point p = Form.MousePosition;
//鼠标当前位置 - 鼠标初始位置 = 窗体要移动的位置
this.Location = new Point(p.X - x, p.Y - y);
}
}
//鼠标释放
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
isFormMove = false;
}
/*
* 问题:
* 在Form里加了一个Button后,Form的KeyPress事件在按下Enter键时就不能触发了?
* 解决方案:
* 重写该方法实现,扩展ProcessDialogKey方法调用Form1_KeyPress事件 */
protected override bool ProcessDialogKey(Keys keyData)
{
if (keyData == Keys.Return)
{
//this.KeyPreview = true;
KeyPressEventArgs myKeyPressEventArgs = new KeyPressEventArgs(Convert.ToChar(keyData));
Form1_KeyPress(this, myKeyPressEventArgs);
}
return base.ProcessDialogKey(keyData);
}
int n = 1;
Thread thread1, thread2, thread3, thread4, thread5, thread6;
//按键事件
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (n % 2 == 1)
{
n++;
thread1 = new Thread(randomLabelText);
thread1.Start(this.label1);
thread1.Join(10); //让别的线程停止
thread2 = new Thread(randomLabelText);
thread2.Start(this.label2);
thread2.Join(5); //让别的线程停止
thread3 = new Thread(randomLabelText);
thread3.Start(this.label3);
thread3.Join(7);
thread4 = new Thread(randomLabelText);
thread4.Start(this.label4);
thread4.Join(14);
thread5 = new Thread(randomLabelText);
thread5.Start(this.label5);
thread5.Join(3);
thread6 = new Thread(randomLabelText);
thread6.Start(this.label6);
thread6.Join(13);
}
else
{
thread1.Abort(); thread2.Abort(); thread3.Abort();
thread4.Abort(); thread5.Abort(); thread6.Abort();
label7.Text = "中奖号码:" + label1.Text + " " + label2.Text + " " + label3.Text;
label7.Text += " " + label4.Text + " " + label5.Text + " " + label6.Text;
n--;
}
}
//产生随机数给Label控件
private void randomLabelText(object obj)
{
Label lb = obj as Label;
Random rand = new Random();
while (true)
{
int i = rand.Next(0, 10);
lb.Text = i.ToString();
Thread.Sleep(100);
}
}
private void button1_MouseClick(object sender, MouseEventArgs e)
{
Application.Exit();
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace 摇奖机
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
//Form的相关“属性”设置:
this.FormBorderStyle = FormBorderStyle.None;
this.TransparencyKey = Color.Yellow; //将某颜色设置为透明
this.BackgroundImage = new Bitmap("Images/Transball.bmp");
}
bool isFormMove = false; //是否移动
int x, y; //获得鼠标初始位置
//鼠标按下
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isFormMove = true;
x = e.X;
y = e.Y;
}
}
//鼠标移动
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (isFormMove)
{
//获得鼠标当前位置
Point p = Form.MousePosition;
//鼠标当前位置 - 鼠标初始位置 = 窗体要移动的位置
this.Location = new Point(p.X - x, p.Y - y);
}
}
//鼠标释放
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
isFormMove = false;
}
/*
* 问题:
* 在Form里加了一个Button后,Form的KeyPress事件在按下Enter键时就不能触发了?
* 解决方案:
* 重写该方法实现,扩展ProcessDialogKey方法调用Form1_KeyPress事件 */
protected override bool ProcessDialogKey(Keys keyData)
{
if (keyData == Keys.Return)
{
//this.KeyPreview = true;
KeyPressEventArgs myKeyPressEventArgs = new KeyPressEventArgs(Convert.ToChar(keyData));
Form1_KeyPress(this, myKeyPressEventArgs);
}
return base.ProcessDialogKey(keyData);
}
int n = 1;
Thread thread1, thread2, thread3, thread4, thread5, thread6;
//按键事件
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (n % 2 == 1)
{
n++;
thread1 = new Thread(randomLabelText);
thread1.Start(this.label1);
thread1.Join(10); //让别的线程停止
thread2 = new Thread(randomLabelText);
thread2.Start(this.label2);
thread2.Join(5); //让别的线程停止
thread3 = new Thread(randomLabelText);
thread3.Start(this.label3);
thread3.Join(7);
thread4 = new Thread(randomLabelText);
thread4.Start(this.label4);
thread4.Join(14);
thread5 = new Thread(randomLabelText);
thread5.Start(this.label5);
thread5.Join(3);
thread6 = new Thread(randomLabelText);
thread6.Start(this.label6);
thread6.Join(13);
}
else
{
thread1.Abort(); thread2.Abort(); thread3.Abort();
thread4.Abort(); thread5.Abort(); thread6.Abort();
label7.Text = "中奖号码:" + label1.Text + " " + label2.Text + " " + label3.Text;
label7.Text += " " + label4.Text + " " + label5.Text + " " + label6.Text;
n--;
}
}
//产生随机数给Label控件
private void randomLabelText(object obj)
{
Label lb = obj as Label;
Random rand = new Random();
while (true)
{
int i = rand.Next(0, 10);
lb.Text = i.ToString();
Thread.Sleep(100);
}
}
private void button1_MouseClick(object sender, MouseEventArgs e)
{
Application.Exit();
}
}
}