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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With