HTAs是Html Applications是,文件扩展名为hta,是微软1999年,随Windows平台发布的,目前只支持IE浏览器。但不要小心看它,例如一个叫APE+CUE小刀的开源项目, 这篇文章有介绍,就是使用HTA做为UI。如果您有安装过SQL SERVER 2005,有注意到有一个Splash.hta安装文件。更多内容,可以参考MSDN.  有以下特点:

1. HTA不是脚本语言,但它可以包含HTML, Javascript,Vbscript.

2. 以Web驱动的界面,所以很多程序使用它做UI。

下面我们再来看SVG如何嵌入于HTA中示例:

<html>
<head>
  <title>SVG-Enabled HTML Application</title>
  <meta http-equiv="x-ua-compatible" content="ie=9">
  <hta:application 
     id="oSample"
     applicationname="svghtasample1"
     version="1"
  &lt;/hta>
</head>
<script type="text/javascript">
    function openURL() {
        var shell = new ActiveXObject("WScript.Shell");
        shell.run("http://www.bing.com");
    }
  </script>
<body>
 
<p>Because this HTA includes an X-UA-Compatible header, 
it is displayed in IE9 Standards mode when Internet 
Explorer 9 is installed on the system.  As a result,
SVG can be used to draw a blue star.</p>     
 
<svg width="12cm" height="4cm" viewBox="0 0 1200 400"
     xmlns="http://www.w3.org/2000/svg" version="1.1">
  <desc>Example Star</desc>
  <polygon 
      fill="blue" stroke="blue" stroke-width="10" 
      points="350,75  379,161 469,161 397,215 423,301 
              350,250 277,301 303,215 231,161 321,161" />
</svg>
<br />
<input type="button" onclick="openURL()" value="Open Bing.com">
</body>
</html>


把上面的代码另存为一个demo.hta文件,你可马上看到SVG绘图效果。同时,倒数第3行,实现一个按钮点击后跳出Bing.com 页面。
下面让我们再看一个复杂点儿例子,显示CPU利用率的HTA:

<html>
<head>
<script language="vbscript">
  set objservice = getobject("winmgmts:{impersonationlevel=impersonate}!\root\cimv2")
  const refrtime=1000
</script>
<script type='text/javascript'>
    window.resizeTo(240, 150);
</script>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="description" content="Adapted from a couple of scripts - 1. To Get CPU Percentage - http://msdn.microsoft.com/en-us/library/windows/desktop/aa392397(v=vs.85).aspx   and 2. To run the script over and over again - http://www.windowsitpro.com/article/vbscript/utility-monitors-crucial-processes">
<title>CPU + RAM usage</title>
 
<HTA:APPLICATION
    APPLICATIONNAME="CPU Usage Percentage"
    ID="cpuUsagePercentage" 
    VERSION="0.1"
    BORDER="dialog"
    SCROLL="no"
    CONTEXTMENU="no"
    SINGLEINSTANCE="no" 
    WINDOWSTATE="normal"/> 
 
<style type="text/css">
  h3{color:purple;}
</style>
</head>
<body>
  <div id='DataArea'>waiting for info</div>
  <script language="vbscript">
  '--- ---'
  sub window_onload
    call getpercentage1()
  end sub
  '--- ---'
  sub getpercentage1()
    set objservice = getobject("winmgmts:{impersonationlevel=impersonate}!\root\cimv2")
    set objinstance1 = objservice.get("win32_perfrawdata_perfos_processor.name='_total'")
    n1 = objinstance1.percentprocessortime
    d1 = objinstance1.timestamp_sys100ns
    timeoutid = window.settimeout("call getpercentage2(" & n1 &","& d1 & ")", refrtime/2)
  end sub
  '--- ---'
  sub getpercentage2(n1, d1)
    set objservice = getobject("winmgmts:{impersonationlevel=impersonate}!\root\cimv2")
    set perf_instance2 = objservice.get("win32_perfrawdata_perfos_processor.name='_total'")
    n2 = perf_instance2.percentprocessortime
    d2 = perf_instance2.timestamp_sys100ns
    percentprocessortime = (1 - ((n2 - n1)/(d2-d1)))*100
    cpupercentage = round(percentprocessortime,2)
    dataarea.innerhtml = "<h3>cpu usage = " & cpupercentage & "%"
    timeoutid = window.settimeout("getpercentage1()", refrtime)
  end sub
  '--- ---'
  </script>
</body>
</html>


你可以看到上面有一些Vbscript与HTML,实现一个CPU Rate Monitor的Web UI.  还有更多的应用在这儿,以及帮助我们查询系统信息,管理数据库
在这儿,只是抛砖引玉,您可能有更好的想法,自己动手试一下吧,希望对您软件开发有帮助。

 


作者:Petter Liu
出处:http://www.cnblogs.com/wintersun/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
该文章也同时发布在我的独立博客中-Petter Liu Blog

posted on 2012-12-14 10:09  PetterLiu  阅读(643)  评论(0编辑  收藏  举报