Winform简单动态点名实现

再也不用 “ Ctrl + X “ 再用剁手!!!^_^!!!

效果预览点下图:

20201005141239001

代码实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CallTheRoll
{
public partial class mainForm : Form
{

public mainForm()
{
InitializeComponent();
}

#region 变量定义

#region 自定义变量
//宽度
int tablewidth = 150;
//高度
int tableheight = 70;
//列数
int numcolumn = 7;
//行数
int numrow = 6;
//学生数
int numstu = 41;
//字体
string thefont = "宋体";
//字号
int thefontsize = 31;
#endregion

#region 公共变量
int idmak = 1;
int cycleindex = 0;
int cycleindexmak = 0;
int chooseid = 1;
string exepath = System.Environment.CurrentDirectory;
int allRows = 0;
#endregion

#endregion

private void Form1_Paint(object sender, PaintEventArgs e)
{
Pen pen = new Pen(Color.Red, 2);//笔的颜色与粗细

for (int i = 30; i <= (tablewidth * numcolumn + 30); i = i + tablewidth)//步长150
{

Point point = new Point(i, 30);

Point point1 = new Point(i, (tableheight * numrow + 30));

e.Graphics.DrawLine(pen, point, point1);//画竖线

}
for (int i = 30; i <= (tableheight * numrow + 30); i = i + tableheight)
{

Point point2 = new Point(30, i);

Point point3 = new Point((tablewidth * numcolumn + 30), i);

e.Graphics.DrawLine(pen, point2, point3);//画横线

}
}

private void Form1_Load(object sender, EventArgs e)
{
ReadTheInformation();
this.Width = tablewidth * numcolumn + 80;
this.Height = tableheight * numrow + 175;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
CallTheRoll.Anchor = AnchorStyles.Bottom;
}

#region 函数封装
public void Initialize()
{
for (int j = 0; j < numrow; j++)
{
for (int i = 0; i < numcolumn; i++)
{
int id;
id = j * numcolumn + i + 1;
if (id > numstu)
{
break;
}
Label lab1 = new Label();
lab1.Location = new Point(30 + tablewidth * i, 30 + tableheight * j);
lab1.Visible = true;
lab1.Width = tablewidth - 1;//设置宽度
lab1.Height = tableheight - 1;//设置高度
lab1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
lab1.BackColor = Color.White;
if (id < 10)
{
lab1.Text = "0" + id.ToString();
}
else
{
lab1.Text = id.ToString();
}
lab1.Font = new Font(thefont, thefontsize);//"宋体, 31"
this.Controls.Add(lab1);//将控件添加到当前窗体
lab1.Name = id.ToString();
}
}
}

public void ReadTheInformation()
{
flag = false;
try
{

string TheInformation = exepath + "//CustomInformation.xlsx";
XSSFWorkbook workbook = null;
using (FileStream fs = File.Open(TheInformation, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
//把xls文件读入workbook变量里,之后就可以关闭了
workbook = new XSSFWorkbook(fs);//07版本(.xlsx)
fs.Close();
}
//读取工作表
ISheet worksheet = workbook.GetSheetAt(0); //读取第一个sheet

//总行数
allRows = worksheet.LastRowNum;//总行数-1 起始为0

//宽度
tablewidth = Convert.ToInt32(worksheet.GetRow(0).GetCell(2).ToString());
//高度
tableheight = Convert.ToInt32(worksheet.GetRow(1).GetCell(2).ToString());
//学生数
numstu = Convert.ToInt32(worksheet.GetRow(2).GetCell(2).ToString());
//列数
numcolumn = Convert.ToInt32(worksheet.GetRow(3).GetCell(2).ToString());
//行数
numrow = Convert.ToInt32(worksheet.GetRow(4).GetCell(2).ToString());
//字体
thefont = worksheet.GetRow(5).GetCell(2).ToString();
//字号
thefontsize = Convert.ToInt32(worksheet.GetRow(6).GetCell(2).ToString());

int row_column = numcolumn * numrow;

if ((allRows + 1) != numstu)
{
MessageBox.Show("学生数量错误!");
System.Environment.Exit(0);
}
else if (row_column < numstu)
{
MessageBox.Show("学生数 > 行数 * 列数");
System.Environment.Exit(0);
}

Initialize();

for (int informationid = 1; informationid <= numstu; informationid++)
{
FillInInformation(informationid, worksheet.GetRow(informationid - 1).GetCell(0).ToString());
}
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
}

public void FillInInformation(int stuno, string stuname)
{
Control[] lb = Controls.Find(stuno.ToString(), true);
lb[0].Text = stuname;
}

public void PopUpPrompt(int stuno)
{
Control[] lb = Controls.Find(stuno.ToString(), true);
string stuname = lb[0].Text;
PopUpPromptForm pupfrm = new PopUpPromptForm(stuname);
pupfrm.ShowDialog();
//MessageBox.Show("没错就是你!有请可爱的 “ " + stuname + " ” 同学!鼓掌!!!","恭喜:",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);
}

public void CycleAnimation()
{
if (idmak != 1)
{
createtimer.Dispose();
deletetimer.Dispose();
choosetimer.Dispose();
choosedeltimer.Dispose();
Control[] ctrl = Controls.Find(((idmak - 1).ToString()), true);
ctrl[0].BackColor = Color.White;
idmak = 1;
createtimer.Start();
}
else
{
createtimer.Dispose();
deletetimer.Dispose();
createtimer.Start();
}
}

public void ChooseCycle()
{
Random chooserand = new Random();
chooseid = chooserand.Next(1, (numstu + 1));
idmak = 1;
choosetimer.Start();
}
#endregion

#region btn_click事件
private void CallTheRoll_Click(object sender, EventArgs e)
{
createtimer.Interval = 950;
deletetimer.Interval = 950;
cycleindexmak = 0;
Random cycleindexrandom = new Random();
cycleindex = cycleindexrandom.Next(1, 3);
CycleAnimation();
}
#endregion

#region timer事件
private void createtimer_Tick(object sender, EventArgs e)
{
createtimer.Interval = deletetimer.Interval;
if (idmak <= numstu)
{
if (idmak == 1)
{
deletetimer.Start();
}
Control[] ctrl = Controls.Find((idmak.ToString()), true);
ctrl[0].BackColor = Color.Red;
//加速调节,单纯的改BUG
if (idmak == 4)
{
Control[] ctrl3 = Controls.Find("3", true);
ctrl3[0].BackColor = Color.White;
}
idmak++;
if (idmak > numstu)
{
createtimer.Dispose();
}
}
}

private void deletetimer_Tick(object sender, EventArgs e)
{
if (idmak <= (numstu + 1))
{
if (deletetimer.Interval > 100)
{
deletetimer.Interval -= 150;
}
Control[] ctrl = Controls.Find(((idmak - 1).ToString()), true);
ctrl[0].BackColor = Color.White;
if (cycleindexmak == 0)
{
//加速调节,单纯的改BUG
if (idmak == 4)
{
Control[] ctrl2 = Controls.Find("2", true);
ctrl2[0].BackColor = Color.White;
Control[] ctrl3 = Controls.Find("3", true);
ctrl3[0].BackColor = Color.Red;
}
}
if (idmak == (numstu + 1))
{
deletetimer.Dispose();
cycleindexmak += 1;
if (cycleindexmak < cycleindex)
{
createtimer.Dispose();
idmak = 1;
createtimer.Start();
}
else
{
ChooseCycle();
}
}
}
}


private void choosetimer_Tick(object sender, EventArgs e)
{
if (idmak <= chooseid)
{
if (idmak == 1)
{
choosedeltimer.Start();
}
Control[] ctrl = Controls.Find((idmak.ToString()), true);
ctrl[0].BackColor = Color.Red;
idmak++;
if (idmak > chooseid)
{
PopUpPrompt(idmak - 1);
choosetimer.Dispose();
}
}
}

private void choosedeltimer_Tick(object sender, EventArgs e)
{
if (idmak <= chooseid)
{
Control[] ctrl = Controls.Find(((idmak - 1).ToString()), true);
ctrl[0].BackColor = Color.White;
if (idmak == chooseid)
{
choosedeltimer.Dispose();
}
}
}
# endregion

private void configbtn_Click(object sender, EventArgs e)
{
ConfigHintForm chfrm = new ConfigHintForm();
chfrm.ShowDialog();
}

private void helpbtn_Click(object sender, EventArgs e)
{
//定义一个ProcessStartInfo实例
System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo();
//设置启动进程的初始目录
info.WorkingDirectory = Application.StartupPath;
//设置启动进程的应用程序或文档名
info.FileName = "readme.docx";
//设置启动进程的参数
info.Arguments = "";
//启动由包含进程启动信息的进程资源
try
{
System.Diagnostics.Process.Start(info);
}
catch (System.ComponentModel.Win32Exception we)
{
MessageBox.Show(this, we.Message);
}
}

}
}


一些其他的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//int randid;
//private void randomtimer_Tick(object sender, EventArgs e)
//{
// Control[] ctrl = Controls.Find(randid.ToString(), true);
// ctrl[0].BackColor = Color.Red;
// deletetimer.Start();
//}

//private void delrandomtimer_Tick(object sender, EventArgs e)
//{
// Control[] ctrl = Controls.Find(randid.ToString(), true);
// ctrl[0].BackColor = Color.White;
//}

//private void RandCallTheRol_Click(object sender, EventArgs e)
//{
// for (int randi = 0; randid < 10; randid++)
// {
// Random r = new Random();
// randid = r.Next(1, 42);
// }
//}




//if (idmak + 1 < 41)
//{
// if (idmak == 0)
// {
// deletetimer.Start();
// }
// ++idmak;
// int i = idmak % 7;
// int j = idmak / 7;
// Label lab1 = new Label();
// lab1.Location = new Point(30 + 100 * i, 30 + 80 * j);
// lab1.Visible = true;
// lab1.Width = 99;//设置宽度
// lab1.Height = 79;//设置高度
// lab1.BorderStyle = BorderStyle.Fixed3D;
// if (idmak + 1 < 10)
// {
// lab1.Text = "0" + (idmak + 1).ToString();
// }
// else
// {
// lab1.Text = (idmak + 1).ToString();
// }
// lab1.Font = new Font("宋体", 50);//"宋体, 9pt"
// lab1.BackColor = Color.Red;
// this.Controls.Add(lab1);//将控件添加到当前窗体
// lab1.BringToFront();
//}
//else
//{
// createtimer.Stop();
//}

//Control[] ctrl = Controls.Find("1", true);
//ctrl[0].BackColor = Color.Red;
//for (int delctrlmak = 0; delctrlmak < 6; delctrlmak++)
//{
// foreach (Control ctrl in this.Controls)
// {
// if (ctrl != button1 && ctrl != CallTheRoll)
// {
// Controls.Remove(ctrl);
// }
// }
//}

//int delidmak = idmak - 1;
//int i = delidmak % 7;
//int j = delidmak / 7;
//Label lab1 = new Label();
//lab1.Location = new Point(30 + 100 * i, 30 + 80 * j);
//lab1.Visible = true;
//lab1.Width = 99;//设置宽度
//lab1.Height = 79;//设置高度
//lab1.BorderStyle = BorderStyle.Fixed3D;
//if (delidmak + 1 < 10)
//{
// lab1.Text = "0" + (delidmak + 1).ToString();
//}
//else
//{
// lab1.Text = (delidmak + 1).ToString();
//}
//lab1.Font = new Font("宋体", 50);//"宋体, 9pt"
////lab1.BackColor = Color.Red;
//this.Controls.Add(lab1);//将控件添加到当前窗体
//lab1.BringToFront();


//int x = lab1.Location.X;//获取控件X坐标

//int y = lab1.Location.Y;//获取控件X坐标

下载地址:点击下载h9jz1g))