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();
}
}
}
作者: XuGang 网名:钢钢 |
出处: http://xugang.cnblogs.com |
声明: 本文版权归作者和博客园共有。转载时必须保留此段声明,且在文章页面明显位置给出原文连接地址! |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架