add:自定义点名背景

This commit is contained in:
2025-07-15 20:50:12 +08:00
parent 4f7c1021c8
commit 1ff40c0016
10 changed files with 474 additions and 2 deletions
+24 -1
View File
@@ -2415,7 +2415,7 @@
<TextBlock Margin="0,12,0,0" Text="随机点名" FontWeight="Bold" Foreground="#fafafa"
FontSize="26" />
</GroupBox.Header>
<ui:SimpleStackPanel Spacing="6">
<ui:SimpleStackPanel Spacing="12">
<ui:SimpleStackPanel Orientation="Horizontal" HorizontalAlignment="Left">
<TextBlock Foreground="#fafafa" Text="显示修改随机点名名单的按钮"
VerticalAlignment="Center" FontSize="14" Margin="0,0,16,0" />
@@ -2425,6 +2425,29 @@
FontWeight="Bold"
Toggled="ToggleSwitchDisplayRandWindowNamesInputBtn_OnToggled" />
</ui:SimpleStackPanel>
<TextBlock Foreground="#fafafa" Text="点名窗口背景设置"
FontSize="16" FontWeight="Bold" Margin="0,10,0,5" />
<ui:SimpleStackPanel Orientation="Horizontal" HorizontalAlignment="Left" Margin="0,5,0,0">
<TextBlock Foreground="#fafafa" Text="背景选择:" VerticalAlignment="Center"
FontSize="14" Margin="0,0,16,0" />
<ComboBox Name="ComboBoxPickNameBackground" FontFamily="Microsoft YaHei UI"
SelectedIndex="0" Width="180"
SelectionChanged="ComboBoxPickNameBackground_SelectionChanged">
<ComboBoxItem Content="默认背景" FontFamily="Microsoft YaHei UI" />
<!-- 自定义背景会在代码中动态添加 -->
</ComboBox>
</ui:SimpleStackPanel>
<ui:SimpleStackPanel Orientation="Horizontal" HorizontalAlignment="Left" Margin="0,5,0,0">
<TextBlock Foreground="#fafafa" Text="自定义背景:" VerticalAlignment="Center"
FontSize="14" Margin="0,0,16,0" />
<Button Name="ButtonAddCustomBackground" Content="上传" FontFamily="Microsoft YaHei UI"
Click="ButtonAddCustomBackground_Click" Padding="10,3"/>
<Button Name="ButtonManageBackgrounds" Content="管理" FontFamily="Microsoft YaHei UI"
Click="ButtonManageBackgrounds_Click" Padding="10,3" Margin="5,0,0,0"/>
</ui:SimpleStackPanel>
<ui:SimpleStackPanel Orientation="Horizontal" HorizontalAlignment="Left">
<TextBlock Foreground="#fafafa" Text="启用随机抽和单次抽按钮"
VerticalAlignment="Center" FontSize="14" Margin="0,0,16,0" />
+60
View File
@@ -1934,5 +1934,65 @@ namespace Ink_Canvas {
}
}
}
// 自定义点名背景相关方法
public void UpdatePickNameBackgroundsInComboBox()
{
// 清除现有的自定义背景选项
if (ComboBoxPickNameBackground != null)
{
// 保留第一个默认选项
while (ComboBoxPickNameBackground.Items.Count > 1)
{
ComboBoxPickNameBackground.Items.RemoveAt(ComboBoxPickNameBackground.Items.Count - 1);
}
// 添加自定义背景选项
foreach (var background in Settings.RandSettings.CustomPickNameBackgrounds)
{
ComboBoxItem item = new ComboBoxItem();
item.Content = background.Name;
item.FontFamily = new System.Windows.Media.FontFamily("Microsoft YaHei UI");
ComboBoxPickNameBackground.Items.Add(item);
}
}
}
public void UpdatePickNameBackgroundDisplay()
{
// 此方法主要用于在外部窗口更改背景后更新UI
if (ComboBoxPickNameBackground != null)
{
ComboBoxPickNameBackground.SelectedIndex = Settings.RandSettings.SelectedBackgroundIndex;
}
}
private void ComboBoxPickNameBackground_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (!isLoaded) return;
Settings.RandSettings.SelectedBackgroundIndex = ComboBoxPickNameBackground.SelectedIndex;
SaveSettingsToFile();
}
private void ButtonAddCustomBackground_Click(object sender, RoutedEventArgs e)
{
AddPickNameBackgroundWindow dialog = new AddPickNameBackgroundWindow(this);
dialog.Owner = this;
dialog.ShowDialog();
if (dialog.IsSuccess)
{
// 自动选中新添加的背景
ComboBoxPickNameBackground.SelectedIndex = ComboBoxPickNameBackground.Items.Count - 1;
}
}
private void ButtonManageBackgrounds_Click(object sender, RoutedEventArgs e)
{
ManagePickNameBackgroundsWindow dialog = new ManagePickNameBackgroundsWindow(this);
dialog.Owner = this;
dialog.ShowDialog();
}
}
}
@@ -602,6 +602,16 @@ namespace Ink_Canvas {
ToggleSwitchDirectCallCiRand.IsOn = Settings.RandSettings.DirectCallCiRand;
RandomDrawPanel.Visibility = Settings.RandSettings.ShowRandomAndSingleDraw ? Visibility.Visible : Visibility.Collapsed;
SingleDrawPanel.Visibility = Settings.RandSettings.ShowRandomAndSingleDraw ? Visibility.Visible : Visibility.Collapsed;
// 加载自定义点名背景
UpdatePickNameBackgroundsInComboBox();
// 设置选择的背景索引
if (Settings.RandSettings.SelectedBackgroundIndex >= ComboBoxPickNameBackground.Items.Count)
{
Settings.RandSettings.SelectedBackgroundIndex = 0;
}
ComboBoxPickNameBackground.SelectedIndex = Settings.RandSettings.SelectedBackgroundIndex;
} else {
Settings.RandSettings = new RandSettings();
ToggleSwitchDisplayRandWindowNamesInputBtn.IsOn = Settings.RandSettings.DisplayRandWindowNamesInputBtn;
+22
View File
@@ -440,6 +440,28 @@ namespace Ink_Canvas
public bool ShowRandomAndSingleDraw { get; set; } = true;
[JsonProperty("directCallCiRand")]
public bool DirectCallCiRand { get; set; } = false;
[JsonProperty("selectedBackgroundIndex")]
public int SelectedBackgroundIndex { get; set; } = 0;
[JsonProperty("customPickNameBackgrounds")]
public List<CustomPickNameBackground> CustomPickNameBackgrounds { get; set; } = new List<CustomPickNameBackground>();
}
public class CustomPickNameBackground
{
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("filePath")]
public string FilePath { get; set; }
public CustomPickNameBackground(string name, string filePath)
{
Name = name;
FilePath = filePath;
}
// 用于JSON序列化
public CustomPickNameBackground() { }
}
public class CustomFloatingBarIcon
@@ -0,0 +1,48 @@
<Window x:Class="Ink_Canvas.AddPickNameBackgroundWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ui="http://schemas.inkore.net/lib/ui/wpf/modern"
xmlns:local="clr-namespace:Ink_Canvas"
mc:Ignorable="d"
Title="添加自定义点名背景" Height="550" Width="800" WindowStartupLocation="CenterScreen" ResizeMode="NoResize">
<Grid Margin="20">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Text="添加自定义点名背景" FontSize="20" FontWeight="Bold" Margin="0,0,0,20" Grid.Row="0"/>
<StackPanel Grid.Row="1" Orientation="Horizontal" Margin="0,0,0,20">
<TextBlock Text="选择背景图片:" VerticalAlignment="Center" FontSize="14"/>
<TextBox Name="BackgroundPathTextBox" Width="400" IsReadOnly="True" Margin="10,0" Height="25"/>
<Button Content="浏览..." Click="BrowseButton_Click" Width="100" Height="45"/>
</StackPanel>
<StackPanel Grid.Row="2" Orientation="Horizontal" Margin="0,0,0,20">
<TextBlock Text="背景名称:" VerticalAlignment="Center" FontSize="14"/>
<TextBox Name="BackgroundNameTextBox" Width="400" Margin="28,0,0,0" Height="25"/>
</StackPanel>
<TextBlock Grid.Row="3" Text="预览:" Margin="0,0,0,10" FontSize="14"/>
<Border Grid.Row="4" BorderBrush="#CCCCCC" BorderThickness="1" Padding="8">
<Grid>
<Image Name="BackgroundPreviewImage" Stretch="Uniform" MaxHeight="250"/>
<TextBlock Text="未选择图片" HorizontalAlignment="Center" VerticalAlignment="Center"
Foreground="Gray" FontSize="16" Name="NoImageText"/>
</Grid>
</Border>
<StackPanel Grid.Row="5" Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,25,0,0">
<Button Content="取消" Width="120" Height="40" Click="CancelButton_Click" Margin="0,0,15,0"/>
<Button Name="SaveButton" Content="保存" Width="120" Height="40" Click="SaveButton_Click" IsEnabled="False"/>
</StackPanel>
</Grid>
</Window>
@@ -0,0 +1,121 @@
using System;
using System.IO;
using System.Windows;
using System.Windows.Media.Imaging;
using Microsoft.Win32;
namespace Ink_Canvas
{
/// <summary>
/// AddPickNameBackgroundWindow.xaml 的交互逻辑
/// </summary>
public partial class AddPickNameBackgroundWindow : Window
{
private MainWindow mainWindow;
private string selectedFilePath;
public bool IsSuccess { get; private set; }
public AddPickNameBackgroundWindow(MainWindow owner)
{
InitializeComponent();
mainWindow = owner;
IsSuccess = false;
// 添加TextBox内容变化事件以检查是否可以保存
BackgroundNameTextBox.TextChanged += (s, e) => ValidateSaveButton();
}
private void BrowseButton_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog
{
Filter = "图像文件|*.png;*.jpg;*.jpeg;*.bmp;*.gif",
Title = "选择一个背景图片"
};
if (openFileDialog.ShowDialog() == true)
{
selectedFilePath = openFileDialog.FileName;
BackgroundPathTextBox.Text = selectedFilePath;
// 显示预览
try
{
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = new Uri(selectedFilePath);
bitmap.EndInit();
BackgroundPreviewImage.Source = bitmap;
NoImageText.Visibility = Visibility.Collapsed;
}
catch (Exception ex)
{
MessageBox.Show($"无法加载图像: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
}
// 自动填充名称建议(文件名,不包括扩展名)
string suggestedName = Path.GetFileNameWithoutExtension(selectedFilePath);
BackgroundNameTextBox.Text = suggestedName;
ValidateSaveButton();
}
}
private void ValidateSaveButton()
{
SaveButton.IsEnabled = !string.IsNullOrWhiteSpace(BackgroundNameTextBox.Text) && !string.IsNullOrEmpty(selectedFilePath);
}
private void CancelButton_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
private void SaveButton_Click(object sender, RoutedEventArgs e)
{
try
{
// 创建pictures/picknamebackgrounds文件夹结构(如果不存在)
string picturesFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "pictures");
string backgroundsFolder = Path.Combine(picturesFolder, "picknamebackgrounds");
if (!Directory.Exists(picturesFolder))
{
Directory.CreateDirectory(picturesFolder);
}
if (!Directory.Exists(backgroundsFolder))
{
Directory.CreateDirectory(backgroundsFolder);
}
// 生成一个唯一的文件名(使用GUID)
string extension = Path.GetExtension(selectedFilePath);
string newFileName = $"{Guid.NewGuid()}{extension}";
string destPath = Path.Combine(backgroundsFolder, newFileName);
// 复制文件到pictures/picknamebackgrounds文件夹
File.Copy(selectedFilePath, destPath);
// 创建新的自定义背景对象
var customBackground = new CustomPickNameBackground(BackgroundNameTextBox.Text, destPath);
// 添加到主窗口的设置中
MainWindow.Settings.RandSettings.CustomPickNameBackgrounds.Add(customBackground);
// 更新ComboBox
mainWindow.UpdatePickNameBackgroundsInComboBox();
// 保存设置
MainWindow.SaveSettingsToFile();
IsSuccess = true;
this.Close();
}
catch (Exception ex)
{
MessageBox.Show($"保存背景时出错: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
}
@@ -0,0 +1,50 @@
<Window x:Class="Ink_Canvas.ManagePickNameBackgroundsWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ui="http://schemas.inkore.net/lib/ui/wpf/modern"
xmlns:local="clr-namespace:Ink_Canvas"
mc:Ignorable="d"
Title="管理点名背景" Height="500" Width="750" WindowStartupLocation="CenterScreen">
<Grid Margin="20">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Text="管理自定义点名背景" FontSize="20" FontWeight="Bold" Margin="0,0,0,15"/>
<ListView Grid.Row="1" Name="BackgroundsListView" BorderBrush="#CCCCCC" BorderThickness="1">
<ListView.View>
<GridView>
<GridViewColumn Header="预览" Width="150">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Border BorderThickness="1" BorderBrush="#CCCCCC">
<Image Source="{Binding FilePath}" Width="140" Height="80" Stretch="Uniform"/>
</Border>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="名称" Width="300" DisplayMemberBinding="{Binding Name}"/>
<GridViewColumn Header="操作" Width="200">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Button Content="设为当前" Width="90" Margin="0,0,5,0" Click="SetAsCurrentButton_Click" Tag="{Binding}"/>
<Button Content="删除" Width="70" Click="DeleteBackgroundButton_Click" Tag="{Binding}"/>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
<StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,15,0,0">
<Button Content="关闭" Width="120" Height="40" Click="CloseButton_Click"/>
</StackPanel>
</Grid>
</Window>
@@ -0,0 +1,98 @@
using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Windows;
using System.Windows.Controls;
namespace Ink_Canvas
{
/// <summary>
/// ManagePickNameBackgroundsWindow.xaml 的交互逻辑
/// </summary>
public partial class ManagePickNameBackgroundsWindow : Window
{
private MainWindow mainWindow;
public ObservableCollection<CustomPickNameBackground> Backgrounds { get; set; }
public ManagePickNameBackgroundsWindow(MainWindow owner)
{
InitializeComponent();
mainWindow = owner;
// 从主窗口的设置获取自定义背景列表
Backgrounds = new ObservableCollection<CustomPickNameBackground>(MainWindow.Settings.RandSettings.CustomPickNameBackgrounds);
BackgroundsListView.ItemsSource = Backgrounds;
}
private void SetAsCurrentButton_Click(object sender, RoutedEventArgs e)
{
if (sender is Button button && button.Tag is CustomPickNameBackground background)
{
// 找到背景在列表中的索引(加8,因为前8个是默认值)
int index = Backgrounds.IndexOf(background) + 1; // 增加1因为索引0将是"默认"
// 更新设置
MainWindow.Settings.RandSettings.SelectedBackgroundIndex = index;
// 更新UI
mainWindow.UpdatePickNameBackgroundDisplay();
// 保存设置
MainWindow.SaveSettingsToFile();
MessageBox.Show($"已将\"{background.Name}\"设置为当前点名背景", "设置成功", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
private void DeleteBackgroundButton_Click(object sender, RoutedEventArgs e)
{
if (sender is Button button && button.Tag is CustomPickNameBackground background)
{
if (MessageBox.Show($"确定要删除背景\"{background.Name}\"吗?", "确认删除", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
{
try
{
// 尝试删除文件
if (File.Exists(background.FilePath))
{
File.Delete(background.FilePath);
}
// 从列表中移除背景
Backgrounds.Remove(background);
// 更新主窗口的设置
MainWindow.Settings.RandSettings.CustomPickNameBackgrounds.Clear();
foreach (var bg in Backgrounds)
{
MainWindow.Settings.RandSettings.CustomPickNameBackgrounds.Add(bg);
}
// 如果当前选中的是被删除的背景,重置为默认背景
int selectedIndex = MainWindow.Settings.RandSettings.SelectedBackgroundIndex;
if (selectedIndex > 0 && selectedIndex - 1 >= MainWindow.Settings.RandSettings.CustomPickNameBackgrounds.Count)
{
MainWindow.Settings.RandSettings.SelectedBackgroundIndex = 0;
mainWindow.UpdatePickNameBackgroundDisplay();
}
// 更新ComboBox
mainWindow.UpdatePickNameBackgroundsInComboBox();
// 保存设置
MainWindow.SaveSettingsToFile();
}
catch (Exception ex)
{
MessageBox.Show($"删除背景时出错: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
}
private void CloseButton_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
}
+4 -1
View File
@@ -9,7 +9,10 @@
mc:Ignorable="d" WindowStyle="None" AllowsTransparency="True" Loaded="Window_Loaded"
WindowStartupLocation="CenterScreen"
Title="Ink Canvas 抽奖" Height="500" Width="900">
<Border Background="#F0F3F9" CornerRadius="10" BorderThickness="1" BorderBrush="#0066BF" Margin="0" ClipToBounds="True">
<Border x:Name="MainBorder" CornerRadius="10" BorderThickness="1" BorderBrush="#0066BF" Margin="0" ClipToBounds="True">
<Border.Background>
<ImageBrush x:Name="BackgroundImage" Stretch="UniformToFill" Opacity="1.0"/>
</Border.Background>
<Canvas>
<Grid Canvas.Left="0" Canvas.Right="0" Canvas.Top="0" Canvas.Bottom="0" Width="900" Height="309" HorizontalAlignment="Center" VerticalAlignment="Top">
<Grid.ColumnDefinitions>
+37
View File
@@ -21,6 +21,40 @@ namespace Ink_Canvas {
BorderBtnHelp.Visibility = settings.RandSettings.DisplayRandWindowNamesInputBtn == false ? Visibility.Collapsed : Visibility.Visible;
RandMaxPeopleOneTime = settings.RandSettings.RandWindowOnceMaxStudents;
RandDoneAutoCloseWaitTime = (int)settings.RandSettings.RandWindowOnceCloseLatency*1000;
// 加载背景
LoadBackground(settings);
}
private void LoadBackground(Settings settings)
{
try
{
int selectedIndex = settings.RandSettings.SelectedBackgroundIndex;
if (selectedIndex <= 0)
{
// 默认背景(无背景)
BackgroundImage.ImageSource = null;
MainBorder.Background = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromRgb(240, 243, 249));
}
else if (selectedIndex <= settings.RandSettings.CustomPickNameBackgrounds.Count)
{
// 自定义背景
var customBackground = settings.RandSettings.CustomPickNameBackgrounds[selectedIndex - 1];
if (File.Exists(customBackground.FilePath))
{
var bitmap = new System.Windows.Media.Imaging.BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = new Uri(customBackground.FilePath);
bitmap.EndInit();
BackgroundImage.ImageSource = bitmap;
}
}
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"加载点名背景出错: {ex.Message}", LogHelper.LogType.Error);
}
}
public RandWindow(Settings settings, bool IsAutoClose) {
@@ -31,6 +65,9 @@ namespace Ink_Canvas {
BorderBtnHelp.Visibility = settings.RandSettings.DisplayRandWindowNamesInputBtn == false ? Visibility.Collapsed : Visibility.Visible;
RandMaxPeopleOneTime = settings.RandSettings.RandWindowOnceMaxStudents;
RandDoneAutoCloseWaitTime = (int)settings.RandSettings.RandWindowOnceCloseLatency * 1000;
// 加载背景
LoadBackground(settings);
new Thread(new ThreadStart(() => {
Thread.Sleep(100);