利用Wscript显示GUI 选单并执行命令,类似.BAThttps://ithelp.ithome.com.tw/articles/10342688
\'Multi button GUI in Wscript,利用wscript显示视窗功能表,并执行命令,取代command/choice
\'Ref:https://stackoverflow.com/questions/21428596/multi-button-gui-vbsscript
\'Ref:https://ithelp.ithome.com.tw/articles/10342688
\'修改小地方
const FormText = "`t开启控制台" \'功能表标题
const LabelText = "请点选按钮" \'第一行文字
const MenuCols = 4 \'功能表栏数
const ButtonHeight = 25 \'高度,预设25
const ButtonWidth = 75 \'宽度,4个中文字约75
MenuTXT=Array("离开","控制台","网路连线","防火墙","电源选项","病毒设定")
MenuCMD=Array("","control","control Ncpa.cpl","control firewall.cpl","control powercfg.cpl","windowsdefender://threatsettings")
\'以下程式不需修改
MenuCount=uBound(MenuTXT) + 1
If MenuCount <> uBound(MenuCMD) + 1 Then MsgBox "功能标题与数量应该相等!" : WScript.Quit 1
Dim fso,objShell
Set fso = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
\'制作暂存ps1
const TemporaryFolder = 2
Dim sTempPath,MenuFile,pos
sTempPath = fso.GetSpecialFolder(TemporaryFolder)
MenuFile = WScript.scriptname
pos=InStrRev(MenuFile,".")
MenuFile=sTempPath+"\\"+Left(MenuFile,pos-1)+".ps1"
call MakeTempPS1(MenuFile)
\'WScript.Quit 0
\'开始显示选单/点选=>StdOut回传=>清除CR(CHR13)及LF(CHR10)
Set exec = objShell.Exec("powershell -executionpolicy bypass -noninteractive -window hidden -file "&MenuFile)
exec.StdIn.Close()
str=exec.StdOut.ReadAll()
str=REPLACE(str,vbcr,"")
str=REPLACE(str,vblf,"")
\'执行完后删除暂存档
\'WScript.Echo "["+str+"]"
If (len(str) >0 ) Then objShell.Run str,4
fso.DeleteFile(MenuFile)
Set exec = Nothing
Set fso=Nothing
Set objShell = Nothing
WScript.Quit 0
\'制作选单PS1
Sub MakeTempPS1(MenuFile)
\'WScript.Echo "["+MenuFile+"]"
Set ps=fso.OpenTextFile(MenuFile,2,True) \'2:Create :read 8:append
ps.Write "#"&Now&" Create Tempfile from "&WScript.ScriptFullName&chr(13)
dim FormW,FormH,BTX,BTY \'表单大小W*H,按钮位置XY
FormW=30+(ButtonWidth +5)*MenuCols \'25:左边界 5:按钮左边界
FormH=70+(ButtonHeight+5)*(int(MenuCount/MenuCols)+1) \'70:上边界 5:按钮上边界
ps.write "Add-Type -AssemblyName System.Windows.Forms" &chr(13)&_
"$Form = New-Object system.Windows.Forms.Form" &chr(13)&_
"$Form.Font = New-Object System.Drawing.Font(""Tahoma"",10,[System.Drawing.FontStyle]::Bold)"&chr(13)&_
"$Form.Text = """&FormText&""""&chr(13)&_
"$Form.Width = "&FormW&chr(13)&_
"$Form.Height = "&FormH+5&chr(13)&_
"$Form.ControlBox = $False"&chr(13)&_
"$Form.StartPosition = ""CenterScreen"""&chr(13)
ps.write chr(13)&_
"$Label = New-Object System.Windows.Forms.Label"&chr(13)&_
"$Label.Text = """&LabelText&""""&chr(13)&_
"$Label.AutoSize = $True"&chr(13)&_
"$Form.Controls.Add($Label)"&chr(13)
\'把第0个调到最后一个
Dim item
For i=0 to MenuCount-1 \'Array重0起算
item=i+1 : If item = MenuCount Then item=0 \'按钮左上开始点
BTX=5 +(ButtonWidth +5)*(i mod MenuCols) \'X=左边界+(按钮宽+按钮左边界)*按钮cols
BTY=25+(ButtonHeight+10)*int(i/MenuCols) \'Y=上边界+(按钮高+按钮上边界)*按钮rows
ps.write chr(13)&_
"$Button"&i+1&" = new-object System.Windows.Forms.Button"&chr(13)&_
"$Button"&i+1&".Location = new-object System.Drawing.Size("&BTX&","&BTY&")"&chr(13)&_
"$Button"&i+1&".Size = new-object System.Drawing.Size("&ButtonWidth&","&ButtonHeight&")"&chr(13)&_
"$Button"&i+1&".Text = """ &MenuTXT(item)&""""&chr(13)&_
"$Button"&i+1&".Add_Click({Write-Host """&MenuCMD(item)&""";$Form.Close()})"&chr(13)&_
"$Form.Controls.Add($Button"&i+1&")"&chr(13)
Next
ps.write chr(13)&"$Form.ShowDialog() | Out-Null"&chr(13)&"Exit 0"
ps.Close
Set ps=nothing
End Sub