1、VB 通过 BEEP 函数 来演奏音乐!Option Explicit以下是窗体代码,在 VB6 调试通过。需在窗体放置下面 5 个控件: Command1:不必设置任何属性 Command2:不必设置任何属性 Command3:不必设置任何属性 Combo1:不必设置任何属性 Text1:在属性窗口设置 MultiLine=True,ScrollBars=2,HideSelection=False本人原创,转载请注明来源:http:/ Declare Function Beep Lib “kernel32“ (ByVal dwFreq As Long, ByVal dwDuration As
2、 Long) As LongDim ctEsc As Boolean, ctExit As Boolean, ctV As Single, ctP() As Single, ctPs As LongPrivate Sub Form_Load()Dim I As LongMe.Caption = “让主板播放乐曲“: Command3.Caption = “停止(&S)“Command1.Caption = “全部播放(&A)“: Command2.Caption = “播放选中部分(&P)“Text1.Font.Name = “宋体“: Text1.Font.Size = 9Call SetP
3、Name 定义音名频率Combo1.AddItem “ 0.25 倍速度“Combo1.AddItem “ 0.4 倍速度“Combo1.AddItem “ 0.5 倍速度“Combo1.AddItem “ 0.6 倍速度“Combo1.AddItem “ 0.7 倍速度“Combo1.AddItem “ 0.8 倍速度“Combo1.AddItem “ 0.9 倍速度“Combo1.AddItem “ 1 倍速度“Combo1.AddItem “ 1.25 倍速度“Combo1.AddItem “ 1.5 倍速度“Combo1.AddItem “ 1.75 倍速度“Combo1.AddIte
4、m “ 2 倍速度“Combo1.AddItem “ 3 倍速度“Combo1.AddItem “ 4 倍速度“Combo1.Text = “ 1 倍速度“例子乐曲Text1.Text = “ 粉刷匠 “ & vbCrLf & _“ 5 3 5 3 | 5 3 1- |2 4 3 2 | 5-|“ & vbCrLf & _“ 我是一个 粉刷 匠,粉刷本能 强, “ & vbCrLf & _“ 5 3 5 3 | 5 3 1- |2 4 3 2 | 1-|“ & vbCrLf & _“ 我要把那 新房 子,刷得很漂 亮。 “ & vbCrLf & _“ 2 2 4 4 | 3 1 5- |2 4
5、 3 2 | 5-|“ & vbCrLf & _“ 刷了房顶 又刷 墙,刷子飞舞 忙, “ & vbCrLf & _“ 5 3 5 3 | 5 3 1- |2 4 3 2 | 1- |“ & vbCrLf & _“ 哎呀我的 小鼻 子,变呀变了 样。“Text1.Text = Text1.Text & vbCrLf & vbCrLf & “ 龙的传人“ & vbCrLf & _“ 6.- 7.1 | 2- 3 2 |1- 1 7.| 6.-|“ & vbCrLf & _“ 遥 远的 东 方有 一 条 江, “ & vbCrLf & _“ 6.- 7.1 | 2- 3 2 | 17. 12| 3
6、- |“ & vbCrLf & _“ 他 的 名 字就 叫 长 江。“Text1.Text = Text1.Text & vbCrLf & vbCrLf & “ 六一国际儿童节歌“ & vbCrLf & _“ 1- 55 | 61 5- |1- 55 |615-|6- 56 |32 3- |6- 56 |32 1-|“ & vbCrLf & vbCrLf & _“ 6- 56 |1- 6| 55 35 |6- |3- 23|5-6|32 13|2- | 6-5 |“ & vbCrLf & vbCrLf & _“ 1- 16| 5- 35|6-|3-2|5- 61|3- 23|1-|1216|
7、5- 35|“ & vbCrLf & vbCrLf & _“ 65 35 | 16 5- |1216| 5- 3- | 55 61| 5-6 | 1113 |“ & vbCrLf & vbCrLf & _“ 2226|1-|“End SubPrivate Sub Form_Resize()Dim L As Long, T As Long, H As Long, W1 As LongOn Error Resume NextW1 = Me.TextWidth(“A“)Command1.Move W1, W1, W1 * 13, W1 * 4Command2.Move W1 * 15, W1, W1
8、 * 17, W1 * 4Command3.Move W1 * 33, W1, W1 * 10, W1 * 4T = W1 * 6Combo1.Move W1, T, W1 * 20T = T + Combo1.Height + W1: H = Me.ScaleHeight - TText1.Move 0, T, Me.ScaleWidth, HEnd SubPrivate Sub Form_Unload(Cancel As Integer)ctExit = TrueEnd SubPrivate Sub SetPName()定义音名频率ctPs = 7: ReDim ctP(1 To ctPs
9、)ctP(1) = 261.63 1#=227.18ctP(2) = 293.66 2#=311.13ctP(3) = 326.63ctP(4) = 349.23 4#369.99ctP(5) = 392 5#=415.3ctP(6) = 440 6#=466.16ctP(7) = 493.88End SubPrivate Sub Combo1_Change()Combo1_ClickEnd SubPrivate Sub Combo1_Click()On Error Resume NextctV = 1 / Val(Combo1.Text) 播放速度If ctV 4 Then ctV = 4I
10、f ctV 7 Then GoTo Next1H = 1: S = 0N2 = Mid(nStr, I + 1, 1)If N2 = “.“ Or N2 = “。“ Then S = 1: H = H * 0.5 音高:降低八度If N2 = “ Or N2 = “ Then S = 1: H = H * 2 音高 :升高八度T = 1DoN2 = Mid(nStr, I + S + T, 1)If N2 “ Then Exit DoT = T + 1LoopText1.SelStart = nStart + I - 1: Text1.SelLength = T + SDoEventsIf ctExit Then Exit SubIf ctEsc Then Exit ForBeep ctP(N) * H, T * ctV * 200 N:音名, H:音高, T:音长Next1:NextText1.Locked = False: Command1.Enabled = True: Command2.Enabled = TrueMe.Caption = “让主板播放乐曲“If OnlySel Then Text1.SelStart = nStart: Text1.SelLength = nLenEnd Sub