Files
community/Ink Canvas/Windows/RandWindow.xaml.cs
T

545 lines
21 KiB
C#
Raw Normal View History

2026-02-21 16:51:34 +08:00
using Ink_Canvas.Helpers;
using iNKORE.UI.WPF.Modern.Common.IconKeys;
2025-08-31 11:43:52 +08:00
using Microsoft.VisualBasic;
using System;
2025-05-25 09:29:48 +08:00
using System.Collections.Generic;
using System.IO;
using System.Linq;
2025-09-07 00:08:11 +08:00
using System.Runtime.InteropServices;
2025-05-25 09:29:48 +08:00
using System.Threading;
using System.Windows;
using System.Windows.Input;
2025-09-07 00:08:11 +08:00
using System.Windows.Interop;
2025-07-28 14:40:44 +08:00
using System.Windows.Media;
using System.Windows.Media.Imaging;
2025-09-07 00:08:11 +08:00
using System.Windows.Threading;
2025-06-10 14:19:01 +08:00
using MessageBox = iNKORE.UI.WPF.Modern.Controls.MessageBox;
2025-05-25 09:29:48 +08:00
2025-08-03 16:46:33 +08:00
namespace Ink_Canvas
{
2025-05-25 09:29:48 +08:00
/// <summary>
/// Interaction logic for RandWindow.xaml
/// </summary>
2025-08-03 16:46:33 +08:00
public partial class RandWindow : Window
{
public RandWindow(Settings settings)
{
2025-05-25 09:29:48 +08:00
InitializeComponent();
AnimationsHelper.ShowWithSlideFromBottomAndFade(this, 0.25);
2025-09-07 00:03:15 +08:00
BorderBtnHelp.Visibility = settings.RandSettings.DisplayRandWindowNamesInputBtn == false ? Visibility.Collapsed : Visibility.Visible;
2025-05-25 09:29:48 +08:00
RandMaxPeopleOneTime = settings.RandSettings.RandWindowOnceMaxStudents;
2025-08-03 16:46:33 +08:00
RandDoneAutoCloseWaitTime = (int)settings.RandSettings.RandWindowOnceCloseLatency * 1000;
2025-07-15 20:50:12 +08:00
// 加载背景
LoadBackground(settings);
2025-09-07 00:08:11 +08:00
2025-10-04 21:55:55 +08:00
// 应用主题
ApplyTheme(settings);
2025-09-07 00:08:11 +08:00
// 设置窗口为置顶
Topmost = true;
2025-09-07 13:30:46 +08:00
2025-09-07 00:08:11 +08:00
// 添加窗口关闭事件处理
Closed += RandWindow_Closed;
2025-09-07 13:30:46 +08:00
2025-09-07 00:08:11 +08:00
// 添加窗口显示事件处理,确保置顶
Loaded += RandWindow_Loaded;
2025-07-15 20:50:12 +08:00
}
2025-08-03 16:46:33 +08:00
2025-07-15 20:50:12 +08:00
private void LoadBackground(Settings settings)
{
try
{
int selectedIndex = settings.RandSettings.SelectedBackgroundIndex;
if (selectedIndex <= 0)
{
// 默认背景(无背景)
BackgroundImage.ImageSource = null;
2025-07-28 14:40:44 +08:00
MainBorder.Background = new SolidColorBrush(Color.FromRgb(240, 243, 249));
2025-07-15 20:50:12 +08:00
}
else if (selectedIndex <= settings.RandSettings.CustomPickNameBackgrounds.Count)
{
// 自定义背景
var customBackground = settings.RandSettings.CustomPickNameBackgrounds[selectedIndex - 1];
if (File.Exists(customBackground.FilePath))
{
2025-07-28 14:40:44 +08:00
var bitmap = new BitmapImage();
2025-07-15 20:50:12 +08:00
bitmap.BeginInit();
bitmap.UriSource = new Uri(customBackground.FilePath);
bitmap.EndInit();
BackgroundImage.ImageSource = bitmap;
}
}
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"加载点名背景出错: {ex.Message}", LogHelper.LogType.Error);
}
2025-05-25 09:29:48 +08:00
}
2025-10-04 21:55:55 +08:00
private void ApplyTheme(Settings settings)
{
try
{
2025-10-04 22:05:58 +08:00
if (settings.Appearance.Theme == 0) // 浅色主题
{
iNKORE.UI.WPF.Modern.ThemeManager.SetRequestedTheme(this, iNKORE.UI.WPF.Modern.ElementTheme.Light);
}
else if (settings.Appearance.Theme == 1) // 深色主题
{
iNKORE.UI.WPF.Modern.ThemeManager.SetRequestedTheme(this, iNKORE.UI.WPF.Modern.ElementTheme.Dark);
}
else // 跟随系统主题
{
bool isSystemLight = IsSystemThemeLight();
if (isSystemLight)
{
iNKORE.UI.WPF.Modern.ThemeManager.SetRequestedTheme(this, iNKORE.UI.WPF.Modern.ElementTheme.Light);
}
else
{
iNKORE.UI.WPF.Modern.ThemeManager.SetRequestedTheme(this, iNKORE.UI.WPF.Modern.ElementTheme.Dark);
}
}
2025-10-04 21:55:55 +08:00
// 根据主题设置窗口背景
if (settings.RandSettings.SelectedBackgroundIndex <= 0)
{
// 没有自定义背景时,使用主题背景色
2025-12-27 12:05:34 +08:00
if (Application.Current.FindResource("RandWindowBackground") is SolidColorBrush backgroundBrush)
2025-10-04 21:55:55 +08:00
{
MainBorder.Background = backgroundBrush;
}
}
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"应用点名窗口主题出错: {ex.Message}", LogHelper.LogType.Error);
}
}
2025-10-04 22:05:58 +08:00
private bool IsSystemThemeLight()
{
var light = false;
try
{
var registryKey = Microsoft.Win32.Registry.CurrentUser;
var themeKey =
registryKey.OpenSubKey("software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize");
var keyValue = 0;
if (themeKey != null) keyValue = (int)themeKey.GetValue("SystemUsesLightTheme");
if (keyValue == 1) light = true;
}
2026-02-21 16:51:34 +08:00
catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex); }
2025-10-04 22:05:58 +08:00
return light;
}
2025-08-03 16:46:33 +08:00
public RandWindow(Settings settings, bool IsAutoClose)
{
2025-05-25 09:29:48 +08:00
InitializeComponent();
isAutoClose = IsAutoClose;
PeopleControlPane.Opacity = 0.4;
PeopleControlPane.IsHitTestVisible = false;
2025-09-07 00:03:15 +08:00
BorderBtnHelp.Visibility = settings.RandSettings.DisplayRandWindowNamesInputBtn == false ? Visibility.Collapsed : Visibility.Visible;
2025-05-25 09:29:48 +08:00
RandMaxPeopleOneTime = settings.RandSettings.RandWindowOnceMaxStudents;
RandDoneAutoCloseWaitTime = (int)settings.RandSettings.RandWindowOnceCloseLatency * 1000;
2025-08-03 16:46:33 +08:00
2025-07-15 20:50:12 +08:00
// 加载背景
LoadBackground(settings);
2025-05-25 09:29:48 +08:00
2025-10-04 21:55:55 +08:00
// 应用主题
ApplyTheme(settings);
2025-09-07 00:08:11 +08:00
// 设置窗口为置顶
Topmost = true;
2025-09-07 13:30:46 +08:00
2025-09-07 00:08:11 +08:00
// 添加窗口关闭事件处理
Closed += RandWindow_Closed;
2025-09-07 13:30:46 +08:00
2025-09-07 00:08:11 +08:00
// 添加窗口显示事件处理,确保置顶
Loaded += RandWindow_Loaded;
2025-08-03 16:46:33 +08:00
new Thread(() =>
{
2025-05-25 09:29:48 +08:00
Thread.Sleep(100);
2025-08-03 16:46:33 +08:00
Application.Current.Dispatcher.Invoke(() =>
{
2025-05-25 09:29:48 +08:00
BorderBtnRand_MouseUp(BorderBtnRand, null);
});
2025-07-28 14:40:44 +08:00
}).Start();
2025-05-25 09:29:48 +08:00
}
public static int randSeed = 0;
2025-07-28 14:40:44 +08:00
public bool isAutoClose;
2025-05-25 09:29:48 +08:00
public bool isNotRepeatName = false;
public int TotalCount = 1;
public int PeopleCount = 60;
public List<string> Names = new List<string>();
2025-08-03 16:46:33 +08:00
private void BorderBtnAdd_MouseUp(object sender, MouseButtonEventArgs e)
{
2025-05-25 09:29:48 +08:00
if (RandMaxPeopleOneTime == -1 && TotalCount >= PeopleCount) return;
if (RandMaxPeopleOneTime != -1 && TotalCount >= RandMaxPeopleOneTime) return;
TotalCount++;
LabelNumberCount.Text = TotalCount.ToString();
FontIconStart.Icon = SegoeFluentIcons.People;
2025-05-25 09:29:48 +08:00
BorderBtnAdd.Opacity = 1;
BorderBtnMinus.Opacity = 1;
}
2025-08-03 16:46:33 +08:00
private void BorderBtnMinus_MouseUp(object sender, MouseButtonEventArgs e)
{
2025-05-25 09:29:48 +08:00
if (TotalCount < 2) return;
TotalCount--;
LabelNumberCount.Text = TotalCount.ToString();
2025-08-03 16:46:33 +08:00
if (TotalCount == 1)
{
FontIconStart.Icon = SegoeFluentIcons.Contact;
2025-05-25 09:29:48 +08:00
}
}
public int RandWaitingTimes = 100;
public int RandWaitingThreadSleepTime = 5;
public int RandMaxPeopleOneTime = 10;
public int RandDoneAutoCloseWaitTime = 2500;
2025-08-03 16:46:33 +08:00
private void BorderBtnRand_MouseUp(object sender, MouseButtonEventArgs e)
{
2025-05-25 09:29:48 +08:00
Random random = new Random();// randSeed + DateTime.Now.Millisecond / 10 % 10);
string outputString = "";
List<string> outputs = new List<string>();
LabelOutput2.Visibility = Visibility.Collapsed;
LabelOutput3.Visibility = Visibility.Collapsed;
2025-08-03 16:46:33 +08:00
new Thread(() =>
{
2025-12-20 19:57:11 +08:00
var animationPool = new List<int>();
for (int num = 1; num <= PeopleCount; num++)
{
animationPool.Add(num);
}
2025-08-03 16:46:33 +08:00
for (int i = 0; i < RandWaitingTimes; i++)
{
2025-12-20 19:57:11 +08:00
if (animationPool.Count == 0)
{
animationPool.Clear();
for (int num = 1; num <= PeopleCount; num++)
{
animationPool.Add(num);
}
}
int randomIndex = random.Next(0, animationPool.Count);
int selectedNumber = animationPool[randomIndex];
2025-12-27 12:05:34 +08:00
2025-12-20 19:57:11 +08:00
int lastIndex = animationPool.Count - 1;
if (randomIndex != lastIndex)
2025-08-03 16:46:33 +08:00
{
2025-12-20 19:57:11 +08:00
animationPool[randomIndex] = animationPool[lastIndex];
2025-05-25 09:29:48 +08:00
}
2025-12-20 19:57:11 +08:00
animationPool.RemoveAt(lastIndex);
2025-08-03 16:46:33 +08:00
Application.Current.Dispatcher.Invoke(() =>
{
if (Names.Count != 0)
{
2025-12-20 19:57:11 +08:00
LabelOutput.Content = Names[selectedNumber - 1];
2025-08-03 16:46:33 +08:00
}
else
{
2025-12-20 19:57:11 +08:00
LabelOutput.Content = selectedNumber.ToString();
2025-05-25 09:29:48 +08:00
}
});
Thread.Sleep(RandWaitingThreadSleepTime);
}
2025-08-03 16:46:33 +08:00
Application.Current.Dispatcher.Invoke(() =>
{
2025-12-20 19:57:11 +08:00
var candidatePool = new List<int>();
for (int num = 1; num <= PeopleCount; num++)
{
candidatePool.Add(num);
}
for (int i = 0; i < TotalCount && candidatePool.Count > 0; i++)
2025-08-03 16:46:33 +08:00
{
2025-12-20 19:57:11 +08:00
int randomIndex = random.Next(0, candidatePool.Count);
int selectedNumber = candidatePool[randomIndex];
2025-12-27 12:05:34 +08:00
2025-12-20 19:57:11 +08:00
int lastIndex = candidatePool.Count - 1;
if (randomIndex != lastIndex)
2025-08-03 16:46:33 +08:00
{
2025-12-20 19:57:11 +08:00
candidatePool[randomIndex] = candidatePool[lastIndex];
2025-05-25 09:29:48 +08:00
}
2025-12-20 19:57:11 +08:00
candidatePool.RemoveAt(lastIndex);
2025-05-25 09:29:48 +08:00
2025-08-03 16:46:33 +08:00
if (Names.Count != 0)
{
2025-12-20 19:57:11 +08:00
outputs.Add(Names[selectedNumber - 1]);
outputString += Names[selectedNumber - 1] + Environment.NewLine;
2025-08-03 16:46:33 +08:00
}
else
{
2025-12-20 19:57:11 +08:00
outputs.Add(selectedNumber.ToString());
outputString += selectedNumber + Environment.NewLine;
2025-05-25 09:29:48 +08:00
}
}
2025-08-03 16:46:33 +08:00
if (TotalCount <= 5)
{
2025-07-28 14:40:44 +08:00
LabelOutput.Content = outputString.Trim();
2025-08-03 16:46:33 +08:00
}
else if (TotalCount <= 10)
{
2025-05-25 09:29:48 +08:00
LabelOutput2.Visibility = Visibility.Visible;
outputString = "";
2025-08-03 16:46:33 +08:00
for (int i = 0; i < (outputs.Count + 1) / 2; i++)
{
2025-07-28 14:40:44 +08:00
outputString += outputs[i] + Environment.NewLine;
2025-05-25 09:29:48 +08:00
}
2025-07-28 14:40:44 +08:00
LabelOutput.Content = outputString.Trim();
2025-05-25 09:29:48 +08:00
outputString = "";
2025-08-03 16:46:33 +08:00
for (int i = (outputs.Count + 1) / 2; i < outputs.Count; i++)
{
2025-07-28 14:40:44 +08:00
outputString += outputs[i] + Environment.NewLine;
2025-05-25 09:29:48 +08:00
}
2025-07-28 14:40:44 +08:00
LabelOutput2.Content = outputString.Trim();
2025-08-03 16:46:33 +08:00
}
else
{
2025-05-25 09:29:48 +08:00
LabelOutput2.Visibility = Visibility.Visible;
LabelOutput3.Visibility = Visibility.Visible;
outputString = "";
2025-08-03 16:46:33 +08:00
for (int i = 0; i < (outputs.Count + 1) / 3; i++)
{
2025-07-28 14:40:44 +08:00
outputString += outputs[i] + Environment.NewLine;
2025-05-25 09:29:48 +08:00
}
2025-07-28 14:40:44 +08:00
LabelOutput.Content = outputString.Trim();
2025-05-25 09:29:48 +08:00
outputString = "";
2025-08-03 16:46:33 +08:00
for (int i = (outputs.Count + 1) / 3; i < (outputs.Count + 1) * 2 / 3; i++)
{
2025-07-28 14:40:44 +08:00
outputString += outputs[i] + Environment.NewLine;
2025-05-25 09:29:48 +08:00
}
2025-07-28 14:40:44 +08:00
LabelOutput2.Content = outputString.Trim();
2025-05-25 09:29:48 +08:00
outputString = "";
2025-08-03 16:46:33 +08:00
for (int i = (outputs.Count + 1) * 2 / 3; i < outputs.Count; i++)
{
2025-07-28 14:40:44 +08:00
outputString += outputs[i] + Environment.NewLine;
2025-05-25 09:29:48 +08:00
}
2025-07-28 14:40:44 +08:00
LabelOutput3.Content = outputString.Trim();
2025-05-25 09:29:48 +08:00
}
2025-08-03 16:46:33 +08:00
if (isAutoClose)
{
new Thread(() =>
{
2025-05-25 09:29:48 +08:00
Thread.Sleep(RandDoneAutoCloseWaitTime);
2025-08-03 16:46:33 +08:00
Application.Current.Dispatcher.Invoke(() =>
{
2025-05-25 09:29:48 +08:00
PeopleControlPane.Opacity = 1;
PeopleControlPane.IsHitTestVisible = true;
Close();
});
2025-07-28 14:40:44 +08:00
}).Start();
2025-05-25 09:29:48 +08:00
}
});
2025-07-28 14:40:44 +08:00
}).Start();
2025-05-25 09:29:48 +08:00
}
2025-08-03 16:46:33 +08:00
private void Window_Loaded(object sender, RoutedEventArgs e)
{
2025-05-25 09:29:48 +08:00
Names = new List<string>();
2025-08-03 16:46:33 +08:00
if (File.Exists(App.RootPath + "Names.txt"))
{
2025-05-25 09:29:48 +08:00
string[] fileNames = File.ReadAllLines(App.RootPath + "Names.txt");
string[] replaces = new string[0];
2025-08-03 16:46:33 +08:00
if (File.Exists(App.RootPath + "Replace.txt"))
{
2025-05-25 09:29:48 +08:00
replaces = File.ReadAllLines(App.RootPath + "Replace.txt");
}
//Fix emtpy lines
2025-08-03 16:46:33 +08:00
foreach (string str in fileNames)
{
2025-05-25 09:29:48 +08:00
string s = str;
//Make replacement
2025-08-03 16:46:33 +08:00
foreach (string replace in replaces)
{
if (s == Strings.Left(replace, replace.IndexOf("-->")))
{
2025-05-25 09:29:48 +08:00
s = Strings.Mid(replace, replace.IndexOf("-->") + 4);
}
}
if (s != "") Names.Add(s);
}
PeopleCount = Names.Count();
TextBlockPeopleCount.Text = PeopleCount.ToString();
2025-08-03 16:46:33 +08:00
if (PeopleCount == 0)
{
2025-05-25 09:29:48 +08:00
PeopleCount = 60;
TextBlockPeopleCount.Text = "点击此处以导入名单";
}
}
}
2026-03-21 16:30:55 +08:00
private async void BorderBtnHelp_MouseUp(object sender, MouseButtonEventArgs e)
2025-08-03 16:46:33 +08:00
{
2026-03-21 16:30:55 +08:00
if (SecurityManager.IsPasswordRequiredForModifyOrClearNameList(MainWindow.Settings))
{
bool ok = await SecurityManager.PromptAndVerifyAsync(
MainWindow.Settings,
this,
"名单修改验证",
"请输入安全密码以修改点名名单。");
if (!ok) return;
}
2026-04-17 13:14:23 +08:00
var namesInputWindow = new NamesInputWindow();
namesInputWindow.Owner = this;
namesInputWindow.ShowDialog();
2025-05-25 09:29:48 +08:00
Window_Loaded(this, null);
}
2025-08-03 16:46:33 +08:00
private void BtnClose_MouseUp(object sender, MouseButtonEventArgs e)
{
2025-05-25 09:29:48 +08:00
Close();
}
2025-06-10 14:19:01 +08:00
2025-06-10 17:21:26 +08:00
// 将 isIslandCallerFirstClick 设为静态字段,实现全局记录
private static bool isIslandCallerFirstClick = true;
2025-06-10 16:48:45 +08:00
2025-08-24 00:05:26 +08:00
private void BorderBtnExternalCaller_MouseUp(object sender, MouseButtonEventArgs e)
2025-06-10 14:19:01 +08:00
{
2025-06-10 16:48:45 +08:00
if (isIslandCallerFirstClick)
{
MessageBox.Show(
2025-08-24 00:05:26 +08:00
"首次使用外部点名功能,请确保已安装相应的点名软件。\n" +
"如未安装,请前往官网下载并安装后再使用。如果已安装请再次点击此按钮。",
2025-06-10 16:48:45 +08:00
"提示", MessageBoxButton.OK, MessageBoxImage.Information);
isIslandCallerFirstClick = false;
return;
}
2025-06-10 14:19:01 +08:00
try
{
2026-03-21 16:49:37 +08:00
string[] protocols;
2025-08-24 00:05:26 +08:00
switch (ComboBoxCallerType.SelectedIndex)
{
case 0: // ClassIsland点名
2026-03-21 16:49:37 +08:00
protocols = ExternalCallerLauncher.GetProtocolsByType(0);
2025-08-24 00:05:26 +08:00
break;
case 1: // SecRandom点名
2026-03-21 16:49:37 +08:00
protocols = ExternalCallerLauncher.GetProtocolsByType(1);
2025-08-24 00:05:26 +08:00
break;
case 2: // NamePicker点名
2026-03-21 16:49:37 +08:00
protocols = ExternalCallerLauncher.GetProtocolsByType(2);
2025-08-24 00:05:26 +08:00
break;
default:
2026-03-21 16:49:37 +08:00
protocols = ExternalCallerLauncher.GetProtocolsByType(0);
2025-08-24 00:05:26 +08:00
break;
}
2026-03-21 16:49:37 +08:00
if (!ExternalCallerLauncher.TryLaunch(protocols, out Exception lastException))
2025-06-10 14:19:01 +08:00
{
2026-03-21 16:49:37 +08:00
throw lastException ?? new InvalidOperationException("external caller protocols are unavailable");
}
2025-06-10 14:19:01 +08:00
}
catch (Exception ex)
{
MessageBox.Show("无法调用外部点名:" + ex.Message);
}
}
2025-09-07 00:08:11 +08:00
/// <summary>
/// 窗口加载事件处理
/// </summary>
private void RandWindow_Loaded(object sender, RoutedEventArgs e)
{
// 使用延迟确保窗口完全加载后再应用置顶
Dispatcher.BeginInvoke(new Action(() =>
{
try
{
// 强制激活窗口
Activate();
Focus();
2025-09-07 13:30:46 +08:00
2025-09-07 00:08:11 +08:00
// 设置置顶
Topmost = true;
2025-09-07 13:30:46 +08:00
2025-09-07 00:08:11 +08:00
// 使用Win32 API强制置顶
var hwnd = new WindowInteropHelper(this).Handle;
if (hwnd != IntPtr.Zero)
{
const int WS_EX_TOPMOST = 0x00000008;
const int GWL_EXSTYLE = -20;
const int SWP_NOMOVE = 0x0002;
const int SWP_NOSIZE = 0x0001;
const int SWP_SHOWWINDOW = 0x0040;
const int SWP_NOOWNERZORDER = 0x0200;
var HWND_TOPMOST = new IntPtr(-1);
// 设置窗口样式为置顶
int exStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
SetWindowLong(hwnd, GWL_EXSTYLE, exStyle | WS_EX_TOPMOST);
// 强制置顶
SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0,
SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW | SWP_NOOWNERZORDER);
}
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"RandWindow置顶失败: {ex.Message}", LogHelper.LogType.Error);
}
}), DispatcherPriority.Loaded);
}
/// <summary>
/// 窗口关闭事件处理
/// </summary>
private void RandWindow_Closed(object sender, EventArgs e)
{
// 窗口关闭时的清理工作
// 这里可以添加必要的清理代码
}
2025-10-05 09:16:36 +08:00
/// <summary>
/// 刷新主题,当主窗口主题切换时调用
/// </summary>
public void RefreshTheme()
{
try
{
// 重新应用主题
ApplyTheme(MainWindow.Settings);
2025-10-06 18:29:12 +08:00
2025-10-05 09:16:36 +08:00
// 强制刷新UI
InvalidateVisual();
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"刷新点名窗口主题出错: {ex.Message}", LogHelper.LogType.Error);
}
}
2025-09-07 00:08:11 +08:00
#region Win32 API
[DllImport("user32.dll")]
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
[DllImport("user32.dll")]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
#endregion
2025-05-25 09:29:48 +08:00
}
}