Public Class Form1
    Dim plist = New ArrayList()
    Dim pos As Byte = 1
    Dim playnum As Byte

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim drivenum As Byte

        drivenum = Me.AxWindowsMediaPlayer1.cdromCollection.count
        If drivenum = 0 Then
            MessageBox.Show("CDROMドライブが利用できません。" & Chr(10) _
                            & "プログラムを終了します。")
            End
        Else
            plist = Me.AxWindowsMediaPlayer1.cdromCollection.Item(0).Playlist

            playnum = plist.count
            If playnum = 0 Then
                MessageBox.Show("再生できる音楽データがありません")
                Label1.Text = "音楽用CDがありません。"
            Else
                Label1.Text = "曲数：" & playnum
                Call ctrl_onoff(True)
                Me.AxWindowsMediaPlayer1.currentPlaylist = plist
                Label2.Text = "Track" & pos
                Timer1.Enabled = True
                Timer2.Enabled = True
            End If
        End If
    End Sub

    Private Sub ctrl_onoff(ByVal pos As Boolean)
        Me.Button1.Enabled = pos
        Me.Button4.Enabled = pos
        Me.Button3.Enabled = pos
        Me.Button4.Enabled = pos
        Me.Button5.Enabled = pos
        Me.Button5.Enabled = pos
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Me.AxWindowsMediaPlayer1.cdromCollection.Item(0).eject()
        Timer1.Enabled = False
        Timer2.Enabled = False
        Label1.Text = "音楽用CDがありません。"
        Label2.Text = ""
        Label3.Text = ""
        Call ctrl_onoff(False)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.AxWindowsMediaPlayer1.Ctlcontrols.play()
        Timer2.Enabled = True
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Label3.Text = Me.AxWindowsMediaPlayer1.Ctlcontrols.currentPositionString
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        With Me.AxWindowsMediaPlayer1
            .Ctlcontrols.previous()
            pos -= 1
            If pos = 0 Then
                pos = playnum
            End If
            Label2.Text = "Track" & pos
        End With

    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        With Me.AxWindowsMediaPlayer1
            .Ctlcontrols.next()
            pos += 1
            If pos > playnum Then
                pos = 1
            End If
            Label2.Text = "Track" & pos
        End With
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Me.AxWindowsMediaPlayer1.Ctlcontrols.stop()
        Timer2.Enabled = False
        Me.Button1.ForeColor = Color.Black

    End Sub

    Dim flag As Boolean = False
    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        With Me.Button1
            If flag = False Then
                .ForeColor = Me.Button1.BackColor
                flag = True
            Else
                .ForeColor = Color.Black
                flag = False
            End If
        End With
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.AxWindowsMediaPlayer1.Ctlcontrols.pause()
        Timer2.Enabled = False
        Me.Button1.ForeColor = Color.Black
    End Sub

    Private Sub AxWindowsMediaPlayer1_CdromMediaChange(ByVal sender As Object, ByVal e As AxWMPLib._WMPOCXEvents_CdromMediaChangeEvent) Handles AxWindowsMediaPlayer1.CdromMediaChange
        pos = 1
        plist = Me.AxWindowsMediaPlayer1.cdromCollection.Item(0).Playlist

        playnum = plist.count
        If playnum = 0 Then
            Label1.Text = "音楽用CDがありません。"
        Else
            Label1.Text = "曲数：" & playnum
            Call ctrl_onoff(True)
            Me.AxWindowsMediaPlayer1.currentPlaylist = plist
            Label2.Text = "Track" & pos
            Timer1.Enabled = True
            Timer2.Enabled = True
        End If
    End Sub

    Private Sub AxWindowsMediaPlayer1_PlayStateChange(ByVal sender As Object, ByVal e As AxWMPLib._WMPOCXEvents_PlayStateChangeEvent) Handles AxWindowsMediaPlayer1.PlayStateChange
        If e.newState = 8 Then
            pos = 1
            Timer1.Enabled = False
            Timer2.Enabled = False
            Me.Button1.ForeColor = Color.Black
            Label2.Text = "Track" & pos
        End If
    End Sub

    Public Sub New()

        ' この呼び出しは、Windows フォーム デザイナで必要です。
        InitializeComponent()

        ' InitializeComponent() 呼び出しの後で初期化を追加します。

    End Sub

    Protected Overrides Sub Finalize()
        MyBase.Finalize()
    End Sub
End Class