InkCanvasForClass/Ink Canvas/RandWindow.xaml.cs

239 lines
8.7 KiB
C#
Raw Normal View History

2022-04-24 23:10:20 +08:00
using Microsoft.VisualBasic;
using ModernWpf.Controls;
2021-10-28 00:48:33 +08:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
2021-10-30 23:55:38 +08:00
using System.Threading;
2021-10-28 00:48:33 +08:00
using System.Windows;
using System.Windows.Input;
namespace Ink_Canvas
{
/// <summary>
/// Interaction logic for RandWindow.xaml
/// </summary>
public partial class RandWindow : Window
{
public RandWindow()
{
InitializeComponent();
}
2021-12-05 22:49:08 +08:00
public RandWindow(bool IsAutoClose)
{
InitializeComponent();
isAutoClose = IsAutoClose;
2023-02-12 22:50:17 +08:00
new Thread(new ThreadStart(() =>
{
2021-12-05 22:49:08 +08:00
Thread.Sleep(100);
Application.Current.Dispatcher.Invoke(() =>
{
BorderBtnRand_MouseUp(BorderBtnRand, null);
});
})).Start();
}
2021-12-05 22:32:45 +08:00
public static int randSeed = 0;
2021-12-05 22:49:08 +08:00
public bool isAutoClose = false;
2021-10-28 00:48:33 +08:00
public int TotalCount = 1;
2021-10-30 23:55:38 +08:00
public int PeopleCount = 60;
2021-10-28 00:48:33 +08:00
public List<string> Names = new List<string>();
private void BorderBtnAdd_MouseUp(object sender, MouseButtonEventArgs e)
{
2021-10-30 23:55:38 +08:00
if (TotalCount >= PeopleCount) return;
2021-10-28 00:48:33 +08:00
TotalCount++;
LabelNumberCount.Content = TotalCount.ToString();
2021-10-30 23:55:38 +08:00
SymbolIconStart.Symbol = Symbol.People;
2021-10-28 00:48:33 +08:00
}
private void BorderBtnMinus_MouseUp(object sender, MouseButtonEventArgs e)
{
if (TotalCount < 2) return;
TotalCount--;
LabelNumberCount.Content = TotalCount.ToString();
2021-10-30 23:55:38 +08:00
if (TotalCount == 1)
{
SymbolIconStart.Symbol = Symbol.Contact;
}
2021-10-28 00:48:33 +08:00
}
private void BorderBtnRand_MouseUp(object sender, MouseButtonEventArgs e)
{
2022-01-24 00:06:19 +08:00
Random random = new Random();// randSeed + DateTime.Now.Millisecond / 10 % 10);
2021-10-28 00:48:33 +08:00
string outputString = "";
2021-10-30 23:55:38 +08:00
List<string> outputs = new List<string>();
List<int> rands = new List<int>();
LabelOutput2.Visibility = Visibility.Collapsed;
LabelOutput3.Visibility = Visibility.Collapsed;
BorderBtnRandCover.Visibility = Visibility.Visible;
new Thread(new ThreadStart(() =>
2021-10-28 00:48:33 +08:00
{
2021-10-30 23:55:38 +08:00
for (int i = 0; i < 5; i++)
2021-10-28 00:48:33 +08:00
{
2021-10-31 17:23:23 +08:00
int rand = random.Next(1, PeopleCount + 1);
2021-10-30 23:55:38 +08:00
while (rands.Contains(rand))
{
2021-10-31 17:23:23 +08:00
rand = random.Next(1, PeopleCount + 1);
2021-10-30 23:55:38 +08:00
}
rands.Add(rand);
2021-10-31 17:23:23 +08:00
if (rands.Count >= PeopleCount) rands = new List<int>();
2021-10-30 23:55:38 +08:00
Application.Current.Dispatcher.Invoke(() =>
{
if (Names.Count != 0)
{
2021-10-31 17:23:23 +08:00
LabelOutput.Content = Names[rand - 1];
2021-10-30 23:55:38 +08:00
}
else
{
LabelOutput.Content = rand.ToString();
}
});
Thread.Sleep(150);
2021-10-28 00:48:33 +08:00
}
2021-10-30 23:55:38 +08:00
rands = new List<int>();
Application.Current.Dispatcher.Invoke(() =>
2021-10-28 00:48:33 +08:00
{
2021-10-30 23:55:38 +08:00
for (int i = 0; i < TotalCount; i++)
{
2021-10-31 17:23:23 +08:00
int rand = random.Next(1, PeopleCount + 1);
2021-10-30 23:55:38 +08:00
while (rands.Contains(rand))
{
2021-10-31 17:23:23 +08:00
rand = random.Next(1, PeopleCount + 1);
2021-10-30 23:55:38 +08:00
}
rands.Add(rand);
2021-10-31 17:23:23 +08:00
if (rands.Count >= PeopleCount) rands = new List<int>();
2021-10-30 23:55:38 +08:00
if (Names.Count != 0)
{
2021-10-31 17:23:23 +08:00
outputs.Add(Names[rand - 1]);
outputString += Names[rand - 1] + Environment.NewLine;
2021-10-30 23:55:38 +08:00
}
else
{
outputs.Add(rand.ToString());
outputString += rand.ToString() + Environment.NewLine;
}
}
if (TotalCount <= 5)
{
LabelOutput.Content = outputString.ToString().Trim();
}
else if (TotalCount <= 10)
{
LabelOutput2.Visibility = Visibility.Visible;
outputString = "";
for (int i = 0; i < (outputs.Count + 1) / 2; i++)
{
outputString += outputs[i].ToString() + Environment.NewLine;
}
LabelOutput.Content = outputString.ToString().Trim();
outputString = "";
for (int i = (outputs.Count + 1) / 2; i < outputs.Count; i++)
{
outputString += outputs[i].ToString() + Environment.NewLine;
}
LabelOutput2.Content = outputString.ToString().Trim();
}
else
{
LabelOutput2.Visibility = Visibility.Visible;
LabelOutput3.Visibility = Visibility.Visible;
outputString = "";
for (int i = 0; i < (outputs.Count + 1) / 3; i++)
{
outputString += outputs[i].ToString() + Environment.NewLine;
}
LabelOutput.Content = outputString.ToString().Trim();
outputString = "";
for (int i = (outputs.Count + 1) / 3; i < (outputs.Count + 1) * 2 / 3; i++)
{
outputString += outputs[i].ToString() + Environment.NewLine;
}
LabelOutput2.Content = outputString.ToString().Trim();
outputString = "";
for (int i = (outputs.Count + 1) * 2 / 3; i < outputs.Count; i++)
{
outputString += outputs[i].ToString() + Environment.NewLine;
}
LabelOutput3.Content = outputString.ToString().Trim();
}
BorderBtnRandCover.Visibility = Visibility.Collapsed;
2021-12-05 22:49:08 +08:00
if (isAutoClose)
{
2023-02-12 22:50:17 +08:00
new Thread(new ThreadStart(() =>
{
2021-12-05 22:49:08 +08:00
Thread.Sleep(1500);
Application.Current.Dispatcher.Invoke(() =>
{
Close();
});
})).Start();
}
2021-10-30 23:55:38 +08:00
});
})).Start();
2021-10-28 00:48:33 +08:00
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
2021-10-30 23:55:38 +08:00
Names = new List<string>();
2023-05-17 21:51:13 +08:00
if (File.Exists(App.RootPath + "Names.txt"))
2021-10-28 00:48:33 +08:00
{
2023-05-17 21:51:13 +08:00
string[] fileNames = File.ReadAllLines(App.RootPath + "Names.txt");
2022-04-24 23:10:20 +08:00
string[] replaces = new string[0];
2023-05-17 21:51:13 +08:00
if (File.Exists(App.RootPath + "Replace.txt"))
2022-04-24 23:10:20 +08:00
{
2023-05-17 21:51:13 +08:00
replaces = File.ReadAllLines(App.RootPath + "Replace.txt");
2022-04-24 23:10:20 +08:00
}
2021-10-28 00:48:33 +08:00
//Fix emtpy lines
2022-04-24 23:10:20 +08:00
foreach (string str in fileNames)
2021-10-28 00:48:33 +08:00
{
2022-04-24 23:10:20 +08:00
string s = str;
//Make replacement
foreach (string replace in replaces)
{
if (s == Strings.Left(replace, replace.IndexOf("-->")))
{
s = Strings.Mid(replace, replace.IndexOf("-->") + 4);
}
}
2021-10-28 00:48:33 +08:00
if (s != "") Names.Add(s);
}
2021-10-30 23:55:38 +08:00
PeopleCount = Names.Count();
TextBlockPeopleCount.Text = PeopleCount.ToString();
2021-10-31 01:16:57 +08:00
if (PeopleCount == 0)
{
PeopleCount = 60;
TextBlockPeopleCount.Text = "点击此处以导入名单";
}
2021-10-28 00:48:33 +08:00
}
}
private void BorderBtnHelp_MouseUp(object sender, MouseButtonEventArgs e)
{
2021-10-30 23:55:38 +08:00
//MessageBox.Show("如需显示姓名,请在程序目录下新建 Names.txt并将姓名输入一行一个。");
new NamesInputWindow().ShowDialog();
Window_Loaded(this, null);
2021-10-28 00:48:33 +08:00
}
private void BtnClose_MouseUp(object sender, MouseButtonEventArgs e)
{
Close();
}
}
}