-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.vb
1809 lines (1246 loc) · 62.4 KB
/
MainWindow.xaml.vb
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Imports System.Text
#Const CoreAudioApi = True
#Const UseWndProc = True
Imports System.Collections.ObjectModel
Imports System.Globalization
Imports System.IO
Imports Shell32
Imports Microsoft.Win32
Imports System.Windows.Threading
Imports System.Windows.Controls.Primitives
Imports System.Runtime.InteropServices
Imports System.Windows.Interop
Imports System.ComponentModel
Imports System.Net
Imports System.Text.RegularExpressions
#If CoreAudioApi Then
Imports CoreAudioApi
#End If
Imports System.Threading
Imports MyMusicWPF.Net
Class MainWindow
#Region "之前的重写窗口消息"
#If UseWndProc Then
'重写窗口呈现
Protected Overrides Sub OnSourceInitialized(e As EventArgs)
MyBase.OnSourceInitialized(e)
Dim hwndSource As HwndSource = PresentationSource.FromVisual(Me)
If hwndSource IsNot Nothing Then
'hwndSource.AddHook(New HwndSourceHook(AddressOf WndProc))
End If
End Sub
'Public Const WM_RBUTTONDOWN = &H204 '当鼠标右键在窗口客户区按下
'Public Const WM_NCRBUTTONDOWN = &HA4
'Public Const WM_SYSCOMMAND = &H112
Const WM_NCRBUTTONUP = &HA5 '当鼠标右键在非窗口客户区松开
'重写消息循环
Protected Overridable Function WndProc(hwnd As IntPtr, msg As Integer, wParam As IntPtr, lParam As IntPtr, ByRef handled As Boolean) As IntPtr
Select Case msg
Case WM_NCRBUTTONUP
Dim title_ContextMenu As Grid = LogicalTreeHelper.FindLogicalNode(mywin, "MusicIcon_Grid")
title_ContextMenu.ContextMenu.IsOpen = True
handled = True
Case Else
handled = False
End Select
End Function
#End If
#End Region
'重写窗口布局变化
Protected Overrides Function ArrangeOverride(arrangeBounds As Size) As Size
mywin = VisualTreeHelper.GetChild(MainWindow, 0)
Dim MainBorder As Border = LogicalTreeHelper.FindLogicalNode(mywin, "MainBorder")
If WindowState = WindowState.Maximized Then
MainBorder.Margin = New Thickness(7)
Else
MainBorder.Margin = New Thickness(0)
End If
Return MyBase.ArrangeOverride(arrangeBounds)
End Function
'==============================程序定义的变量==========================================
Dim mywin As DependencyObject '用于 查找 模块 控件
Dim Isrndplay As Boolean = False
Dim rndmusiclist As ArrayList '随机 列表
Dim IsUserChangeSystemVol As Boolean = True '控制 调节 系统音量
Dim IsMouseDownOnSlider As Boolean = False '控制调节slider进度
#If CoreAudioApi Then
Dim sysvoldevice As New MMDeviceEnumerator '获取系统音量
#End If
Dim AllMusicList As New MusicList '该list是主要的歌曲列表,显示在主程序的大的listbox里的数据
Dim SearchMusicList As New MusicList '搜索列表
Public m_Lrc_List As New LrcData_List '储存歌词的列表
Dim Lrc_Map As Dictionary(Of TimeSpan, String) '储存歌词的 Map
Dim Thread_Lrc As Thread '显示歌词的Timer
Dim Thread_RefreshProgress As Thread '刷新进度条的线程
Dim Thread_LoadMusicInfo As Thread '加载歌曲信息的线程
Dim LoadForm As New LoadForm
Dim currentPos As TimeSpan
Public IsFirstRun As Boolean = True
Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles MyBase.Loaded
LoadForm.Show()
mylistbox.DisplayMemberPath = "MusicNameAndAuthors"
mylistbox.ItemsSource = AllMusicList
Thread_LoadMusicInfo = New Thread(New ThreadStart(AddressOf LoadMp3Info)) With {.IsBackground = True}
Thread_LoadMusicInfo.SetApartmentState(ApartmentState.STA)
Thread_LoadMusicInfo.Start()
LoadLastSetting()
''歌词 Timer
'Thread_Lrc = New Thread(AddressOf LrcThread_Tick)
'Thread_Lrc.IsBackground = True
'Thread_Lrc.Start()
m_LrcPanel.mainwin = Me
''音量 Timer
'VolTimer.Interval = TimeSpan.FromSeconds(0.1)
'AddHandler VolTimer.Tick, AddressOf VolTimer_Tick
'刷新 播放进度条
Thread_RefreshProgress = New Thread(New ThreadStart(AddressOf RefreshProgressThread)) With {.IsBackground = True}
Thread_RefreshProgress.Start()
End Sub
#Region "加载函数"
Sub LoadDefalutSetting()
Title = "我的音乐播放器"
MusicPlayer.Source = Nothing
Dim MusicPlayButton As ToggleButton = LogicalTreeHelper.FindLogicalNode(mywin, "MusicPlayButton")
MusicPlayButton.IsChecked = False
nowcolor_rect.Fill = New SolidColorBrush(Color.FromArgb(255, 227, 20, 0))
sliderProgress.Value = 0
SliderWidth.Maximum = SystemParameters.PrimaryScreenWidth
SliderHeight.Maximum = SystemParameters.PrimaryScreenHeight
For Each x As Rectangle In Rect_Parent.Children
AddHandler x.MouseDown, AddressOf Rect_MouseDown
Next
Dim s = CType(nowcolor_rect.Fill, SolidColorBrush)
Dim _color = s.Color
sliderR.Value = _color.R
sliderG.Value = _color.G
sliderB.Value = _color.B
sliderA.Value = _color.A
Resources("MainColor") = Color.FromArgb(sliderA.Value, sliderR.Value, sliderG.Value, sliderB.Value)
Resources("MainColorBrush") = New SolidColorBrush(Color.FromArgb(sliderA.Value, sliderR.Value, sliderG.Value, sliderB.Value))
My.Settings.Reset()
#If CoreAudioApi Then
Try
'关联系统声音到本程序
Dim voldecice As MMDevice = sysvoldevice.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia)
If voldecice IsNot Nothing Then
AddHandler voldecice.AudioEndpointVolume.OnVolumeNotification, AddressOf 获取系统主音量
End If
'初始化 音量面板系统音量
Dim sysvolchecked As CheckBox = LogicalTreeHelper.FindLogicalNode(mywin, "sysvolchecked")
Dim systemvolslider As Slider = LogicalTreeHelper.FindLogicalNode(mywin, "systemvolslider")
Dim mmdevice As MMDevice = sysvoldevice.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia)
sysvolchecked.IsChecked = mmdevice.AudioEndpointVolume.Mute
systemvolslider.Value = mmdevice.AudioEndpointVolume.MasterVolumeLevelScalar
Catch ex As Exception
End Try
#End If
'初始化 控件模板 的引用
Dim m_Background_Image As Image = LogicalTreeHelper.FindLogicalNode(mywin, "m_BackgroundImage")
m_Background_Image.Source = New BitmapImage((New Uri("Frozen.jpg", UriKind.Relative)))
End Sub
Sub LoadLastSetting()
'载入音量
Dim volslider As Slider = LogicalTreeHelper.FindLogicalNode(mywin, "volslider")
volslider.Value = My.Settings.Vol
Dim Title_Grid As Grid = LogicalTreeHelper.FindLogicalNode(mywin, "MusicIcon_Grid")
Dim temp_menuitem As System.Windows.Controls.MenuItem = Title_Grid.ContextMenu.Items(5)
temp_menuitem.IsChecked = My.Settings.ShowLRC
'载入上次播放的音乐
If My.Settings.MusicLoc <> "" Then
MusicPlayer.Source = New Uri(My.Settings.MusicLoc, UriKind.Absolute)
MusicPlayer.Position = My.Settings.MusicPos
Backgeffect.Value = My.Settings.Blur
Backgopacity.Value = My.Settings.Opacity
textMusicPos.Text = My.Settings.textMusicPos
Title = My.Settings.MusicTitle
sliderProgress.Maximum = My.Settings.MucisTotal
If Double.IsNaN(My.Settings.MusicPercent) Then
My.Settings.MusicPercent = 0
End If
sliderProgress.Value = My.Settings.MusicPercent * My.Settings.MucisTotal
End If
'程序主体颜色设置
If My.Settings.mycolor.IsEmpty = False Then
nowcolor_rect.Fill = New SolidColorBrush(Color.FromArgb(My.Settings.mycolor.A, My.Settings.mycolor.R, My.Settings.mycolor.G, My.Settings.mycolor.B))
Else
nowcolor_rect.Fill = New SolidColorBrush(Color.FromArgb(255, 227, 20, 0))
End If
SliderWidth.Maximum = SystemParameters.PrimaryScreenWidth
SliderHeight.Maximum = SystemParameters.PrimaryScreenHeight
For Each x As Rectangle In Rect_Parent.Children
AddHandler x.MouseDown, AddressOf Rect_MouseDown
Next
Dim s = CType(nowcolor_rect.Fill, SolidColorBrush)
Dim _color = s.Color
sliderR.Value = _color.R
sliderG.Value = _color.G
sliderB.Value = _color.B
sliderA.Value = _color.A
Resources("MainColor") = Color.FromArgb(sliderA.Value, sliderR.Value, sliderG.Value, sliderB.Value)
Resources("MainColorBrush") = New SolidColorBrush(Color.FromArgb(sliderA.Value, sliderR.Value, sliderG.Value, sliderB.Value))
#If CoreAudioApi Then
Try
'关联系统声音到本程序
Dim voldecice As MMDevice = sysvoldevice.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia)
If voldecice IsNot Nothing Then
AddHandler voldecice.AudioEndpointVolume.OnVolumeNotification, AddressOf 获取系统主音量
End If
'初始化 音量面板系统音量
Dim sysvolchecked As CheckBox = LogicalTreeHelper.FindLogicalNode(mywin, "sysvolchecked")
Dim systemvolslider As Slider = LogicalTreeHelper.FindLogicalNode(mywin, "systemvolslider")
Dim mmdevice As MMDevice = sysvoldevice.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia)
sysvolchecked.IsChecked = mmdevice.AudioEndpointVolume.Mute
systemvolslider.Value = mmdevice.AudioEndpointVolume.MasterVolumeLevelScalar
Catch ex As Exception
End Try
#End If
'初始化 控件模板 的引用
Dim m_Background_Image As Image = LogicalTreeHelper.FindLogicalNode(mywin, "m_BackgroundImage")
Dim sBackgroundImage As String = My.Settings.BackgroundImagePath
If sBackgroundImage IsNot Nothing Then
If File.Exists(sBackgroundImage) Then
m_Background_Image.Source = New BitmapImage((New Uri(sBackgroundImage, UriKind.RelativeOrAbsolute)))
Else
m_Background_Image.Source = New BitmapImage((New Uri("Frozen.jpg", UriKind.Relative)))
End If
Else
m_Background_Image.Source = New BitmapImage((New Uri("Frozen.jpg", UriKind.Relative)))
End If
End Sub
Sub dll()
Dim restart_bool As Boolean = False
Dim resources As System.Resources.ResourceManager = My.Resources.ResourceManager
If File.Exists(Environment.CurrentDirectory + "\Interop.Shell32.dll") = False Then
If Environment.OSVersion.Version.Major >= 7 Then
Dim b() As Byte = resources.GetObject("Interop.Shell32_Win8")
Dim s As IO.Stream = File.Create(Environment.CurrentDirectory + "\Interop.Shell32.dll") '要保存的路径
s.Write(b, 0, b.Length)
s.Close()
Else
Dim b() As Byte = resources.GetObject("Interop_Shell32_Win7")
Dim s As IO.Stream = File.Create(Environment.CurrentDirectory + "\Interop.Shell32.dll") '要保存的路径
s.Write(b, 0, b.Length)
s.Close()
End If
restart_bool = True
Else
Dim dllfile As New FileInfo(Environment.CurrentDirectory + "\Interop.Shell32.dll")
If dllfile.Length = 36864 Then
If Environment.OSVersion.Version.Major >= 7 Then
dllfile.Delete()
Dim b() As Byte = resources.GetObject("Interop_Shell32_Win8")
Dim s As IO.Stream = File.Create(Environment.CurrentDirectory + "\Interop.Shell32.dll") '要保存的路径
s.Write(b, 0, b.Length)
s.Close()
restart_bool = True
End If
ElseIf dllfile.Length = 38912 Then
If Environment.OSVersion.Version.Major = 6 Then
dllfile.Delete()
Dim b() As Byte = resources.GetObject("Interop_Shell32_Win7")
Dim s As IO.Stream = File.Create(Environment.CurrentDirectory + "\Interop.Shell32.dll") '要保存的路径
s.Write(b, 0, b.Length)
s.Close()
restart_bool = True
End If
End If
End If
If File.Exists(Environment.CurrentDirectory + "\CoreAudioApi.dll") = False Then
Dim b() As Byte = resources.GetObject("CoreAudioApi")
Dim s As IO.Stream = File.Create(Environment.CurrentDirectory + "\CoreAudioApi.dll") '要保存的路径
s.Write(b, 0, b.Length)
s.Close()
restart_bool = True
End If
If restart_bool = True Then System.Windows.Forms.Application.Restart() : Application.Current.Shutdown()
End Sub
'Dim OpenFile As New OpenFileDialog
''' <summary>
''' 加载命令行
''' </summary>
Sub LoadCmdLine()
If Environment.GetCommandLineArgs.Length >= 2 Then
If Path.GetExtension(Environment.GetCommandLineArgs(1)).ToLower.Contains("mp3") Then
If File.Exists(Environment.GetCommandLineArgs(1)) Then
Dim MusicPlayButton As ToggleButton = LogicalTreeHelper.FindLogicalNode(mywin, "MusicPlayButton")
Dim sh As New ShellClass
Dim dir As Folder = sh.NameSpace(Path.GetDirectoryName(Environment.GetCommandLineArgs(1)))
Dim item As FolderItem = dir.ParseName(Path.GetFileName(Environment.GetCommandLineArgs(1)))
If dir.GetDetailsOf(item, 21).Contains("-") Then
Title = dir.GetDetailsOf(item, 21)
Else
Title = dir.GetDetailsOf(item, 21) + " - " + dir.GetDetailsOf(item, 20)
End If
MusicImage.Source = GetMusicImage(Environment.GetCommandLineArgs(1))
If MusicImage Is Nothing Then
MusicImage.Source = New BitmapImage(New Uri("Default_MusciImage.png", UriKind.RelativeOrAbsolute))
End If
MusicPlayer.Source = New Uri(Environment.GetCommandLineArgs(1))
MusicPlayer.Play()
MusicPlayButton.ToolTip = "暂停"
MusicPlayButton.IsChecked = True
TaskBarPlayBtn.ImageSource = New BitmapImage(New Uri("pack://application:,,,/Resources/pause.ico"))
End If
End If
End If
End Sub
#End Region
#Region "线程"
Dim sw As New Stopwatch()
''显示歌词线程
'Sub LrcThread_Tick()
' ''''''''''''''显示 歌词相关''''''''''''''''''''''''
' While True
' Dim i As Integer = 0
' If LrcList.Count <> 0 Then
' For i = 1 To LrcList.Count
' If Mid(currentPos.ToString, 4, 5) = Mid(LrcList(i - 1), 1, 5) Then
' Me.Dispatcher.Invoke(Sub()
' LrcTextBlock.Text = LrcList(i - 1).ToString.Split("]")(1)
' LrcTextBlock.InvalidateVisual()
' End Sub)
' End If
' Next
' End If
' Thread.Sleep(300)
' End While
'End Sub
Sub RefreshProgressThread()
While True
Dispatcher.Invoke(New Action(Sub()
If MusicPlayer.Source IsNot Nothing And MusicPlayer.NaturalDuration.HasTimeSpan Then
If IsMouseDownOnSlider = False Then
sliderProgress.Value = MusicPlayer.Position.TotalSeconds
End If
sliderProgress.Maximum = MusicPlayer.NaturalDuration.TimeSpan.TotalSeconds
textMusicPos.Text = MusicPlayer.Position.Minutes.ToString() + ":" + MusicPlayer.Position.Seconds.ToString("00.#") + " / " + MusicPlayer.NaturalDuration.TimeSpan.Minutes.ToString() + ":" + MusicPlayer.NaturalDuration.TimeSpan.Seconds.ToString("00.#")
taskbar.ProgressValue = sliderProgress.Value / sliderProgress.Maximum
currentPos = MusicPlayer.Position
End If
Dim locfromwin As Point = MusicImage.TranslatePoint(New Point(0, 0), Me)
taskbar.ThumbnailClipMargin = New Thickness(locfromwin.X + 1, locfromwin.Y, ActualWidth - MusicImage.ActualWidth - locfromwin.X, ActualHeight - MusicImage.ActualHeight - locfromwin.Y + 1)
End Sub))
Thread.Sleep(1000)
End While
End Sub
Private Sub LoadMp3Info()
Dispatcher.Invoke(New Action(Sub()
AllMusicList.Clear()
End Sub))
If My.Settings.MusicPaths Is Nothing OrElse My.Settings.MusicPaths = "" Then
Dim FBDialog As New Forms.FolderBrowserDialog()
FBDialog.ShowNewFolderButton = False
If FBDialog.ShowDialog = Forms.DialogResult.OK Then
My.Settings.MusicPaths = FBDialog.SelectedPath
My.Settings.Save()
End If
End If
If Directory.Exists(My.Settings.MusicPaths) Then
For Each path As String In Directory.GetFiles(My.Settings.MusicPaths, "*.mp3", SearchOption.AllDirectories)
Dispatcher.Invoke(New Action(Sub()
AllMusicList.Add(New Music() With {.MusicLoc = path})
End Sub))
Next
End If
Parallel.ForEach(AllMusicList, Sub(temp_music As Music)
Dim dir As Folder = Nothing
Dim item As FolderItem = Nothing
Me.Dispatcher.Invoke(New Action(Sub()
Dim sh As New ShellClass
dir = sh.NameSpace(Path.GetDirectoryName(temp_music.MusicLoc))
item = dir.ParseName(Path.GetFileName(temp_music.MusicLoc))
End Sub))
temp_music.MusicAuthors = dir.GetDetailsOf(item, 20)
temp_music.MusicBitrate = dir.GetDetailsOf(item, 28)
temp_music.MusicGenre = dir.GetDetailsOf(item, 16)
temp_music.MusicLength = dir.GetDetailsOf(item, 27)
temp_music.MusicSize = dir.GetDetailsOf(item, 1)
temp_music.MusicAlbum = dir.GetDetailsOf(item, 14)
temp_music.MusicYear = dir.GetDetailsOf(item, 15)
temp_music.MusicTitle = dir.GetDetailsOf(item, 21)
If temp_music.MusicTitle = "" Then temp_music.MusicTitle = Path.GetFileNameWithoutExtension(temp_music.MusicLoc)
If temp_music.MusicTitle.Contains("-") Then
temp_music.MusicNameAndAuthors = temp_music.MusicTitle
Else
temp_music.MusicNameAndAuthors = temp_music.MusicTitle + " - " + temp_music.MusicAuthors
End If
End Sub)
For index = 0 To AllMusicList.Count - 1
Dim temp As Int16 = index
If AllMusicList(index).MusicLoc = My.Settings.MusicLoc Then
Me.Dispatcher.Invoke(New Action(Sub()
playingitem.Text = (1 + temp).ToString()
End Sub))
Exit For
End If
Next
Dispatcher.Invoke(New Action(Sub()
在列表框中选中要播放的歌曲()
Visibility = Visibility.Visible
MusicPathTextBlock.Text = My.Settings.MusicPaths
'重新加载就按歌名升序排序
AllMusicList.Sort("MusicTitle", 1)
mylistbox.ItemsSource = AllMusicList
mylistbox.InvalidateArrange()
If LoadForm IsNot Nothing Then
LoadForm.Close()
End If
If IsFirstRun Then
IsFirstRun = False
Left = (SystemParameters.PrimaryScreenWidth - ActualWidth) / 2
Top = (SystemParameters.PrimaryScreenHeight - ActualHeight) / 2
WindowState = WindowState.Normal
Me.Activate()
End If
IsFirstRun = False
End Sub))
Return
End Sub
#End Region
#Region "单击颜色方块,改变主题颜色相关"
Private Sub Rect_MouseDown(sender As Object, e As MouseButtonEventArgs)
Me.Resources("MainColorBrush") = CType(sender, Rectangle).Fill
Dim s As String = CType(sender, Rectangle).Fill.ToString
Dim _color As SolidColorBrush = CType(sender, Rectangle).Fill
Dim a = _color.Color.A
Dim r = _color.Color.R
Dim g = _color.Color.G
Dim b = _color.Color.B
Me.Resources("MainColor") = Color.FromArgb(a, r, g, b)
Me.Resources("MainColorBrush") = New SolidColorBrush(Color.FromArgb(a, r, g, b))
nowcolor_rect.Fill = New SolidColorBrush(Color.FromArgb(a, r, g, b))
sliderR.Value = r
sliderG.Value = g
sliderB.Value = b
sliderA.Value = a
End Sub
Private Sub sliderR_MouseMove(sender As Object, e As System.Windows.Input.MouseEventArgs) Handles sliderR.MouseMove, sliderG.MouseMove, sliderB.MouseMove, sliderA.MouseMove
If e.LeftButton = MouseButtonState.Pressed Then
Me.Resources("MainColor") = Color.FromArgb(sliderA.Value, sliderR.Value, sliderG.Value, sliderB.Value)
Me.Resources("MainColorBrush") = New SolidColorBrush(Color.FromArgb(sliderA.Value, sliderR.Value, sliderG.Value, sliderB.Value))
End If
End Sub
'重置背景色
Private Sub ResetBackImage_MouseDown(sender As Object, e As MouseButtonEventArgs)
Dim m_Background_Image As Image = LogicalTreeHelper.FindLogicalNode(mywin, "m_BackgroundImage")
m_Background_Image.Source = New BitmapImage(New Uri("pack://application:,,,/Frozen.jpg", UriKind.RelativeOrAbsolute))
My.Settings.BackgroundImagePath = "pack://application:,,,/Frozen.jpg"
End Sub
'改变背景图片
Private Sub ChangeBackImg_MouseDown(sender As Object, e As MouseButtonEventArgs) Handles ChangeBackImg.MouseDown
Dim m_Background_Image As Image = LogicalTreeHelper.FindLogicalNode(mywin, "m_BackgroundImage")
Dim open As New Microsoft.Win32.OpenFileDialog
open.Title = "选择背景图片"
open.CheckFileExists = True
open.Multiselect = False
open.Filter = "图片文件|*.jpg;*.png;*.bmp|所有文件|*.*"
open.ShowDialog()
If open.FileName = "" Then Exit Sub
Dim backimg As New ImageBrush(New BitmapImage(New Uri(open.FileName)))
backimg.Stretch = Stretch.UniformToFill
m_Background_Image.Source = New BitmapImage(New Uri(open.FileName, UriKind.RelativeOrAbsolute))
My.Settings.BackgroundImagePath = open.FileName
My.Settings.Save()
End Sub
#End Region
#Region "listbox右上角3的数字——操作"
'左击最左边按钮 将listbox 使正在播放项 获焦 可视
Private Sub playingitem_MouseDown(sender As Object, e As MouseButtonEventArgs) Handles playingitem.MouseDown
If e.ClickCount >= 1 And MusicPlayer.Source IsNot Nothing Then
Dim n As Integer
For Each x As Music In mylistbox.Items
n += 1
If MusicPlayer.Source.OriginalString = x.MusicLoc Then
mylistbox.ScrollIntoView(mylistbox.Items(n - 1))
mylistbox.SelectedItem = mylistbox.Items(n - 1)
playingitem.ToolTip = "正在播放第 " + (n).ToString + " 首歌:" + vbCrLf + x.MusicNameAndAuthors
Exit Sub
End If
Next
'正在播放的项被删除之后:
mylistbox.ScrollIntoView(mylistbox.Items(playingitem.Text - 1))
mylistbox.SelectedItem = mylistbox.Items(playingitem.Text - 1)
End If
End Sub
'左击中间按钮 将listbox 使选中项 获焦 可视
Private Sub txtSelectedIndex_MouseDown(sender As Object, e As MouseButtonEventArgs) Handles txtSelectedIndex.MouseDown
If txtSelectedIndex.Text = "-1" Or txtSelectedIndex.Text = "" Then Exit Sub
If e.ClickCount > 1 And MusicPlayer.Source IsNot Nothing Then
Dim n As Integer
For Each x As Music In mylistbox.Items
n += 1
If MusicPlayer.Source.OriginalString = x.MusicLoc Then
mylistbox.ScrollIntoView(mylistbox.Items(n - 1))
mylistbox.SelectedItem = mylistbox.Items(n - 1)
Exit Sub
End If
Next
End If
mylistbox.ScrollIntoView(mylistbox.SelectedItem)
End Sub
'右双击最右边数字重新ListBox
Private Sub txtCount_MouseDown(sender As Object, e As MouseButtonEventArgs) Handles txtCount.MouseDown
mylistbox.InvalidateArrange()
End Sub
#End Region
#Region "控制播放"
Sub PlayMusic()
If mylistbox.Items.Count = 0 Then
MessageBox.Show("没有找任何歌曲呀!", "提示信息!")
Return
End If
Dim MusicPlayButton As ToggleButton = LogicalTreeHelper.FindLogicalNode(mywin, "MusicPlayButton")
Dim Title_Grid As Grid = LogicalTreeHelper.FindLogicalNode(mywin, "MusicIcon_Grid")
Dim temp_menuitem As System.Windows.Controls.MenuItem = Title_Grid.ContextMenu.Items(0)
temp_menuitem.Header = "暂停"
MusicPlayer.Play()
If MusicPlayButton.IsChecked = True Then
TaskBarPlayBtn.ImageSource = New BitmapImage(New Uri("pack://application:,,,/Resources/pause.ico"))
Else
TaskBarPlayBtn.ImageSource = New BitmapImage(New Uri("pack://application:,,,/Resources/play.ico"))
End If
taskbar.ProgressState = Windows.Shell.TaskbarItemProgressState.Normal
End Sub
Sub PlayMusic(ByVal path As String)
If mylistbox.Items.Count = 0 Then
MessageBox.Show("没有找任何歌曲呀!", "提示信息!")
Return
End If
Dim Title_Grid As Grid = LogicalTreeHelper.FindLogicalNode(mywin, "MusicIcon_Grid")
Dim temp_menuitem As System.Windows.Controls.MenuItem = Title_Grid.ContextMenu.Items(0)
temp_menuitem.Header = "暂停"
MusicPlayer.Source = Nothing
MusicPlayer.Source = New Uri(path, UriKind.RelativeOrAbsolute)
MusicPlayer.Position = New TimeSpan(0)
sliderProgress.Value = 0
MusicPlayer.Play()
在列表框中选中要播放的歌曲()
End Sub
Sub PauseMusic()
Dim Title_Grid As Grid = LogicalTreeHelper.FindLogicalNode(mywin, "MusicIcon_Grid")
Dim temp_menuitem As System.Windows.Controls.MenuItem = Title_Grid.ContextMenu.Items(0)
temp_menuitem.Header = "播放"
MusicPlayer.Pause()
Dim MusicPlayButton As ToggleButton = LogicalTreeHelper.FindLogicalNode(mywin, "MusicPlayButton")
MusicPlayButton.ToolTip = "播放"
taskbar.ProgressState = Windows.Shell.TaskbarItemProgressState.Paused
TaskBarPlayBtn.ImageSource = New BitmapImage(New Uri("pack://application:,,,/Resources/play.ico"))
End Sub
'播放器打开音乐触发事件
Private Sub MusicPlayer_MediaOpened(sender As Object, e As RoutedEventArgs) Handles MusicPlayer.MediaOpened
If MusicPlayer.Source.OriginalString.Contains(My.Settings.MusicLoc) Then
MusicPlayer.Position = My.Settings.MusicPos
End If
在列表框中选中要播放的歌曲()
Dim MusicPlayButton As ToggleButton = LogicalTreeHelper.FindLogicalNode(mywin, "MusicPlayButton")
Dim Title_Grid As Grid = LogicalTreeHelper.FindLogicalNode(mywin, "MusicIcon_Grid")
Dim temp_menuitem As System.Windows.Controls.MenuItem = Title_Grid.ContextMenu.Items(0)
temp_menuitem.Header = "暂停"
MusicPlayButton.ToolTip = "暂停"
MusicPlayButton.IsChecked = True
taskbar.ProgressState = Windows.Shell.TaskbarItemProgressState.Normal
TaskBarPlayBtn.ImageSource = New BitmapImage(New Uri("pack://application:,,,/Resources/pause.ico"))
Dim locfromwin As Point = MusicImage.TranslatePoint(New Point(0, 0), Me)
taskbar.ThumbnailClipMargin = New Thickness(locfromwin.X + 1, locfromwin.Y, ActualWidth - MusicImage.ActualWidth - locfromwin.X, ActualHeight - MusicImage.ActualHeight - locfromwin.Y + 1)
MusicImage.Source = GetMusicImage(MusicPlayer.Source.OriginalString)
Try
Dim title_ContextMenu As Grid = LogicalTreeHelper.FindLogicalNode(mywin, "MusicIcon_Grid")
Dim showlrc_menuitem As System.Windows.Controls.MenuItem = title_ContextMenu.ContextMenu.Items(5)
If showlrc_menuitem.IsChecked Then
Dim flag As Boolean = LoadLRC()
m_LrcPanel.m_ItemControl.Visibility = Windows.Visibility.Visible
MusicImage.Opacity = 0
InfoGrid.Visibility = Windows.Visibility.Collapsed
Else
m_LrcPanel.m_ItemControl.Visibility = Windows.Visibility.Hidden
MusicImage.Opacity = 1
InfoGrid.Visibility = Windows.Visibility.Visible
End If
Catch ex As Exception
m_LrcPanel.m_ItemControl.Visibility = Windows.Visibility.Hidden
MusicImage.Opacity = 1
InfoGrid.Visibility = Windows.Visibility.Visible
m_LrcPanel.timer.Stop()
If m_LrcPanel.list_height IsNot Nothing Then m_LrcPanel.list_height.Clear()
If m_LrcPanel.m_list_lrc IsNot Nothing Then m_LrcPanel.m_list_lrc.Clear()
m_Lrc_List.Clear()
End Try
End Sub
'播放器 播放结束
Private Sub MusicPlayer_MediaEnded(sender As Object, e As EventArgs)
LrcTextBlock.Text = ""
If Isrndplay = True Then 随机播放音乐() : 在列表框中选中要播放的歌曲() : Exit Sub
For index = 0 To AllMusicList.Count - 1
If AllMusicList(index).MusicLoc = MusicPlayer.Source.OriginalString Then
If index >= mylistbox.Items.Count - 1 Then PlayMusic(AllMusicList(0).MusicLoc) : Exit Sub
PlayMusic(AllMusicList(index + 1).MusicLoc)
Exit Sub
End If
Next
End Sub
Sub 在列表框中选中要播放的歌曲()
If MusicPlayer.Source Is Nothing Then Return
For Each x In AllMusicList
If x.MusicLoc Like MusicPlayer.Source.OriginalString Then
mylistbox.SelectedItem = x
mylistbox.ScrollIntoView(mylistbox.SelectedItem)
playingitem.Text = mylistbox.SelectedIndex + 1
playingitem.ToolTip = "正在播放第 " + (mylistbox.SelectedIndex + 1).ToString + " 首歌:" + vbCrLf + x.MusicNameAndAuthors
textTitle.Text = x.MusicTitle
textSonger.Text = x.MusicAuthors
Title = x.MusicNameAndAuthors
Exit For
End If
Next
End Sub
Sub 随机播放音乐()
'先把正在播放的歌曲从随机列表中删除
Try
rndmusiclist.Remove(MusicPlayer.Source.OriginalString)
Catch ex As Exception
End Try
Dim musiccount As Integer = rndmusiclist.Count
Dim x As Integer
Dim i As Integer = 0
If musiccount > 1 Then
Dim rnd As New Random()
x = rnd.Next(1, musiccount) - 1
MusicImage.Source = GetMusicImage(rndmusiclist(x))
If MusicImage Is Nothing Then
MusicImage.Source = New BitmapImage(New Uri("Default_MusciImage.png", UriKind.RelativeOrAbsolute))
End If
MusicPlayer.Source = New Uri(rndmusiclist(x), UriKind.Absolute)
MusicPlayer.Play()
Else
MusicImage.Source = GetMusicImage(rndmusiclist(0))
If MusicImage Is Nothing Then
MusicImage.Source = New BitmapImage(New Uri("Default_MusciImage.png", UriKind.RelativeOrAbsolute))
End If
MusicPlayer.Source = New Uri(rndmusiclist(0), UriKind.Absolute)
MusicPlayer.Play()
rndmusiclist.Clear()
'将 所有歌曲列表 加载到 rndmusiclist
For Each m As Music In AllMusicList
rndmusiclist.Add(m.MusicLoc)
If m.MusicLoc = MusicPlayer.Source.OriginalString Then
mylistbox.SelectedIndex = i
End If
Next
rndmusiclist.Remove(MusicPlayer.Source.OriginalString)
Exit Sub
End If
For Each m As Music In mylistbox.Items
If m.MusicLoc = MusicPlayer.Source.OriginalString Then
mylistbox.SelectedIndex = i : mylistbox.ScrollIntoView(mylistbox.SelectedItem) : Exit For
End If
i += 1
Next
'设置标题 歌曲
For Each temp_music As Music In AllMusicList
If MusicPlayer.Source.OriginalString = temp_music.MusicLoc Then
Me.Title = temp_music.MusicNameAndAuthors
textTitle.Text = temp_music.MusicTitle
textSonger.Text = temp_music.MusicAuthors
Exit For
End If
Next
rndmusiclist.Remove(rndmusiclist(x))
End Sub
'播放音乐:左上角播放按钮; 标题栏 右击菜单 播放
Private Sub MusicPlayButton_Click(sender As Object, e As EventArgs)
Dim MusicPlayButton As ToggleButton = LogicalTreeHelper.FindLogicalNode(mywin, "MusicPlayButton")
If sender.ToString.Contains("Thum") Then
MusicPlayButton.IsChecked = Not MusicPlayButton.IsChecked
End If
If AllMusicList.Count = 0 Then
MessageBox.Show("没有找任何歌曲呀!", "提示信息!", MessageBoxButton.OK, MessageBoxImage.Information)
Return
End If
If MusicPlayer.Source Is Nothing Then
If MusicPlayButton.IsChecked Then
PlayMusic(AllMusicList(0).MusicLoc)
Else
PauseMusic()
End If
Else
If MusicPlayButton.IsChecked Then
PlayMusic()
Else
PauseMusic()
End If
End If
End Sub
'标题菜单栏的右键菜单 播放
Private Sub MusicContextMenu_Play_Click(sender As Object, e As RoutedEventArgs)
Dim MusicPlayButton As ToggleButton = LogicalTreeHelper.FindLogicalNode(mywin, "MusicPlayButton")
MusicPlayButton.IsChecked = Not MusicPlayButton.IsChecked
MusicPlayButton_Click(sender, Nothing)
End Sub
'左双击列表框项目 播放音乐
Private Sub ListBoxItem_MouseLeftButtonDown(sender As Object, e As MouseButtonEventArgs)
If e.ClickCount >= 2 Then
Dim mygrid As Grid = sender
Dim lbi As ListBoxItem = mygrid.TemplatedParent
Dim tempmusic As Music = CType(lbi.Content, Music)
PlayMusic(tempmusic.MusicLoc)
MusicPlayer.Position = New TimeSpan
sliderProgress.Value = 0
End If
End Sub
'标题栏 下一首 按钮
Private Sub MusicnextButton_Click(sender As Object, e As EventArgs)
If MusicImage.Source Is Nothing Then Exit Sub
For index = 0 To AllMusicList.Count - 1
If AllMusicList(index).MusicLoc = MusicPlayer.Source.OriginalString Then
If index = AllMusicList.Count - 1 Then PlayMusic(AllMusicList.First.MusicLoc) : Exit Sub
'PlayMusic(AllMusicList(index + 1).MusicLoc)
MusicPlayer_MediaEnded(sender, e)
Exit Sub
End If
Next
End Sub
'任务栏中 停止 按钮控制事件
Private Sub TaskBarStopBtn_Click(sender As Object, e As EventArgs)
MusicPlayer.Stop()
PauseMusic()
End Sub
'任务栏中 上一首