{"id":68783,"date":"2005-10-10T16:13:00","date_gmt":"2005-10-10T16:13:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/10\/10\/how-can-i-center-an-hta-on-the-screen\/"},"modified":"2005-10-10T16:13:00","modified_gmt":"2005-10-10T16:13:00","slug":"how-can-i-center-an-hta-on-the-screen","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-center-an-hta-on-the-screen\/","title":{"rendered":"How Can I Center an HTA on the Screen?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! How can I center an HTA on the screen?<BR><BR>&#8212; BH<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, BH. Well, we have an answer for you, although it\u2019s not perfect: we can resize (if need be) and center your window, but there will be a momentary flash on the screen when all this happens. It\u2019s not <I>too<\/I> terribly distracting and the net effect is what you want: the HTA will be centered on screen. We were hoping to make this process a little smoother, but this approach will have to do for now.<\/P>\n<P>Here\u2019s the code for our sample HTA. (To try this out, copy the code, paste it into Notepad, and then save the file with a <B>.hta<\/B> file extension.) The part we care about (and the only part that really does much of anything) is the <B>Window_Onload<\/B> subroutine, a subroutine which automatically runs any time the HTA is loaded or refreshed:<\/P><PRE class=\"codeSample\">&lt;html&gt;\n&lt;head&gt;<\/p>\n<p>&lt;title&gt;Centered HTA&lt;\/title&gt;<\/p>\n<p>&lt;HTA:APPLICATION \n     ID=&#8221;objHTA&#8221;\n     APPLICATIONNAME=&#8221;Centered HTA&#8221;\n     SCROLL=&#8221;yes&#8221;\n     SINGLEINSTANCE=&#8221;yes&#8221;\n&gt;\n&lt;\/head&gt;<\/p>\n<p>&lt;SCRIPT Language=&#8221;VBScript&#8221;&gt;\n    Sub Window_Onload\n        strComputer = &#8220;.&#8221;\n        Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\n        Set colItems = objWMIService.ExecQuery(&#8220;Select * From Win32_DesktopMonitor&#8221;)\n        For Each objItem in colItems\n            intHorizontal = objItem.ScreenWidth\n            intVertical = objItem.ScreenHeight\n        Next\n        intLeft = (intHorizontal &#8211; 800) \/ 2\n        intTop = (intVertical &#8211; 600) \/ 2\n        window.resizeTo 800,600\n        window.moveTo intLeft, intTop\n    End Sub\n&lt;\/SCRIPT&gt;<\/p>\n<p>&lt;body&gt;&lt;\/body&gt;\n&lt;\/html&gt;\n<\/PRE>\n<P>Start this HTA and &#8211; after a brief flash &#8211; the window will be resized to 800 pixels by 600 pixels, and will be centered onscreen.<\/P>\n<P>Good question: what <I>is<\/I> going on here? Well we start off with some standard WMI code that connects to the WMI service on the local computer and then queries the <B>Win32_DesktopMonitor<\/B> class.<\/P>\n<TABLE class=\"dataTable\" id=\"EOD\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. We\u2019re assuming that you have only one monitor connected to your machine. If you have multiple monitors you might have to add a WHERE clause that ensures you will retrieve the screen height and width of the primary monitor.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>After connecting to the Win32_DesktopMonitor class we then use these two lines of code to determine the current screen dimensions:<\/P><PRE class=\"codeSample\">intHorizontal = objItem.ScreenWidth\nintVertical = objItem.ScreenHeight\n<\/PRE>\n<P>We\u2019ve already decided, in advance, that we want our HTA window to be 800 pixels wide by 600 pixels high. Therefore we can use this code to figure out where the top left-hand corner of the window needs to be positioned:<\/P><PRE class=\"codeSample\">intLeft = (intHorizontal &#8211; 800) \/ 2\nintTop = (intVertical &#8211; 600) \/ 2\n<\/PRE>\n<P>As you can see, we\u2019re taking the screen width (stored in the variable intHorizontal) and subtracting 800 pixels (the horizontal size of the HTA window). Let\u2019s assume we have a monitor resolution of 1024&#215;768. Subtracting 800 from 1024 leaves us with 224: that tells how us how much wider the screen is than our HTA window. To center the window, we just need to make sure we have 112 pixels on each side; that\u2019s why we divide 224 (intHorizontal &#8211; 800) by 2. <\/P>\n<P>We then repeat the process for the screen height. On our sample monitor, that gives us 768 &#8211; 600, or 168. Dividing that by two leaves us with 84 pixels above and below the window.<\/P>\n<P>And <I>that<\/I> leaves us with two final chores. First, we resize the window to 800 pixels by 600 pixels:<\/P><PRE class=\"codeSample\">window.resizeTo 800,600\n<\/PRE>\n<P>Next, we position the window accordingly. That\u2019s what we do here:<\/P><PRE class=\"codeSample\">window.moveTo intLeft, intTop\n<\/PRE>\n<P>All we\u2019re doing is calling the <B>moveTo<\/B> method and moving the HTA window so that the top left-hand corner is located 112 pixels in from the left and 84 pixels down from the top. <\/P>\n<P>Incidentally, we put the <B>resizeTo<\/B> method near the bottom of the subroutine for educational purposes: we wanted to talk about resizeTo and moveTo at the same time. However, you\u2019ll get a slightly less-distracting flash if you start the subroutine off by resizing the window:<\/P><PRE class=\"codeSample\">Sub Window_Onload\n    window.resizeTo 800,600\n    strComputer = &#8220;.&#8221;\n    Set objWMIService = GetObject(&#8220;Winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\n    Set colItems = objWMIService.ExecQuery(&#8220;Select * From Win32_DesktopMonitor&#8221;)\n    For Each objItem in colItems\n        intHorizontal = objItem.ScreenWidth\n        intVertical = objItem.ScreenHeight\n    Next\n    intLeft = (intHorizontal &#8211; 800) \/ 2\n    intTop = (intVertical &#8211; 600) \/ 2\n    window.moveTo intLeft, intTop\nEnd Sub\n<\/PRE>\n<P>It\u2019s not a big deal, but it helps a little. And we\u2019ll continue to hunt for ways to eliminate the flashing. (Why is that so difficult? Well, ideally, we\u2019d hide the HTA window until it has been resized and moved, something you can do with Internet Explorer. However, we haven\u2019t found a way to do that with HTAs. But we haven\u2019t given upon that.)<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I center an HTA on the screen?&#8212; BH Hey, BH. Well, we have an answer for you, although it\u2019s not perfect: we can resize (if need be) and center your window, but there will be a momentary flash on the screen when all this happens. It\u2019s not too terribly distracting [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[3,4,5,30],"class_list":["post-68783","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-vbscript","tag-web-pages-and-htas"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I center an HTA on the screen?&#8212; BH Hey, BH. Well, we have an answer for you, although it\u2019s not perfect: we can resize (if need be) and center your window, but there will be a momentary flash on the screen when all this happens. It\u2019s not too terribly distracting [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68783","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=68783"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68783\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=68783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}