none
Auto-Centering VBScript based HTA RRS feed

  • Question

  • Hi ScriptingGuys/All,

    I am using the command below to call a custom HTA from within VBScript, and am having real difficulties getting it to launch in centre screen, rather than tucked up in one corner as it currently is!

    sCommand = "mshta.exe ""javascript:{new ActiveXObject(""InternetExplorer.Application"")" _
    	& ".putProperty('" & iRandom & "',window); window.resizeTo(" & WidthX & "," & HeightY & ");" _
    		& "window.moveTo(" & ("screen.Width" & - WidthX) & "/2" & "," & ("screen.Height" & - HeightY) & "/2" & ")}"""

    As you can see, I have the code which I THINK should be there and should work in the 'window.moveTo' statement, but this seems to be ignored completely!

    ('WidthX' and 'HeightY' are the two parameters passed in when calling the function, that determine the desired size of the window)

    If anyone has any suggestions or can point me in the right direction, I'd be very grateful!

    Many thanks,

    James



    Sunday, February 9, 2014 1:00 AM

Answers

  • Or this but it won't get you too far.

    sCommand = "mshta.exe ""javascript:{" _
                                   & "width=600;" _
                                   & "height=400;" _
                                   & "window.resizeTo(width,height);" _
                                   & "window.moveTo((screen.width-width)/2,(screen.height-height)/2);" _
                                   & "}"""
    Set sh = CreateObject("Wscript.Shell")
    sh.Run sCommand


    ¯\_(ツ)_/¯

    • Marked as answer by James Mealing Monday, February 10, 2014 3:05 PM
    Sunday, February 9, 2014 2:02 AM
  • Or this but it won't get you too far.

    sCommand = "mshta.exe ""javascript:{" _
                                   & "width=600;" _
                                   & "height=400;" _
                                   & "window.resizeTo(width,height);" _
                                   & "window.moveTo((screen.width-width)/2,(screen.height-height)/2);" _
                                   & "}"""
    Set sh = CreateObject("Wscript.Shell")
    sh.Run sCommand


    ¯\_(ツ)_/¯

    Please look again at this


    ¯\_(ツ)_/¯

    • Marked as answer by James Mealing Monday, February 10, 2014 3:04 PM
    • Unmarked as answer by James Mealing Monday, February 10, 2014 3:05 PM
    • Marked as answer by James Mealing Monday, February 10, 2014 3:05 PM
    Monday, February 10, 2014 2:31 PM

All replies

  • How to center an HTA:

    <html>
    
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>My HTML Application</title>
    <script language="vbscript">
        Sub window_onload()
            CenterWindow 520, 600
        End Sub
        
        Sub CenterWindow( widthX, heightY )
            self.ResizeTo widthX, heightY 
            self.MoveTo (screen.Width - widthX)/2, (screen.Height - heightY)/2
        End Sub
    
    </script>
    </head>
    <body>
    <!-- HTML goes here -->
    
    </body>
    </html>
    


    ¯\_(ツ)_/¯

    Sunday, February 9, 2014 1:41 AM
  • Or this but it won't get you too far.

    sCommand = "mshta.exe ""javascript:{" _
                                   & "width=600;" _
                                   & "height=400;" _
                                   & "window.resizeTo(width,height);" _
                                   & "window.moveTo((screen.width-width)/2,(screen.height-height)/2);" _
                                   & "}"""
    Set sh = CreateObject("Wscript.Shell")
    sh.Run sCommand


    ¯\_(ツ)_/¯

    • Marked as answer by James Mealing Monday, February 10, 2014 3:05 PM
    Sunday, February 9, 2014 2:02 AM
  • Hi jrv, thank you for your reply.

    I'm afraid I don't quite follow your first suggestion. The code I have isn't a 'native' HTA as such, it's an HTA generated from within a VBScript thus I'm not sure where/how your example would fit in with what I've got. If it helps here is the code I have;

    Function HTA(WidthX, HeightY)
    	Dim iRandom, sCommand, oIE
    
    	randomize : iRandom = Int(1000000 * rnd)
    
    	sCommand = "mshta.exe ""javascript:{new ActiveXObject(""InternetExplorer.Application"")" _
    	& ".putProperty('" & iRandom & "',window); self.resizeTo(" & WidthX & "," & HeightY & ");" _
    		& "self.moveTo(" & ("screen.Width" & - WidthX) & "/2" & "," & ("screen.Height" & - HeightY) & "/2" & ")}"""
    
    	With CreateObject("WScript.Shell")
    		.Run sCommand, 1, False
    		Do Until .AppActivate("javascript:{new ") : Wsh.Sleep 10 : Loop
    	End With
    
    	For Each oIE In CreateObject("Shell.Application").Windows
    		If IsObject(oIE.GetProperty(iRandom)) Then
    			Set HTA = oIE.GetProperty(iRandom)
    			oIE.Quit
    			HTA.document.write "<HTA:Application border=thin sysmenu=no contextMenu=no minimizebutton=no maximizebutton=no/>" _
                 & "<body scroll=no style='font:normal 12pt Arial; background-color:whitesmoke; border-Style:inset; border-Width:2px'" _
                 & "onunload='VBScript:If Not Done.Value Then window.event.cancelBubble=true : window.event.returnValue=false : Done.Value=true : End If'>" _
    			 & "<input type=hidden id=Done value=false><center><span id=Message></span>" _
                 & "<input type=button id=buttonLock style='font:italic' onclick=Done.Value=""Lock"">&nbsp;&nbsp;&nbsp;" _
    			 & "<input type=button id=buttonLogOff style='font:italic' onclick=Done.Value=""LogOff""><center></body>"
    			HTA.buttonLogOff.Focus
    			Exit Function
    		End If
    	Next
    End Function

    I saw this example used in a previous post and have adapted it slightly for my needs.

    Why is it you say that performing the resize/move actions at the point of building the command won't work? The resize seems to work fine having it there, and the move code doesn't error as such it just doesn't do anything!


    Monday, February 10, 2014 10:53 AM
  • Or this but it won't get you too far.

    sCommand = "mshta.exe ""javascript:{" _
                                   & "width=600;" _
                                   & "height=400;" _
                                   & "window.resizeTo(width,height);" _
                                   & "window.moveTo((screen.width-width)/2,(screen.height-height)/2);" _
                                   & "}"""
    Set sh = CreateObject("Wscript.Shell")
    sh.Run sCommand


    ¯\_(ツ)_/¯

    Please look again at this


    ¯\_(ツ)_/¯

    • Marked as answer by James Mealing Monday, February 10, 2014 3:04 PM
    • Unmarked as answer by James Mealing Monday, February 10, 2014 3:05 PM
    • Marked as answer by James Mealing Monday, February 10, 2014 3:05 PM
    Monday, February 10, 2014 2:31 PM
  • Thanks jrv,

    Had completely messed up my quotation mark work around the 'window.moveTo' command somehow.

    Thanks again.

    Monday, February 10, 2014 3:07 PM
  • spot on, thank you!
    Friday, March 31, 2017 9:27 AM