How do I centre an HTA window so that 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.
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
.