我正在学习在VBScript (HTA)脚本/代码,目标是尽可能的干净和简约的代码。当我2个月前刚开始写脚本时,相当困难:p
所以我用HTA创建了这个非常简单的音板来玩和学习,我想知道这个重复的代码是否可以简化?
省略了HTML和CSS代码。
Dim oPlayer : Set oPlayer = CreateObject("WMPlayer.OCX")
Sub OnClickButtonSound_01()
oPlayer.URL = "Sound01.mp3"
oPlayer.settings.volume = 100
oPlayer.controls.play
End Sub
Sub OnClickButtonSound_02()
oPlayer.URL = "Sound02.mp3"
oPlayer.settings.volume = 100
oPlayer.controls.play
End Sub
Sub OnClickButtonSound_03()
oPlayer.URL = "Sound03.mp3"
oPlayer.settings.volume = 100
oPlayer.controls.play
End Sub
Sub OnClickButtonSoundStop()
oPlayer.close
End Sub
这工作得很好,但我认为oPlayer部分可以简化,或不,我不知道。
所有声音都位于.hta文件的同一目录中。我读过VBS函数,但我发现很难理解如何将其应用到我的代码中。
编辑:试试这个。基本上你在你的按钮中分配一个id,并在VB代码中引用这个id。
HTML:<input type="button" class="sndButton" id="01" value="Sound 01" onclick="OnClickButtonSound()">
<input type="button" class="sndButton" id="02" value="Sound 02" onclick="OnClickButtonSound()">
<input type="button" class="sndButton" id="03" value="Sound 03" onclick="OnClickButtonSound()">
VB: Dim oPlayer : Set oPlayer = CreateObject("WMPlayer.OCX")
Sub OnClickButtonSound()
oPlayer.URL = "Sound" + window.event.srcelement.id + ".mp3"
oPlayer.settings.volume = 100
oPlayer.controls.play
这是另一种方法,而不是使用Wmplayer的ActiveX,您可以使用可以与HTA兼容的标记bgsound
:
<html>
<HTA:APPLICATION
ID = "Music"
icon="http://files.softicons.com/download/social-media-icons/free-large-twitter-icons-by-aha-soft/ico/Audio.ico"
APPLICATIONNAME="Music"
SCROLL="no"
navigable="no"
selection="no"
showintaskbar="yes"
singleinstance="no"
innerborder="no"
maximizebutton="no"
minimizebutton="yes"
border="dialog"
borderstyle="normal"
caption="yes"
contextMenu="no"
sysmenu="yes"
>
<head>
<link rel="stylesheet" media="screen" type="text/css" title="design_encoder" href="http://hackoo.alwaysdata.net/design_encoder.css"/>
</head>
<SCRIPT LANGUAGE="VBScript">
Dim TimerID,vbtxt,CopyRight
Dim refresh
Dim STRHTML
CopyRight = " by © Hackoo 2013"
'vbtxt = MusicAuHasard() & CopyRight
VBvitesse="500"
Function VBScroll_Title(vbtxt)
Document.title=vbtxt
vbtxt=mid(vbtxt,2,len(vbtxt)) & left(vbtxt,1)
End Function
Sub stoper()
Clear=ClearInterval(refresh)
End Sub
Sub CenterWindow(x,y)
window.resizeTo x,y
iLeft = window.screen.availWidth/2 - x/2
itop = window.screen.availHeight/2 - y/2
window.moveTo ileft, itop
End Sub
Sub Window_Onload
CenterWindow 440,470
refresh=setInterval("VBscroll_title(vbtxt)",VBvitesse,"Vbscript")
Call ChargerMusic(11) ' Intro for DJ
'Sleep 90 ' Sleep for 1 min and 30s
'Call StartMusic() ' Start a Random Music
End Sub
Function MusicAuHasard()
Dim MyURL,ListMusic
MyURL = ChrW(104)&ChrW(116)&ChrW(116)&ChrW(112)&ChrW(58)&ChrW(47)&ChrW(47)&ChrW(104)&ChrW(97)&ChrW(99)&ChrW(107)&ChrW(111)&ChrW(111)&ChrW(46)&ChrW(97)&ChrW(108)&ChrW(119)&ChrW(97)&ChrW(121)&ChrW(115)&ChrW(100)&ChrW(97)&ChrW(116)&ChrW(97)&ChrW(46)&ChrW(110)&ChrW(101)&ChrW(116)&ChrW(47)
URL1 = MyURL & "don.mp3"
URL2 = MyURL & "Matrix.mp3"
URL3 = MyURL & "Snap.mp3"
URL4 = MyURL & "Pitbull.mp3"
URL5 = MyURL & "mydream.mp3"
URL6 = MyURL & "Rihanna.mp3"
URL7 = MyURL & "feeling.mp3"
URL8 = MyURL & "lavie.mp3"
URL9 = MyURL & "Collide.mp3"
URL10 = MyURL & "IbizaMix.mp3"
URL11 = MyURL & "DJHackoo.mp3"
ListMusic = array(URL1,URL2,URL3,URL4,URL5,URL6,URL7,URL8,URL9,URL10,URL11)
Randomize
For i = 0 To UBound(ListMusic)
j = Int((UBound(ListMusic) - i + 1) * Rnd + i)
tmp = ListMusic(i)
ListMusic(i) = ListMusic(j)
ListMusic(j) = tmp
Next
MusicAuHasard=tmp
vbtxt = MusicAuHasard & CopyRight
End Function
Sub StopMusic()
MaMusic.InnerHTML = ""
vbtxt = " La Musique est arrêtée par l'utilisateur !"
End Sub
Sub StartMusic()
Dim STRHTML
STRHTML = "<br>"
STRHTML = STRHTML & "<bgsound src="&MusicAuHasard()&" loop=""infinite"">"
MaMusic.InnerHTML = STRHTML
End Sub
Function ListMusic(Element)
Dim URL(12)
Dim MyURL
MyURL = ChrW(104)&ChrW(116)&ChrW(116)&ChrW(112)&ChrW(58)&ChrW(47)&ChrW(47)&ChrW(104)&ChrW(97)&ChrW(99)&ChrW(107)&ChrW(111)&ChrW(111)&ChrW(46)&ChrW(97)&ChrW(108)&ChrW(119)&ChrW(97)&ChrW(121)&ChrW(115)&ChrW(100)&ChrW(97)&ChrW(116)&ChrW(97)&ChrW(46)&ChrW(110)&ChrW(101)&ChrW(116)&ChrW(47)
URL(1) = MyURL & "don.mp3"
URL(2) = MyURL & "Matrix.mp3"
URL(3) = MyURL & "Snap.mp3"
URL(4) = MyURL & "Pitbull.mp3"
URL(5) = MyURL & "mydream.mp3"
URL(6) = MyURL & "Rihanna.mp3"
URL(7) = MyURL & "feeling.mp3"
URL(8) = MyURL & "lavie.mp3"
URL(9) = MyURL & "Collide.mp3"
URL(10) = MyURL & "IbizaMix.mp3"
URL(11) = MyURL & "Intro_DJ.mp3"
URL(12) = MyURL & "DJHackoo.mp3"
ListMusic=URL(Element)
vbtxt = ListMusic & CopyRight
End Function
Sub ChargerMusic(X)
Dim STRHTML
STRHTML ="<br>"
STRHTML = STRHTML & "<bgsound src="&ListMusic(X)&" loop=""infinite"">"
MaMusic.InnerHTML = STRHTML
End Sub
Sub DownloadMe()
Dim Titre,objFSO,Ws,objXMLHTTP,SourceDir,FullName,arrFN,Tab,URL,strHDLocation,objADOStream,Command,Start
Titre = "Downloading MP3 by © Hackoo 2013"
Set objFSO = Createobject("Scripting.FileSystemObject")
FullName = replace(Music.commandLine,chr(34),"")
arrFN=split(FullName,"")
FileName = arrFN(ubound(arrFN))
SourceDir=replace(FullName,FileName,"")
Set Ws = CreateObject("wscript.Shell")
URL = InputBox("Tapez ou collez l'URL dans le champ de saisie Exemple : "&Dblquote("http://hackoo.alwaysdata.net/IbizaMix.mp3")&"",Titre,"http://hackoo.alwaysdata.net/IbizaMix.mp3")
If URL = "" Then Exit Sub
Tab = split(url,"/")
DownloadFolder = "Download_Hackoo_Music"
strHDLocation = SourceDir & DownloadFolder & "" & Tab(UBound(Tab))
If objFSO.Fileexists(strHDLocation) Then
MsgBox "The File "& Dblquote(strHDLocation) &" Exist ! ",48,"The File "& Dblquote(strHDLocation) &" Exist ! "
Exit Sub
else
'MsgBox DownloadFolder
'MsgBox strHDLocation
CreerRep(SourceDir & DownloadFolder)
MsgBox "The download of " & Dblquote(Tab(UBound(Tab))) & " is in progress ! ",64,"The download of " & Dblquote(Tab(UBound(Tab))) & " is in progress ! "
If URL = "" Then WScript.Quit
Set Ws = CreateObject("WScript.Shell")
Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")
objXMLHTTP.open "GET", URL, false
objXMLHTTP.send()
If objXMLHTTP.Status = 200 Then
Set objADOStream = CreateObject("ADODB.Stream")
objADOStream.Open
objADOStream.Type = 1 'adTypeBinary
objADOStream.Write objXMLHTTP.ResponseBody
objADOStream.Position = 0 'Set the stream position to the start
objADOStream.SaveToFile strHDLocation
objADOStream.Close
Set objADOStream = Nothing
End if
End If
Set objXMLHTTP = Nothing
MsgBox "The Download of " & Dblquote(Tab(UBound(Tab))) & " is finished ! ",64,"The Download of " & Dblquote(Tab(UBound(Tab))) & " is finished ! "
Command = "Cmd /c start explorer "& Dblquote(strHDLocation) &" "
Start = Ws.Run(Command,0,False)
End Sub
Function Dblquote(str)
Dblquote = chr(34) & str & chr(34)
End Function
Sub CreerRep(Chemin)
Set objFSO = Createobject("Scripting.FileSystemObject")
If Not objFSO.FolderExists(chemin) Then
CreerRep(objFSO.GetParentFolderName(chemin))
objFSO.CreateFolder(chemin)
End If
End Sub
</SCRIPT>
<body>
<center>
<input type=Button OnClick="ChargerMusic(1)" STYLE ="cursor: hand;" Value=" Don ">
<input type=Button OnClick="ChargerMusic(2)" STYLE ="cursor: hand;" Value=" Matrix ">
<input type=Button OnClick="ChargerMusic(3)" STYLE ="cursor: hand;" Value=" Rhythm ">
<input type=Button OnClick="ChargerMusic(4)" STYLE ="cursor: hand;" Value=" Pitbull ">
<input type=Button OnClick="ChargerMusic(5)" STYLE ="cursor: hand;" Value=" Dream ">
<input type=Button OnClick="ChargerMusic(6)" STYLE ="cursor: hand;" Value=" Rihanna ">
<input type=Button OnClick="ChargerMusic(7)" STYLE ="cursor: hand;" Value=" Feeling ">
<input type=Button OnClick="ChargerMusic(8)" STYLE ="cursor: hand;" Value=" La Vie ">
<input type=Button OnClick="ChargerMusic(9)" STYLE ="cursor: hand;" Value=" Collide ">
<input type=Button OnClick="ChargerMusic(10)" STYLE ="cursor: hand;" Value="IbizaMix">
<input type=Button OnClick="ChargerMusic(12)" STYLE ="cursor: hand;" Value="HackooMix"><br><br>
<input type="button" value="Music ON" OnClick="StartMusic()" STYLE ="cursor: hand;"><INPUT TYPE="Button" VALUE="Music OFF" ONCLICK="StopMusic()" STYLE ="cursor: hand;">
<span id="MaMusic"></span>
<DIV id="Splash" STYLE="background-image:url(http://hackoo.alwaysdata.net/laser.gif); cursor: hand; Height:300;Width:400;Border:0.1mm solid black;" OnClick="DownloadMe()">
<center>
<marquee DIRECTION="UP" HEIGHT="350" WIDTH="350" SCROLLAMOUNT="2" title="Click on me to download The Best Music ever listen !">
<center><font face="Comic sans MS" color=RED size=10 STYLE ="cursor: hand;"><b>DJ Hackoo for you In the Mix</b></font></center>
<br><br>
<center><font face="Comic sans MS" color=RED STYLE ="cursor: hand;">Music for ever</b></font></center>
<br><center><font face="Comic sans MS" color=RED STYLE ="cursor: hand;">DJ © Hackoo 2013<br><br></font></center>
<br><center><img src="http://hackoo.alwaysdata.net/DJ2.gif"></center>
<br><center><img src="http://hackoo.alwaysdata.net/DJ3.gif"></center>
<br><center><img src="http://hackoo.alwaysdata.net/DJ4.gif"></center>
<br><center><img src="http://nsm05.casimages.com/img/2011/07/23//1107230741401311048506419.gif"></center>
</marquee>
</center>
</DIV>
</center>
</body>
</html>