以hta窗为中心

时间:2014-03-10 13:19:09

标签: html hta

如何将HTA窗口居中,使其位于屏幕中央,无论HTA大小的屏幕分辨率如何:

</head>
<script>
Sub DoResize 'Do not use Window_Onload
   window.resizeTo 800,600
   strComputer = "."
   Set objWMIService = GetObject("Winmgmts:\\" & strComputer & "\root\cimv2")
   Set colItems = objWMIService.ExecQuery("Select * From Win32_DesktopMonitor")
   For Each objItem in colItems
       intHorizontal = objItem.ScreenWidth
       intVertical = objItem.ScreenHeight
   Next
   intLeft = (intHorizontal - 800) / 2
   intTop = (intVertical - 600) / 2
   window.moveTo intLeft, intTop
End Sub
DoResize() 'Run the subroutine to position the containing window (your HTA dialog) before the body is rendered.
</script>
<body>

但如果我改变屏幕分辨率它不起作用,它会调整HTA窗口的大小。

问题:无论屏幕分辨率的HTA大小如何,如何将HTA移动到屏幕中心。

2 个答案:

答案 0 :(得分:4)

使用WMI有点过分。您可以改为使用screen对象:

window.resizeTo (800, 600);
window.moveTo((screen.width - 800) / 2, (screen.height - 600) / 2);

如果您想在开始时调整窗口大小,请将上面的行放在script部分的(JS)head标记中,甚至在<hta: application>标记之前,或使用代码在事件处理程序中或您需要它的地方。

如果要将任务栏的高度排除在居中区域之外,可以使用screen.availHeight代替screen.height

答案 1 :(得分:1)

var winWidth = 800, winHeight = 600;
window.resizeTo(winWidth, winHeight);
window.moveTo((screen.width - winWidth) / 2, (screen.height - winHeight) / 2);