我如何将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>
Run Code Online (Sandbox Code Playgroud)
但是,如果我改变屏幕分辨率它不起作用,它会调整HTA窗口的大小.
问题:无论屏幕分辨率的HTA大小如何,如何将HTA移动到屏幕中心.
为此使用 WMI 有点矫枉过正。您可以使用screenobject 代替:
window.resizeTo (800, 600);
window.moveTo((screen.width - 800) / 2, (screen.height - 600) / 2);
Run Code Online (Sandbox Code Playgroud)
如果您想在开始时调整窗口大小,请将上面的行放在该部分中的 (JS)script标记中head,甚至在<hta: application>标记之前,或者在事件处理程序中或您需要的任何地方使用该代码。
如果要将任务栏的高度排除在居中区域之外,则可以使用screen.availHeight代替screen.height。