Private Sub Command2_Click()
Dim s As String
Dim i As Integer
s = Text1(0).Text
For i = 1 To Len(s)
Select Case Mid(s, i, 1)
Case "1"
Call PicA_Click(23)
Sleep 300
Case "2"
Call PicA_Click(34)
Sleep 300
Case "3"
Call PicA_Click(36)
Sleep 300
Case "4"
Call PicA_Click(38)
Sleep 300
Case "5"
Call PicA_Click(40)
Sleep 300
Case "6"
Call PicA_Click(42)
Sleep 300
Case "7"
Call PicA_Click(44)
Sleep 300
End Select
Next
End Sub
改编成支持两个文本框同时播放的效果,就像一首歌曲,一个主旋律和伴奏同时演奏的效果。
以下是改编后的代码,可以实现两个文本框中的内容同时播放,类似于主旋律和伴奏同时演奏的效果:
Private Sub Command2_Click()
Dim s1 As String
Dim s2 As String
Dim i As Integer
s1 = Text1(0).Text
s2 = Text1(1).Text '假设第二个文本框的名称为 Text1(1)
For i = 1 To Len(s1) Or Len(s2) '取两个字符串长度中的较大值作为循环次数
Dim note1 As String
Dim note2 As String
If i <= Len(s1) Then
note1 = Mid(s1, i, 1)
Else
note1 = "" '如果第一个字符串结束了,设置为空字符串
End If
If i <= Len(s2) Then
note2 = Mid(s2, i, 1)
Else
note2 = "" '如果第二个字符串结束了,设置为空字符串
End If
Dim midiNote1 As Integer
Dim midiNote2 As Integer
Select Case note1
Case "1"
midiNote1 = 23
Case "2"
midiNote1 = 34
Case "3"
midiNote1 = 36
Case "4"
midiNote1 = 38
Case "5"
midiNote1 = 40
Case "6"
midiNote1 = 42
Case "7"
midiNote1 = 44
Case Else
midiNote1 = 0
End Select
Select Case note2
Case "1"
midiNote2 = 23
Case "2"
midiNote2 = 34
Case "3"
midiNote2 = 36
Case "4"
midiNote2 = 38
Case "5"
midiNote2 = 40
Case "6"
midiNote2 = 42
Case "7"
midiNote2 = 44
Case Else
midiNote2 = 0
End Select
If midiNote1 > 0 Then
Call PicA_Click(midiNote1)
End If
If midiNote2 > 0 Then
Call PicA_Click(midiNote2)
End If
Sleep 300
Next
End Sub
在上述代码中,首先获取两个文本框中的内容,然后在循环中同时处理两个文本框的字符。如果某个文本框的字符已经处理完,则对应音符设置为空。接着根据字符转换为 MIDI 音符编号,并在音符编号大于 0 时调用PicA_Click
方法播放音符。最后添加了 300 毫秒的延迟。
请注意,代码中的Text1(0)
和Text1(1)
假设了文本框的名称和索引,你需要根据实际情况进行调整。同时,确保PicA_Click
方法能够正确处理 MIDI 音符编号并播放音符。