Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Centring a hta window

Tags:

html

hta

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.

like image 601
09stephenb Avatar asked Mar 10 '14 13:03

09stephenb


2 Answers

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.

like image 91
Teemu Avatar answered Sep 22 '22 19:09

Teemu


var winWidth = 800, winHeight = 600;
window.resizeTo(winWidth, winHeight);
window.moveTo((screen.width - winWidth) / 2, (screen.height - winHeight) / 2);
like image 22
Pedro404 Avatar answered Sep 22 '22 19:09

Pedro404