WMP 冲刺应用程序
使用 Windows Media Player 组件

引言
这是一个我上学期间空闲时间编写的有趣的小程序。这是我发布的第一个文章,请多多包涵。:)
我参考了 Alastair Dallas 撰写的一篇文章:Tiny StopWatch Application(微型秒表应用程序)。
我还参考了以下教程:Using WMP(使用 WMP)。
背景
Power Hour 是一种许多大学生玩的游戏。 规则是每分钟喝一口啤酒,持续一个小时。 我导入了 Windows Media Player COM 组件,以便每分钟切换一首歌曲。
Using the Code
第一个代码片段是按钮控件的点击事件过程。
' 1) Button Control
Dim startTime As DateTime
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
' If the button is clicked then start the timer.
' Once the timer is started and the button is clicked again,
' the timer is stopped and the player fails
If (Timer1.Enabled) Then
Timer1.Stop()
MessageBox.Show("You did not complete power hour", "Failed")
Label3.Text = String.Empty
Label4.Text = String.Empty
Else
startTime = DateTime.Now()
Timer1.Start()
Button1.Text = "Stop!"
End If
End Sub
第二个代码片段是计时器控件。
' 2) Timer Control
Private Sub Timer1_Tick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Timer1.Tick
Dim span As TimeSpan = DateTime.Now.Subtract(startTime)
Label4.Text = span.Seconds.ToString
ProgressBar1.Value += 1
If ProgressBar1.Value = 60 Then
ProgressBar1.Value = 0
ProgressBar2.Value += 1
Dim span2 As TimeSpan = DateTime.Now.Subtract(startTime)
Label3.Text = span2.Minutes.ToString
wmp.Ctlcontrols.next()
If ProgressBar2.Value = 60 Then
MessageBox.Show("You've completed power hours", _
"Congratulations!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
ProgressBar2.Value = 0
End If
End If
End Sub
第三个代码片段是导入播放列表的菜单项。
' 3) Menu Item to add Play List
Private Sub AddMusicToolStripMenuItem_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles AddMusicToolStripMenuItem.Click
Dim dlg As New OpenFileDialog()
dlg.InitialDirectory = "C:\"
dlg.CheckFileExists = True
dlg.Title = "Import a Playlist from WMP"
dlg.Filter = "Windows Media Playlists|*.wpl"
dlg.Multiselect = False
dlg.ShowDialog()
wmp.URL = dlg.FileName
End Sub
这个小项目的所有代码就是这些了。 你需要添加 Windows Media Player COM 组件。 要做到这一点,右键单击工具箱,选择“选择项...”,选择“COM 组件”,然后选中“Windows Media Player”。 现在你可以将该控件添加到你的窗体中。
关注点
这个项目中最好的一点是 WMP COM 组件。 我设置了允许人们导入他们的 WMP 播放列表,并且计时器每分钟切换一首歌曲。 这可以防止人们编辑整首歌曲并在一分钟处截断它,就像我看到的一些人所做的那样。
如果您有任何问题,请通过 dayk995@cobleskill.edu 与我联系。
历史
- 2007 年 7 月 31 日:初始发布