115 lines
3.8 KiB
C#
115 lines
3.8 KiB
C#
using System.Windows;
|
|
using System.Windows.Media;
|
|
using System.Windows.Media.Imaging;
|
|
using YwxAppWpfBarrage;
|
|
using YwxAppWpfBarrage.Controls;
|
|
using YwxAppWpfBarrage.Models;
|
|
|
|
namespace WpfApp1;
|
|
|
|
/// <summary>
|
|
/// Interaction logic for MainWindow.xaml
|
|
/// </summary>
|
|
public partial class MainWindow : Window
|
|
{
|
|
private readonly Random _random = new Random();
|
|
|
|
public MainWindow()
|
|
{
|
|
InitializeComponent();
|
|
|
|
// 订阅事件
|
|
BarrageControl.BarrageClick += OnBarrageClick;
|
|
BarrageControl.BarrageAdded += OnBarrageAdded;
|
|
BarrageControl.BarrageRemoved += OnBarrageRemoved;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnBarrageClick(object sender, BarrageEventArgs e)
|
|
{
|
|
MessageBox.Show($"点击了弹幕: {e.Message.Content}");
|
|
}
|
|
|
|
private void OnBarrageAdded(object sender, BarrageEventArgs e)
|
|
{
|
|
// 可以在这里处理弹幕添加事件
|
|
}
|
|
|
|
private void OnBarrageRemoved(object sender, BarrageEventArgs e)
|
|
{
|
|
// 可以在这里处理弹幕移除事件
|
|
}
|
|
|
|
private void PauseButton_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
BarrageControl.IsPaused = true;
|
|
}
|
|
|
|
private void ResumeButton_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
BarrageControl.IsPaused = false;
|
|
}
|
|
|
|
private void ClearButton_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
BarrageControl.ClearAll();
|
|
}
|
|
|
|
private void Button_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
|
|
// 方法1: 添加单条弹幕
|
|
AddSingleTestBarrage();
|
|
|
|
// 方法2: 添加多条弹幕(批量)
|
|
// AddMultipleTestBarrage(5);
|
|
}
|
|
|
|
|
|
// 添加单条测试弹幕
|
|
private void AddSingleTestBarrage()
|
|
{
|
|
var colors = new[] { Colors.Red, Colors.Green, Colors.Blue, Colors.Yellow, Colors.White, Colors.Cyan, Colors.Magenta };
|
|
var fonts = new[] { "Microsoft YaHei", "SimSun", "Arial", "Segoe UI" };
|
|
var message = new BarrageMessage
|
|
{
|
|
// Content = $"这是单条测试弹幕 {DateTime.Now:HH:mm:ss}",
|
|
Content = $"这是单条测试弹幕 ",
|
|
Color = colors[_random.Next(colors.Length)],
|
|
FontSize = 12 + _random.Next(10),
|
|
FontFamily = new FontFamily(fonts[_random.Next(fonts.Length)]),
|
|
FontWeight = _random.Next(2) == 0 ? FontWeights.Normal : FontWeights.Bold,
|
|
FontStyle = _random.Next(2) == 0 ? FontStyles.Normal : FontStyles.Italic,
|
|
// Opacity = 0.7 + _random.NextDouble() * 0.3,
|
|
AvatarUrl = "https://unpkg.com/outeres@0.0.10/demo/avatar/1.jpg" // 使用占位图作为头像
|
|
};
|
|
BarrageControl.AddBarrage(message);
|
|
}
|
|
|
|
// 添加多条测试弹幕
|
|
private void AddMultipleTestBarrage(int count)
|
|
{
|
|
var messages = new List<BarrageMessage>();
|
|
var colors = new[] { Colors.Red, Colors.Green, Colors.Blue, Colors.Yellow, Colors.White, Colors.Cyan, Colors.Magenta };
|
|
var fonts = new[] { "Microsoft YaHei", "SimSun", "Arial", "Segoe UI" };
|
|
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
messages.Add(new BarrageMessage
|
|
{
|
|
Content = $"批量测试弹幕 {i + 1} - {DateTime.Now:HH:mm:ss}",
|
|
Color = colors[_random.Next(colors.Length)],
|
|
FontSize = 12 + _random.Next(10),
|
|
FontFamily = new FontFamily(fonts[_random.Next(fonts.Length)]),
|
|
FontWeight = _random.Next(2) == 0 ? FontWeights.Normal : FontWeights.Bold,
|
|
FontStyle = _random.Next(2) == 0 ? FontStyles.Normal : FontStyles.Italic,
|
|
Opacity = 0.7 + _random.NextDouble() * 0.3,
|
|
AvatarUrl = $"https://via.placeholder.com/30?text={i + 1}" // 带编号的头像
|
|
});
|
|
}
|
|
|
|
BarrageControl.AddBarrageBatch(messages);
|
|
}
|
|
} |