📄 form1.vb
字号:
Option Strict Off
Option Explicit On
Friend Class Form1
Inherits System.Windows.Forms.Form
#Region "Windows 窗体设计器生成的代码"
Public Sub New()
MyBase.New()
If m_vb6FormDefInstance Is Nothing Then
If m_InitializingDefInstance Then
m_vb6FormDefInstance = Me
Else
Try
'对于启动窗体,所创建的第一个实例为默认实例。
If System.Reflection.Assembly.GetExecutingAssembly.EntryPoint.DeclaringType Is Me.GetType Then
m_vb6FormDefInstance = Me
End If
Catch
End Try
End If
End If
'此调用是 Windows 窗体设计器所必需的。
InitializeComponent()
End Sub
'窗体重写处置,以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal Disposing As Boolean)
If Disposing Then
If Not components Is Nothing Then
components.Dispose()
End If
End If
MyBase.Dispose(Disposing)
End Sub
Private components As System.ComponentModel.IContainer
'Windows 窗体设计器所必需的
Public ToolTip1 As System.Windows.Forms.ToolTip
Public WithEvents Timer1 As System.Windows.Forms.Timer
Public WithEvents Command6 As System.Windows.Forms.Button
Public WithEvents Command5 As System.Windows.Forms.Button
Public WithEvents Command4 As System.Windows.Forms.Button
Public WithEvents Command3 As System.Windows.Forms.Button
Public WithEvents Command2 As System.Windows.Forms.Button
Public WithEvents Command1 As System.Windows.Forms.Button
'注意:以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器来修改它。
'不要使用代码编辑器来修改它。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container()
Me.Command6 = New System.Windows.Forms.Button()
Me.Command1 = New System.Windows.Forms.Button()
Me.Command3 = New System.Windows.Forms.Button()
Me.Command2 = New System.Windows.Forms.Button()
Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
Me.ToolTip1 = New System.Windows.Forms.ToolTip(Me.components)
Me.Command5 = New System.Windows.Forms.Button()
Me.Command4 = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'Command6
'
Me.Command6.BackColor = System.Drawing.SystemColors.Control
Me.Command6.Cursor = System.Windows.Forms.Cursors.Default
Me.Command6.Location = New System.Drawing.Point(256, 46)
Me.Command6.Name = "Command6"
Me.Command6.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.Command6.Size = New System.Drawing.Size(114, 29)
Me.Command6.TabIndex = 5
Me.Command6.Text = "退出"
'
'Command1
'
Me.Command1.BackColor = System.Drawing.SystemColors.Control
Me.Command1.Cursor = System.Windows.Forms.Cursors.Default
Me.Command1.Location = New System.Drawing.Point(10, 9)
Me.Command1.Name = "Command1"
Me.Command1.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.Command1.Size = New System.Drawing.Size(114, 29)
Me.Command1.TabIndex = 0
Me.Command1.Text = "弹出"
'
'Command3
'
Me.Command3.BackColor = System.Drawing.SystemColors.Control
Me.Command3.Cursor = System.Windows.Forms.Cursors.Default
Me.Command3.Location = New System.Drawing.Point(256, 9)
Me.Command3.Name = "Command3"
Me.Command3.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.Command3.Size = New System.Drawing.Size(114, 29)
Me.Command3.TabIndex = 2
Me.Command3.Text = "暂停"
'
'Command2
'
Me.Command2.BackColor = System.Drawing.SystemColors.Control
Me.Command2.Cursor = System.Windows.Forms.Cursors.Default
Me.Command2.Location = New System.Drawing.Point(133, 9)
Me.Command2.Name = "Command2"
Me.Command2.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.Command2.Size = New System.Drawing.Size(114, 29)
Me.Command2.TabIndex = 1
Me.Command2.Text = "播放"
'
'Timer1
'
Me.Timer1.Enabled = True
Me.Timer1.Interval = 1000
'
'Command5
'
Me.Command5.BackColor = System.Drawing.SystemColors.Control
Me.Command5.Cursor = System.Windows.Forms.Cursors.Default
Me.Command5.Location = New System.Drawing.Point(133, 46)
Me.Command5.Name = "Command5"
Me.Command5.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.Command5.Size = New System.Drawing.Size(114, 29)
Me.Command5.TabIndex = 4
Me.Command5.Text = "上一首"
'
'Command4
'
Me.Command4.BackColor = System.Drawing.SystemColors.Control
Me.Command4.Cursor = System.Windows.Forms.Cursors.Default
Me.Command4.Location = New System.Drawing.Point(10, 46)
Me.Command4.Name = "Command4"
Me.Command4.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.Command4.Size = New System.Drawing.Size(114, 29)
Me.Command4.TabIndex = 3
Me.Command4.Text = "下一首"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(389, 87)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Command6, Me.Command5, Me.Command4, Me.Command3, Me.Command2, Me.Command1})
Me.KeyPreview = True
Me.Location = New System.Drawing.Point(4, 23)
Me.MaximizeBox = False
Me.MinimizeBox = False
Me.Name = "Form1"
Me.Text = "使用MCI指令的CD播放器"
Me.ResumeLayout(False)
End Sub
#End Region
#Region "升级支持"
Private Shared m_vb6FormDefInstance As Form1
Private Shared m_InitializingDefInstance As Boolean
Public Shared Property DefInstance() As Form1
Get
If m_vb6FormDefInstance Is Nothing OrElse m_vb6FormDefInstance.IsDisposed Then
m_InitializingDefInstance = True
m_vb6FormDefInstance = New Form1()
m_InitializingDefInstance = False
End If
DefInstance = m_vb6FormDefInstance
End Get
Set
m_vb6FormDefInstance = Value
End Set
End Property
#End Region
Public Declare Function mciExecute Lib "winmm.dll" (ByVal lpstrCommand As String) As Integer
Public Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer
Dim Curtunt As Short '存放当前正在播放的曲目号
Dim Total As Short '存放CD碟片上的曲目总数
Dim RetVal As Integer '存放MCI函数的返回值
'弹出CD-ROM
Private Sub Command1_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command1.Click
RetVal = mciExecute("set cdaudio door open")
End Sub
'播放
Private Sub Command2_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command2.Click
RetVal = mciExecute("play cdaudio")
End Sub
'暂停
Private Sub Command3_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command3.Click
RetVal = mciExecute("pause cdaudio")
End Sub
'播放下一首
Private Sub Command4_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command4.Click
Dim ReturnStr As VB6.FixedLengthString = New VB6.FixedLengthString(128)
RetVal = mciExecute("set cdaudio time format tmsf")
'设置from后的时间格式
If Curtunt < Total Then RetVal = mciExecute("play cdaudio from " & Str(Curtunt + 1))
End Sub
'播放上一首
Private Sub Command5_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command5.Click
Dim ReturnStr As VB6.FixedLengthString = New VB6.FixedLengthString(128)
RetVal = mciExecute("set cdaudio time format tmsf")
If Curtunt > 1 Then RetVal = mciExecute("play cdaudio from" & Str(Curtunt - 1))
End Sub
'中止CD的播放并退出
Private Sub Command6_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command6.Click
RetVal = mciExecute("stop cdaudio")
RetVal = mciExecute("close cdaudio")
End
End Sub
'程序启动时打开CDAudio设备并得到曲目总数
Private Sub Form1_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
Dim ReturnStr As VB6.FixedLengthString = New VB6.FixedLengthString(128)
RetVal = mciSendString("open cdaudio", ReturnStr.Value, 128, 0)
RetVal = mciSendString("status cdaudio number of tracks", ReturnStr.Value, 128, 0)
Total = Val(ReturnStr.Value)
End Sub
'退出时中止CD的播放
'UPGRADE_WARNING: Form 事件 Form1.Unload 具有新的行为。 单击以获得更多信息:ms-help://MS.MSDNVS/vbcon/html/vbup2065.htm
Private Sub Form1_Closed(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Closed
RetVal = mciExecute("stop cdaudio")
RetVal = mciExecute("close cdaudio")
End Sub
'每隔一秒监测当前播放的曲目号
Private Sub Timer1_Tick(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Timer1.Tick
On Error Resume Next
Dim ReturnStr As VB6.FixedLengthString = New VB6.FixedLengthString(128)
RetVal = mciSendString("status cdaudio Current track", ReturnStr.Value, 128, 0)
Curtunt = Val(ReturnStr.Value)
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -