How do I centre a HTA window so it is in the centre of the screen regardless of the screen resolution of the size of the HTA I have this:

</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>

But if I change the screen resolution it doesn't work and it resizes the HTA window.

Question: How do I move the HTA to the centre of the screen regardless of the HTA size of screen resolution.

有帮助吗?

解决方案

Using WMI for this is a bit overkilling. You can use screen object instead:

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

If you want to resize the window at start, put the lines above to a (JS) script tag in the head section, even before <hta: application> tag, or use the code in an event handler or where ever you need it.

If you want to exclude taskbar's height out of the centering area, you can use screen.availHeight instead of screen.height.

其他提示

var winWidth = 800, winHeight = 600;
window.resizeTo(winWidth, winHeight);
window.moveTo((screen.width - winWidth) / 2, (screen.height - winHeight) / 2);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top