'Activate and Focus HTA window using VBscripting

I have developed several HTA files where each file calls the other. There is one HTA file that i need (if called) to stay as the "Always on top" window while it is open.

I have tried "AppActivate" function thru an infinite loop using Vb script however i always find that it activates the window in the taskbar but does not make the HTA file always on top.

Searched up and down, couldn't find working way to get it working in reliable way that does not involve 3rd party applications.

My HTA file code:

<html>
<hta:application id="oHTA"
    border="none"
    caption="no"
    contextmenu="no"
    innerborder="no"
    scroll="no"
    showintaskbar="no"
/>
<script language="VBScript">
    Sub Window_OnLoad
        'Resize and position the window
        width = 133 : height = 25
        window.resizeTo width, height
        window.moveTo ((Screen.Width / 3)*2),0
    End Sub
    
    Sub CallMain ()
        Set objShell = CreateObject("WScript.Shell")
        objShell.Run("Main.hta")
        window.close 'close this HTA
    End Sub
    
</script>
<body background="Strt.png" onclick="CallMain">
</html>

Any suggestions would be appreciated.



Solution 1:[1]

Here's an example HTA with multiple pages. This should simplify the issue of focus.

<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<meta charset="UTF-8" http-equiv="X-UA-Compatible" content="IE=9">
<hta:application id=oHTA
    border=none
    caption=no
    contextmenu=no
    innerborder=no
    scroll=no
    showintaskbar=no
>
<script language="VBScript">
Set oWSH = CreateObject("Wscript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")

MyPath  = Mid(document.URL,8)
MyFolder = oFSO.GetParentFolderName(MyPath)
oWSH.CurrentDirectory = MyFolder

Sub window_onLoad
  StartPage
End Sub

Sub StartPage
  HideAllPages
  window.document.body.background = "Strt.png"
  iStartPage.style.display = "inline"
  window.resizeTo 133, 25
  window.moveTo ((screen.availWidth / 3)*2),0
End Sub

Sub MainPage
  HideAllPages
  window.document.body.background = "Main.png"
  iMainPage.style.display = "inline"
  CenterWindow 800,600
End Sub

Sub SubPage1
  HideAllPages
  window.document.body.background = "Sub1.png"
  iSubPage1.style.display = "inline"
  CenterWindow 640,480
End Sub

Sub SubPage2
  HideAllPages
  window.document.body.background = "Sub2.png"
  iSubPage1.style.display = "inline"
  CenterWindow 640,480
End Sub

Sub HideAllPages
  iStartPage.style.display = "none"
  iMainPage.style.display = "none"
  iSubPage1.style.display = "none"
  iSubPage2.style.display = "none"
End Sub

Sub CenterWindow(x,y)
    window.resizeTo x, y
    window.moveTo (screen.availWidth - x)/2, (screen.availHeight - y)/2
End Sub

</script>
<style>
body {background-size:cover; background-attachment:fixed; background-repeat:no-repeat}
.sb {background-color:transparent; background-repeat:no-repeat; border:none;
    cursor:pointer; overflow:hidden; outline:none; width:100%}
</style>
<body>
<div id=iStartPage>
  <input class=sb type=button onclick=MainPage()>
</div>
<div id=iMainPage>
  <input type=button value=Back onclick=StartPage()>
  <input type=button value=SubPage1 onclick=SubPage1()>
  <input type=button value=SubPage2 onclick=SubPage2()>
</div>
<div id=iSubPage1>
  <input type=button value=Back onclick=MainPage()>
</div>
<div id=iSubPage2>
  <input type=button value=Back onclick=MainPage()>
</div>
</body>
</html>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 LesFerch