170 lines
5.7 KiB
Markdown
170 lines
5.7 KiB
Markdown
# YwxAppWpfDanMu
|
|
|
|
YwxAppWpfDanMu/
|
|
├── Controls/
|
|
│ ├── DanMuControl.xaml
|
|
│ ├── DanMuControl.xaml.cs
|
|
│ ├── DanMuItem.xaml
|
|
│ ├── DanMuItem.xaml.cs
|
|
│ ├── DanMuPool.cs
|
|
│ ├── DanMuSettings.cs
|
|
│ └── DanMuFilter.cs
|
|
├── Models/
|
|
│ ├── DanMuMessage.cs
|
|
│ └── DanMuEventArgs.cs
|
|
├── Services/
|
|
│ ├── DanMuDispatcher.cs
|
|
│ ├── DanMuPerformanceMonitor.cs
|
|
│ └── IDanMuService.cs
|
|
├── Utils/
|
|
│ ├── DanMuQueueProcessor.cs
|
|
│ └── RateLimiter.cs
|
|
└── Properties/
|
|
└── AssemblyInfo.cs
|
|
|
|
这个实现提供了完整的弹幕功能,包括多行显示、不同颜色、头像支持、防重叠、椭圆形边框、透明度渐变、点击事件、字体样式设置、暂停/继续功能、过滤功能等。还包含了批处理队列处理逻辑、速率限制、错误处理和性能监控等功能。
|
|
|
|
<Window x:Class="YourApp.MainWindow"
|
|
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
|
xmlns:danmu="clr-namespace:YwxAppWpfDanMu.Controls;assembly=YwxAppWpfDanMu"
|
|
Title="弹幕测试" Height="450" Width="800">
|
|
<Grid>
|
|
<Grid.RowDefinitions>
|
|
<RowDefinition Height="*"/>
|
|
<RowDefinition Height="Auto"/>
|
|
</Grid.RowDefinitions>
|
|
|
|
<danmu:DanMuControl x:Name="DanMuControl" Grid.Row="0" LineCount="8"/>
|
|
|
|
<StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Center">
|
|
<Button Content="暂停" Width="80" Margin="5" Click="PauseButton_Click"/>
|
|
<Button Content="继续" Width="80" Margin="5" Click="ResumeButton_Click"/>
|
|
<Button Content="清空" Width="80" Margin="5" Click="ClearButton_Click"/>
|
|
<Button Content="添加测试" Width="80" Margin="5" Click="AddTestButton_Click"/>
|
|
</StackPanel>
|
|
</Grid>
|
|
</Window>
|
|
|
|
|
|
// 在主窗口中使用弹幕控件
|
|
public partial class MainWindow : Window
|
|
{
|
|
private readonly Random _random = new Random();
|
|
|
|
public MainWindow()
|
|
{
|
|
InitializeComponent();
|
|
|
|
// 订阅事件
|
|
DanMuControl.DanMuClick += OnDanMuClick;
|
|
DanMuControl.DanMuAdded += OnDanMuAdded;
|
|
DanMuControl.DanMuRemoved += OnDanMuRemoved;
|
|
|
|
// 添加测试弹幕
|
|
AddTestDanMu();
|
|
}
|
|
|
|
private void AddTestDanMu()
|
|
{
|
|
var colors = new[] { Colors.Red, Colors.Green, Colors.Blue, Colors.Yellow, Colors.White };
|
|
|
|
for (int i = 0; i < 100; i++)
|
|
{
|
|
var message = new DanMuMessage
|
|
{
|
|
Content = $"这是测试弹幕 {i + 1}",
|
|
Color = colors[_random.Next(colors.Length)],
|
|
FontSize = 12 + _random.Next(10),
|
|
Opacity = 0.7 + _random.NextDouble() * 0.3
|
|
};
|
|
|
|
DanMuControl.AddDanMu(message);
|
|
}
|
|
}
|
|
|
|
private void OnDanMuClick(object sender, DanMuEventArgs e)
|
|
{
|
|
MessageBox.Show($"点击了弹幕: {e.Message.Content}");
|
|
}
|
|
|
|
private void OnDanMuAdded(object sender, DanMuEventArgs e)
|
|
{
|
|
// 可以在这里处理弹幕添加事件
|
|
}
|
|
|
|
private void OnDanMuRemoved(object sender, DanMuEventArgs e)
|
|
{
|
|
// 可以在这里处理弹幕移除事件
|
|
}
|
|
|
|
private void PauseButton_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
DanMuControl.IsPaused = true;
|
|
}
|
|
|
|
private void ResumeButton_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
DanMuControl.IsPaused = false;
|
|
}
|
|
|
|
private void ClearButton_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
DanMuControl.ClearAll();
|
|
}
|
|
|
|
private void AddTestButton_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
// 方法1: 添加单条弹幕
|
|
AddSingleTestDanMu();
|
|
|
|
// 方法2: 添加多条弹幕(批量)
|
|
// AddMultipleTestDanMu(5);
|
|
}
|
|
|
|
// 添加单条测试弹幕
|
|
private void AddSingleTestDanMu()
|
|
{
|
|
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 DanMuMessage
|
|
{
|
|
Content = $"这是单条测试弹幕 {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" // 使用占位图作为头像
|
|
};
|
|
|
|
DanMuControl.AddDanMu(message);
|
|
}
|
|
|
|
// 添加多条测试弹幕
|
|
private void AddMultipleTestDanMu(int count)
|
|
{
|
|
var messages = new List<DanMuMessage>();
|
|
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 DanMuMessage
|
|
{
|
|
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}" // 带编号的头像
|
|
});
|
|
}
|
|
|
|
DanMuControl.AddDanMuBatch(messages);
|
|
}
|
|
} |