/
MainWindow.xaml.cs
398 lines (365 loc) · 15.4 KB
/
MainWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using CookComputing.XmlRpc;
namespace DanmukuRPCServer
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
private const int WS_EX_TRANSPARENT = 32;
private const int GWL_EXSTYLE = -20;
public MainOverlay overlay;
public FullOverlay fulloverlay;
private DispatcherTimer timer;
private DispatcherTimer renderTimer;
private Danmu danmu;
private bool showdanmu = true;
private Queue<string> infos = new Queue<string>(50);
private static XmlRpcListenerService _svc = new DanmukuService();
private System.Windows.Forms.NotifyIcon notifyIcon;
[DllImport("user32")]
private static extern uint SetWindowLong(IntPtr hwnd, int nIndex, uint dwNewLong);
[DllImport("user32")]
private static extern uint GetWindowLong(IntPtr hwnd, int nIndex);
public MainWindow()
{
InitializeComponent();
}
static void ProcessRequest(IAsyncResult result)
{
HttpListener listener = result.AsyncState as HttpListener;
// 结束异步操作
HttpListenerContext context = listener.EndGetContext(result);
// 重新启动异步请求处理
listener.BeginGetContext(ProcessRequest, listener);
try
{
// 处理请求
_svc.ProcessRequest(context);
}
catch (Exception)
{
}
}
private void FuckMicrosoft(object sender, EventArgs eventArgs)
{
if (this.fulloverlay != null)
{
this.fulloverlay.Topmost = false;
this.fulloverlay.Topmost = true;
}
if (this.overlay != null)
{
this.overlay.Topmost = false;
this.overlay.Topmost = true;
}
if (DanmukuService.close)
{
App.Current.Shutdown();
}
if (DanmukuService.danmu ^ showdanmu)
{
showdanmu = !showdanmu;
infos.Enqueue("全屏弹幕:" + showdanmu + "\n");
}
}
private void AddDanmu(object sender, EventArgs eventArgs)
{
if (DanmukuService.MsgQueue.TryTake(out danmu))
{
this.AddDMText(danmu.user, danmu.text, danmu.warn.Equals("true", StringComparison.OrdinalIgnoreCase));
}
}
private void OpenOverlay()
{
this.overlay = new MainOverlay();
this.overlay.Deactivated += new EventHandler(this.overlay_Deactivated);
this.overlay.SourceInitialized += delegate (object param0, EventArgs param1)
{
IntPtr handle = new WindowInteropHelper(this.overlay).Handle;
uint windowLong = MainWindow.GetWindowLong(handle, -20);
MainWindow.SetWindowLong(handle, -20, windowLong | 32u);
};
this.overlay.Background = Brushes.Transparent;
this.overlay.ShowInTaskbar = false;
this.overlay.Topmost = true;
this.overlay.Top = SystemParameters.WorkArea.Top + Store.MainOverlayXoffset;
this.overlay.Left = SystemParameters.WorkArea.Right - Store.MainOverlayWidth + Store.MainOverlayYoffset;
this.overlay.Height = SystemParameters.WorkArea.Height;
this.overlay.Width = Store.MainOverlayWidth;
}
private void overlay_Deactivated(object sender, EventArgs e)
{
if (sender is MainOverlay)
{
(sender as MainOverlay).Topmost = true;
}
}
private void OpenFullOverlay()
{
this.fulloverlay = new FullOverlay();
this.fulloverlay.Deactivated += new EventHandler(this.fulloverlay_Deactivated);
this.fulloverlay.Background = Brushes.Transparent;
this.fulloverlay.SourceInitialized += delegate (object param0, EventArgs param1)
{
IntPtr handle = new WindowInteropHelper(this.fulloverlay).Handle;
uint windowLong = MainWindow.GetWindowLong(handle, -20);
MainWindow.SetWindowLong(handle, -20, windowLong | 32u);
};
this.fulloverlay.ShowInTaskbar = false;
this.fulloverlay.Topmost = true;
this.fulloverlay.Top = SystemParameters.WorkArea.Top;
this.fulloverlay.Left = SystemParameters.WorkArea.Left;
this.fulloverlay.Width = SystemParameters.WorkArea.Width;
this.fulloverlay.Height = SystemParameters.WorkArea.Height;
}
private void fulloverlay_Deactivated(object sender, EventArgs e)
{
if (sender is FullOverlay)
{
(sender as FullOverlay).Topmost = true;
}
}
private void s_Completed(object sender, EventArgs e)
{
ClockGroup clockGroup = sender as ClockGroup;
if (clockGroup != null)
{
FullScreenDanmaku fullScreenDanmaku = Storyboard.GetTarget(clockGroup.Children[0].Timeline) as FullScreenDanmaku;
if (fullScreenDanmaku != null)
{
this.fulloverlay.LayoutRoot.Children.Remove(fullScreenDanmaku);
}
}
}
private void sb_Completed(object sender, EventArgs e)
{
ClockGroup clockGroup = sender as ClockGroup;
if (clockGroup != null)
{
DanmakuTextControl danmakuTextControl = Storyboard.GetTarget(clockGroup.Children[2].Timeline) as DanmakuTextControl;
if (danmakuTextControl != null)
{
this.overlay.LayoutRoot.Children.Remove(danmakuTextControl);
}
}
}
public void AddDMText(string user, string text, bool warn)
{
if (user == null || text == null)
return;
if (Dispatcher.CheckAccess())
{
while (infos.Count >= 50)
{
infos.Dequeue();
}
if (warn)
{
infos.Enqueue($"! {text}\n");
}
else
{
if (string.IsNullOrEmpty(user))
{
infos.Enqueue($"{text}\n");
}
else
{
infos.Enqueue($"{user}: {text}\n");
}
}
this.textBox.Text = string.Join("\n", infos);
this.textBox.Focus();
this.textBox.CaretIndex = this.textBox.Text.Length;
this.textBox.SelectionStart = this.textBox.Text.Length;
this.textBox.SelectionLength = 0;
this.textBox.ScrollToEnd();
if (true)
{
DanmakuTextControl danmakuTextControl = new DanmakuTextControl();
danmakuTextControl.UserName.Text = user;
if (warn)
{
danmakuTextControl.UserName.Foreground = Brushes.Red;
danmakuTextControl.Text.Foreground = Brushes.LightBlue;
}
text = text.Trim("\r\n ".ToCharArray());
danmakuTextControl.Text.Text = text;
danmakuTextControl.ChangeHeight();
Storyboard storyboard = (Storyboard)danmakuTextControl.Resources["Storyboard1"];
storyboard.Completed += new EventHandler(this.sb_Completed);
this.overlay.LayoutRoot.Children.Add(danmakuTextControl);
}
if (!warn && showdanmu)
{
lock (this.fulloverlay.LayoutRoot.Children)
{
FullScreenDanmaku fullScreenDanmaku = new FullScreenDanmaku();
fullScreenDanmaku.Text.Text = text;
fullScreenDanmaku.ChangeHeight();
double width = fullScreenDanmaku.Text.DesiredSize.Width;
Dictionary<double, bool> dictionary = new Dictionary<double, bool>();
dictionary.Add(0.0, true);
foreach (object current in this.fulloverlay.LayoutRoot.Children)
{
if (current is FullScreenDanmaku)
{
FullScreenDanmaku fullScreenDanmaku2 = current as FullScreenDanmaku;
if (!dictionary.ContainsKey((double)Convert.ToInt32(fullScreenDanmaku2.Margin.Top)))
{
dictionary.Add((double)Convert.ToInt32(fullScreenDanmaku2.Margin.Top), true);
}
if (fullScreenDanmaku2.Margin.Left > SystemParameters.PrimaryScreenWidth - width - 50.0)
{
dictionary[(double)Convert.ToInt32(fullScreenDanmaku2.Margin.Top)] = false;
}
}
}
double top;
if (dictionary.All(p => !p.Value))
{
top = dictionary.Max(p => p.Key) + fullScreenDanmaku.Text.DesiredSize.Height;
}
else
{
top = (from p in dictionary
where p.Value
select p).Min(p => p.Key);
}
Storyboard storyboard2 = new Storyboard();
Duration duration = new Duration(TimeSpan.FromTicks(Convert.ToInt64((SystemParameters.PrimaryScreenWidth + width) / Store.FullOverlayEffect1 * 10000000.0)));
ThicknessAnimation thicknessAnimation = new ThicknessAnimation(new Thickness(SystemParameters.PrimaryScreenWidth, top, 0.0, 0.0), new Thickness(-width, top, 0.0, 0.0), duration);
storyboard2.Children.Add(thicknessAnimation);
storyboard2.Duration = duration;
Storyboard.SetTarget(thicknessAnimation, fullScreenDanmaku);
Storyboard.SetTargetProperty(thicknessAnimation, new PropertyPath("(FrameworkElement.Margin)", new object[0]));
this.fulloverlay.LayoutRoot.Children.Add(fullScreenDanmaku);
storyboard2.Completed += new EventHandler(this.s_Completed);
storyboard2.Begin();
}
}
}
else
{
this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(delegate
{
this.AddDMText(user, text, warn);
}));
}
}
private void Window_Closed(object sender, EventArgs e)
{
App.Current.Shutdown();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.timer = new DispatcherTimer(TimeSpan.FromSeconds(1), DispatcherPriority.Normal, new EventHandler(this.FuckMicrosoft), base.Dispatcher);
this.timer.Start();
this.textBox.Text += ("Start timer\n");
this.renderTimer = new DispatcherTimer(TimeSpan.FromMilliseconds(200), DispatcherPriority.Render, new EventHandler(this.AddDanmu), base.Dispatcher);
this.renderTimer.Start();
this.textBox.Text += ("Start consumer\n");
this.OpenOverlay();
this.overlay.Show();
this.textBox.Text += ("Start overlay\n");
this.OpenFullOverlay();
this.fulloverlay.Show();
this.textBox.Text += ("Start full overlay\n");
try
{
HttpListener listener = new HttpListener();
listener.Prefixes.Add("http://+:7773/");
listener.Start();
listener.BeginGetContext(ProcessRequest, listener);
}
catch (Exception ex)
{
MessageBox.Show(this, ex.Message);
App.Current.Shutdown();
}
this.textBox.Text += ("Start RPC server\n");
notifyIcon = new System.Windows.Forms.NotifyIcon();
notifyIcon.BalloonTipText = "弹幕DA☆ZE";
notifyIcon.BalloonTipTitle = "DanmukuApp";
notifyIcon.Text = "DanmukuApp Tray";
notifyIcon.Icon = System.Drawing.SystemIcons.WinLogo;
notifyIcon.Visible = true;
notifyIcon.ShowBalloonTip(800);
notifyIcon.MouseClick += new System.Windows.Forms.MouseEventHandler(notifyIcon_MouseClick);
notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu(new System.Windows.Forms.MenuItem[]
{
new System.Windows.Forms.MenuItem("关于", (obj, args) =>
System.Windows.MessageBox.Show(this, "DanmukuApp by bajdcc", "NewsApp")),
new System.Windows.Forms.MenuItem("-"),
new System.Windows.Forms.MenuItem("显示全屏弹幕", (obj, args) =>
DanmukuService.danmu = true),
new System.Windows.Forms.MenuItem("关闭全屏弹幕", (obj, args) =>
DanmukuService.danmu = false),
new System.Windows.Forms.MenuItem("-"),
new System.Windows.Forms.MenuItem("关闭", (obj, args) =>
Close()),
});
DelayHide();
}
private async void DelayHide()
{
await Task.Delay(TimeSpan.FromSeconds(2)).ContinueWith((task) =>
{
if (this.Dispatcher.CheckAccess())
{
Hide();
}
else
{
this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(delegate
{
Hide();
}));
}
});
}
private void notifyIcon_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (IsVisible)
{
Activate();
}
else
{
Show();
}
}
private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Escape)
{
Hide();
}
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
notifyIcon.Dispose();
}
}
}