在VB中,有一文本框里是數字,如何用聲音報出其中的數字,這個數字是變化的。
熱心網友
用VB編寫報分程序我們經常在計算器上、電話里聽到小姐如銀鈴般美妙的報數聲能否由計算機自動報分呢?當然可以。既然要報分,當然要有聲音,你就用Win98自帶的錄音機錄上0到10這十一個數字,每個數字保存為一個WAV文件(取名為0。wav、1。wav、……10。wav),還有點、百、千、萬、分這五個字也要分別保存為WAV文件(取名為d。wav、b。wav、q。wav、w。wav、f。wav)。并將這些文件與下面編寫的工程文件放在同一文件夾下。經我多次實踐,每個WAV文件的發音時長為0。38秒為宜。接下來,我們就可以開始編程了。在VB6中新建一個工程文件,在Form1中建立一個Textbox控件Text1,兩個CommandButton控件Command1和Command2。Text1的Text屬性為空,Command1的Caption屬性為“報分”,Command2的Caption屬性為“退出”。源程序如下:OptionExplicitPrivateDeclareFunctionsndPlaySoundLib“winmm。dll”Alias“sndPlaySoundA”(ByVallpszSoundNameAsString,ByValuFlagsAsLong)AsLongPrivateSubbf_zcx(x,y,z)'報分子程序,y為數x的長度,z為數x的整數位數Dima,num,num1,i,tIfVal(x)=0Thena=sndPlaySound(App。Path+“\”+“0。wav”,&H0)'報0GoToaa'App。Path返回工程文件或EXE文件的當前路徑EndIft=1Fori=1Toz'報整數部分num=Right(Left(x,i),1)IfNot(i=1Andz=2Andnum=1)Then'當x為十位數,且十位數為1時,不報1Ifnum=0Thent=0'當前數num為0時,不報0ElseIft=0Then'當前數不為0,而其前一數為0時,報0和當前數numa=sndPlaySound(App。Path+“\”+“0。wav”,&H0)'報0a=sndPlaySound(App。Path+“\”&num&“。wav”,&H0)'報數numt=1Elsea=sndPlaySound(App。Path+“\”&num&“。wav”,&H0)'報數numEndIfEndIfEndIfIfz-i+1=5Andnum0Then'當num為0時,不報萬a=sndPlaySound(App。Path+“\”+“w。wav”,&H0)'報萬EndIfIfz-i+1=4Andnum0Then'當num為0時,不報千a=sndPlaySound(App。Path+“\”+“q。wav”,&H0)'報千EndIfIfz-i+1=3Andnum0Then'當num為0時,不報百a=sndPlaySound(App。Path+“\”+“b。wav”,&H0)'報百EndIfIfz-i+1=2Andnum0Then'當num為0時,不報十a=sndPlaySound(App。Path+“\”+“10。wav”,&H0)'報十EndIfNextiIfy=zThenGoToaaa=sndPlaySound(App。Path+“\”+“d。wav”,&H0)'報點t=1Fori=z+2Toy'報小數部分num=Right(Left(x,i),1)a=sndPlaySound(App。Path+“\”&num&“。wav”,&H0)'報數numIft=3ThenExitFort=t+1 (App。Path+“\”+“f。wav”,&H0)'報分EndSubPrivateSubCommand1_Click()Dima,iIfText1。Text99999Theni=MsgBox(“分數超過99999,不允許報分!”,vbExclamation,“錯誤”)GoToaaEndIfa=Trim(Text1。Text)Callbf_zcx(a,Len(a),IIf(Int(Val(a))=Val(a),Len(a),InStr(a,“。”)-1))aa:EndSubPrivateSubCommand1_KeyPress(KeyAsciiAsInteger)IfKeyAscii=13Then'13為回車鍵KeyAscii=0'避免計算機發出報警聲CallCommand1_ClickEndIfEndSubPrivateSubCommand2_Click()UnloadMe'本按鈕控制軟件的退出EndSubPrivateSubForm_unLoad(cancelAsInteger)End'退出程序EndSubPrivateSubText1_KeyPress(KeyAsciiAsInteger)IfKeyAscii=13Then'13為回車鍵KeyAscii=0'避免計算機發出報警聲CallCommand1_ClickExitSubEndIfIfKeyAscii57ThenIfKeyAscii127AndKeyAscii46AndKeyAscii8ThenKeyAscii=0Beep'發出報警聲ExitSubEndIfEndIfEndSub以上程序在Win98、VB6上,運行通過。