We can resize and center the window (if needed), but do so with a momentary flicker on the screen. This is not too obvious, and the actual result is what you expect: The HTA will be in the center of the screen. We want this process to be a little smoother, but at the moment we have to use this method.
The following is the code for the HTA example. (To implement this procedure, copy the code, paste it into Notepad, and then save the file with an. hta for the file name extension.) The part that we're worried about (and the only really useful part) is the Window_onload subroutine that runs automatically whenever an HTA is loaded or updated:
Copy Code code as follows:
<title>centered hta</title>
Id= "Objhta"
Applicationname= "Centered HTA"
Scroll= "Yes"
Singleinstance= "Yes"
>
<script language= "VBScript" >
Sub Window_onload
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.resizeto 800,600
Window.moveto Intleft, Inttop
End Sub
</SCRIPT>
<body></body>
Start this HTA-after a quick flash-the window will adjust to 800x600 pixels and will be located in the center of the screen.
Good question: What's going to happen here? We first use some standard WMI code, connect to the WMI service on the local computer, and then query the Win32_DesktopMonitor class.
Attention. We assume that your computer is connected to only one monitor. If you have more than one monitor, you will have to add a WHERE clause to ensure that you will retrieve the screen height and width of the primary display.
After connecting to the Win32_DesktopMonitor class, we will use the following two lines of code to determine the size of the current screen:
Copy Code code as follows:
Inthorizontal = Objitem.screenwidth
Intvertical = Objitem.screenheight
We've decided in advance that we want the HTA window to be 800 pixels wide and 600 pixels high. So, we can use the following code to figure out where to put the upper-left corner of the window:
Copy Code code as follows:
Intleft = (intHorizontal-800)/2
Inttop = (intVertical-600)/2
As you can see, we'll get the screen width (stored in the inthorizontal variable) and subtract 800 pixels (the horizontal size of the HTA window). Suppose we have a monitor with a resolution of 1024x768. 1024 minus 800 is 224: This tells us how much wider the screen is than the HTA window. To center the window, we only need to make sure that each edge is 112 pixels, which is why dividing by 224 (that is, inthorizontal–800) by 2.
Then, for the screen height, repeat the procedure. For our sample display, we get 768–600 (ie 168) and then divide by 2 to get 84 pixels (that is, the size on the bottom of the window).
In this way, we still need to do two things. First, resize the window to 800x600 pixels:
Window.resizeto 800,600
Second, place the window accordingly. And that's what we're going to do now:
Window.moveto Intleft, Inttop
All we have to do is call the MoveTo method and move the HTA window so that its upper-left corner is 112 pixels from the left and 84 pixels from the top.
By the way, we put the Resizeto method near the bottom of the subroutine for teaching purposes: we want to tell Resizeto with MoveTo. However, when you start a subroutine by adjusting the window, there is a slight, less obvious flicker:
Copy Code code as follows:
Sub 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
It's not a big problem, but it helps. We will continue to look for ways to eliminate flicker. (Why is it so difficult?) Ideally, we'll hide it before we adjust and move the HTA window, which can be performed using Internet Explorer. However, we have not yet found a way to use the HTA to do the operation. But we never give up. )