HTA (HTML Application) 是微軟技術,將含 VBScript/JScript 的 HTML 另存為 .hta,即成為獨立桌面應用程式。擁有完整系統權限(讀寫檔案、存取登錄、WMI、資料庫等),介面以 HTML+CSS 實作,開發效率極高。
<html>
<head>
<title>第一個 HTA</title>
</head>
<body>
<center>HTA - HTML Application</center>
</body>
</html>
存為 demo.hta,雙擊即可執行。甚至可省略 <html>, <body> 標籤。
置於 <head> 內,屬性如下:
<HTA:APPLICATION
ID="oHTA"
APPLICATIONNAME="MyApp"
BORDER="thin"
BORDERSTYLE="raised"
CAPTION="yes"
ICON="app.ico"
MAXIMIZEBUTTON="yes"
MINIMIZEBUTTON="yes"
SHOWINTASKBAR="no"
SINGLEINSTANCE="yes"
SYSMENU="yes"
VERSION="1.0"
WINDOWSTATE="normal"
SCROLL="yes">
| 屬性 | 說明 | 預設值 |
|---|---|---|
| APPLICATIONNAME | 應用程式名稱 (SINGLEINSTANCE 識別用) | |
| BORDER | 邊框類型: thick, dialog, none, thin | thick |
| BORDERSTYLE | 邊框風格: normal, complex, raised, static, sunken | normal |
| CAPTION | 是否顯示標題列 | yes |
| ICON | 視窗圖示路徑 | |
| MAXIMIZEBUTTON | 顯示最大化按鈕 | yes |
| MINIMIZEBUTTON | 顯示最小化按鈕 | yes |
| SHOWINTASKBAR | 工作列顯示 | yes |
| SINGLEINSTANCE | 單一實例 (依 APPLICATIONNAME) | no |
| SYSMENU | 系統選單 | yes |
| VERSION | 版本號 | |
| WINDOWSTATE | 初始狀態: normal, minimize, maximize | normal |
| SCROLL | 捲軸 | yes |
<head>
<title>定時關機小程序</title>
<HTA:APPLICATION
APPLICATIONNAME="AutoShutdown"
BORDER="thin"
BORDERSTYLE="raised"
ICON="C:\WINDOWS\SYSTEM32\control.exe"
SCROLL="no"
SINGLEINSTANCE="yes"
MAXIMIZEBUTTON="no"
MINIMIZEBUTTON="no"
WINDOWSTATE="normal">
</head>
<script language="VBScript">
Sub Window_onLoad
' 置中視窗
strComputer = "."
Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set col = objWMI.ExecQuery("Select * From Win32_DesktopMonitor")
For Each obj In col
w = obj.ScreenWidth : h = obj.ScreenHeight
Next
window.resizeTo 240, 500
window.moveTo (w-240)/2, (h-500)/2
End Sub
Sub RunCmd(cmd)
CreateObject("WScript.Shell").Run cmd, 0, True
End Sub
Sub OneSub : RunCmd "shutdown -s -f -t 1800" : End Sub ' 30分
Sub TwoSub : RunCmd "shutdown -s -f -t 2700" : End Sub ' 45分
Sub ThrSub : RunCmd "shutdown -s -f -t 3600" : End Sub ' 60分
Sub OK
If IsNumeric(Text.Value) Then
RunCmd "shutdown -s -f -t " & (Text.Value * 60)
Else
MsgBox "請輸入有效數字"
End If
End Sub
Sub ShutDown : RunCmd "shutdown -s -t 0" : End Sub
Sub EShutDown : RunCmd "shutdown -a" : End Sub ' 取消
</script>
<body>
<center>
<input type="button" value="30 分鐘後關機" onclick="OneSub" style="height:30px;width:200px"><br><br>
<input type="button" value="45 分鐘後關機" onclick="TwoSub" style="height:30px;width:200px"><br><br>
<input type="button" value="60 分鐘後關機" onclick="ThrSub" style="height:30px;width:200px"><br><br>
自訂分鐘: <input type="text" id="Text" style="width:120px">
<input type="button" value="確定" onclick="OK"><br><br>
<input type="button" value="取消關機排程" onclick="EShutDown" style="width:200px"><br><br>
<input type="button" value="立即關機" onclick="ShutDown" style="width:200px">
</center>
</body>
更多 HTA 範例:腳本之家 HTA 專區