HTA Always on Top
Hi all,

I've got a lovely HTA that is intended to be displayed on all our users screens at Windows startup, as defined by a GPO.  While everything is working fine, I'd really like to have it as an "always on top" window or at least grab focus/flash whenever somebody clicks away.

Every forum thread I've found via Google says this is not possible, but other people have found 'hacks' to get this to work.  Various people mention either modal/modeless dialogues or using an onblur function.  Unfortunately, as a self taught 'coder', my VBS/HTA powers are fairly weak and I don't really know how to put this into practice.

Would anybody be able to give me some guidance on how I could possibly launch the HTA and retain focus or keep it above other windows somehow?

Cheers

Andy
December 22nd, 2009 2:55pm

Hey, I know this isn't really a "solution", and more of just a "hack", but you could use the WinSetOnTop () function in AutoIT to achieve your goal, I think.

http://www.autoitscript.com/autoit3/docs/functions/WinSetOnTop.htm

Cheers,
Trevor Sullivan
Consultant
http://trevorsullivan.wordpress.com
Free Windows Admin Tool Kit Click here and download it now
December 22nd, 2009 3:53pm

Hey, I know this isn't really a "solution", and more of just a "hack", but you could use the WinSetOnTop () function in AutoIT to achieve your goal, I think.

http://www.autoitscript.com/autoit3/docs/functions/WinSetOnTop.htm

Cheers,
Trevor Sullivan
Consultant
http://trevorsullivan.wordpress.com
December 22nd, 2009 3:53pm

Thanks Trevor
I might fall back on this if nobody can suggest a way of doing it via VBS or within the HTA itself ... ?

I'm not completely against using a 3rd party tool like this, but I'd like to avoid it if possible.
Free Windows Admin Tool Kit Click here and download it now
December 22nd, 2009 4:12pm

if you haven't come across this one.. it might offer an acceptable workaround to it..
http://www.scriptinganswers.com/forum2/forum_posts.asp?TID=2300
December 23rd, 2009 7:04am

hello

you might be able to use the hta, to open another hta, that is allways on top (System modal).

look at this article by scripting guys, it describes how to open a model dialog box.

You could make this dialog box your main interface.


http://207.46.16.252/en-us/magazine/2007.05.heyscriptingguy.aspx


Merry x-mas

Free Windows Admin Tool Kit Click here and download it now
December 23rd, 2009 10:00am

Thanks for your help so far guys, but it appears that I'm fighting a losing battle.  I've figured the best way to do this, without using 3rd party tools, is to open the HTA from another VBS file and use AppActivate on a loop to nag the user.  For those at home who are wondering:

wshShell.Run "application.hta"
do
wscript.sleep 5000
wshShell.AppActivate "xx Application Title xx"
loop

With the HTA window frame removed, preventing the user from closing it prematurely (unless they figure out how to press Alt-F4) this is a simple but effective solution.

My only problem now is trying to figure out how to quit the VBS loop once the HTA has been closed.  Is there a wscript.shell function I can use to check for the existence of the HTA by title or process?

Cheers

Andy
December 23rd, 2009 2:49pm

I've used something like this on several occasions.  You can probably apply to your loop at well.  I've never used HTA before, so I'm not sure what it's executable name is in the process list, but you can easily get that in taskmanager.

set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & computerName & "\root\cimv2") 
continueRunning = true
While continueRunning
   Set colProcessList = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name = '" & executableName & "'")
   if colProcessList.count = 0 then
      continueRunning = false
   else
      Wscript.sleep 5000
   end if
wend
Free Windows Admin Tool Kit Click here and download it now
December 23rd, 2009 4:01pm

HTA's run under mshta.exe, which can cover several of our home made or out of the box apps, so maybe a bit dangerous to terminate the process itself, but I'm sure I can use something along these lines.  Ideally, I need some way of detecting the HTA by application title.  Does anyone have any ideas?

I'm going to have a dig around myself and will post what I find for any others in the same boat as me.

Cheers

Andy
December 23rd, 2009 10:27pm

Hi Andy,

win32_Process has a property "CommandLine" that includes full path to running application (MSHTA.EXE in case of HTA App) and full path to its arguments (path of your HTA that is executed).

You can use this property to find all instances of MSHTA.EXE and do some filtering to identify if any of them is running your HTA. here is a sample script that prints out command line of all running HTAs.

strHTAExecutable = "MSHTA.EXE"
set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & "." & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name = '" & strHTAExecutable & "'")
if colProcessList.count = 0 then
  'no HTA Apps found running, code to handle the condition
else
  for each objProcess in colProcessList
     wscript.echo objProcess.commandLine
     'code to find out your HTA based on its path/name from objProcess.commandLine property 
  next
end if


Hope it helps,
Free Windows Admin Tool Kit Click here and download it now
December 24th, 2009 3:44am

Thanks for your help everyone, I've finally got something useable.  Thanks to uc for cracking the final part too.

Here's my code to turn a HTA into a nag screen, if anybody needs it:

    strHTAexe = "MSHTA.EXE"

    wshShell.Run "REPLACE WITH HTA FILENAME.hta"

    set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & "." & "\root\cimv2")

    do

      wscript.sleep 5000

      Set colProcessList = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name = '" & strHTAexe & "'")

      If colProcessList.count = 0 Then
        wscript.quit
      Else

        a = 0

        For each objProcess in colProcessList
          If inStr(objProcess.commandLine, "REPLACE WITH PART OF HTA FILENAME") <> 0 Then
            a = 1
          End If
        Next

        If Not a = 1 Then
          wscript.quit
        End If

      End If

      wshShell.AppActivate "REPLACE WITH HTA APP TITLE"

    loop
Thanks again all !

Cheers

Andy
  • Marked as answer by CraigLieb Sunday, January 10, 2010 8:09 PM
December 24th, 2009 12:32pm

Thanks for your help everyone, I've finally got something useable.  Thanks to uc for cracking the final part too.

Here's my code to turn a HTA into a nag screen, if anybody needs it:

    strHTAexe = "MSHTA.EXE"

    wshShell.Run "REPLACE WITH HTA FILENAME.hta"

    set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & "." & "\root\cimv2")

    do

      wscript.sleep 5000

      Set colProcessList = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name = '" & strHTAexe & "'")

      If colProcessList.count = 0 Then
        wscript.quit
      Else

        a = 0

        For each objProcess in colProcessList
          If inStr(objProcess.commandLine, "REPLACE WITH PART OF HTA FILENAME") <> 0 Then
            a = 1
          End If
        Next

        If Not a = 1 Then
          wscript.quit
        End If

      End If

      wshShell.AppActivate "REPLACE WITH HTA APP TITLE"

    loop
Thanks again all !

Cheers

Andy
  • Marked as answer by CraigLieb Sunday, January 10, 2010 8:09 PM
Free Windows Admin Tool Kit Click here and download it now
December 24th, 2009 12:32pm

You need a Third-party application
http://www.nirsoft.net/utils/nircmd.html 32 or 64 bit version
put nircmdc.exe in your path or in the folder with hta file,
now use this sample .hta

<!========================================================================
<!
<! NAME: RusiaToday.hta
<!
<! REVISION:
<!
<! AUTHOR: Emprear / gc.gianello[dot]gmail.com
<!
<! DATE  : 15/02/2013 09:03:38
<!
<! COMMENT:
<!     Rusia Today en Espaol
<!     Canal Internacional de Noticias
<!
<!======================================================================-->
<!
<html>
<head>
<title>RusiaToday</title>
<script type="text/javascript">
window.resizeTo(470,300);
var intHorizontal = screen.width;
var intVertical =screen.height;
var left_pos = (intHorizontal - 470) / 2;
var top_pos = (intVertical - 300) / 2;
window.moveTo(left_pos,top_pos);
</script>
<HTA:APPLICATION
ID="RusiaToday"
APPLICATIONNAME="RusiaToday"
BORDER="Dialog"
BORDERSTYLE="Normal"
CAPTION="yes"
CONTEXTMENU="no"
INNERBORDER="no"
MAXIMIZEBUTTON="no"
MINIMIZEBUTTON="no"
ICON = "http://actualidad.rt.com/favicon.ico"
SCROLL="no"
SELECTION="no"
SHOWINTASKBAR="yes"
SINGLEINSTANCE="yes"
SYSMENU="yes"
WINDOWSTATE="Normal"
>
<script type="text/javascript">
function ontop(){
if ((document.getElementById('wtop').innerHTML) == 'notop') {
document.getElementById('wtop').innerHTML = 'top';
new ActiveXObject("WScript.Shell").Run('%comspec% /C nircmdc win settopmost title RusiaToday 0',0,true);
}else{
document.getElementById('wtop').innerHTML = 'notop';
new ActiveXObject("WScript.Shell").Run('%comspec% /C nircmdc win settopmost title RusiaToday 1',0,true);
}
}
</script>
<style type="text/css">
html, body{
padding: 0;
border: none;
margin: 0;
overflow: hidden;
}
object#rto{
margin: 0;
border: none: padding: 0;
;
left: 0;
top: 0
z-index: 1;
}
button#wtop{
;
top: 1%;
right: 1%;
z-index: 999;
width:38px;
font-size: 10px;
font-weight: bold;
padding: 2px 4px;
}
/*]]>*/
</style>
</head>
<body style="background:#1E1D11">
<object type="application/x-shockwave-flash" data="http://actualidad.rt.com/static/jwplayer/player.swf" id="rto" height='274' width='470'>
<param name="movie" value="http://actualidad.rt.com/static/jwplayer/player.swf" />
<param name="loop" value="false" />
<param name="menu" value="false" />
<param name="play" value="true" />
<param name="wmode" value="transparent" />
<param name='allowfullscreen' value='true'>
<param name='allowscriptaccess' value='always'>
<param name="flashvars" value="&autostart=true&backcolor=0x000000&bandwidth=1016&controlbar=over&file=RT_Spanish_2&frontcolor=0xffffff&image=http%3A%2F%2Factualidad.rt.com%2Fstatic%2Fvideo-player-bg.png&lightcolor=0xffffff&mute=false&plugins=viral-2d&screencolor=0x404040&skin=http%3A%2F%2Factualidad.rt.com%2Fstatic%2Fjwplayer%2Fplayer_skin.zip&streamer=rtmp%3A%2F%2Frt.fms.visionip.tv%2Flive%3Fautostart%3Dtrue&viral.onpause=false" />
<param name="bgcolor" value="#1E1D11" />
</object>
<button onclick="ontop();" id="wtop">top</button>
</body>
</html>

Look at the ontop() function and how alternate top and notop status using nircmdc command line utilitiy
Thats all

Regards

  • Proposed as answer by Emprear Friday, February 15, 2013 3:38 PM
February 15th, 2013 3:36pm

You need a Third-party application
http://www.nirsoft.net/utils/nircmd.html 32 or 64 bit version
put nircmdc.exe in your path or in the folder with hta file,
now use this sample .hta

<!========================================================================
<!
<! NAME: RusiaToday.hta
<!
<! REVISION:
<!
<! AUTHOR: Emprear / gc.gianello[dot]gmail.com
<!
<! DATE  : 15/02/2013 09:03:38
<!
<! COMMENT:
<!     Rusia Today en Espaol
<!     Canal Internacional de Noticias
<!
<!======================================================================-->
<!
<html>
<head>
<title>RusiaToday</title>
<script type="text/javascript">
window.resizeTo(470,300);
var intHorizontal = screen.width;
var intVertical =screen.height;
var left_pos = (intHorizontal - 470) / 2;
var top_pos = (intVertical - 300) / 2;
window.moveTo(left_pos,top_pos);
</script>
<HTA:APPLICATION
ID="RusiaToday"
APPLICATIONNAME="RusiaToday"
BORDER="Dialog"
BORDERSTYLE="Normal"
CAPTION="yes"
CONTEXTMENU="no"
INNERBORDER="no"
MAXIMIZEBUTTON="no"
MINIMIZEBUTTON="no"
ICON = "http://actualidad.rt.com/favicon.ico"
SCROLL="no"
SELECTION="no"
SHOWINTASKBAR="yes"
SINGLEINSTANCE="yes"
SYSMENU="yes"
WINDOWSTATE="Normal"
>
<script type="text/javascript">
function ontop(){
if ((document.getElementById('wtop').innerHTML) == 'notop') {
document.getElementById('wtop').innerHTML = 'top';
new ActiveXObject("WScript.Shell").Run('%comspec% /C nircmdc win settopmost title RusiaToday 0',0,true);
}else{
document.getElementById('wtop').innerHTML = 'notop';
new ActiveXObject("WScript.Shell").Run('%comspec% /C nircmdc win settopmost title RusiaToday 1',0,true);
}
}
</script>
<style type="text/css">
html, body{
padding: 0;
border: none;
margin: 0;
overflow: hidden;
}
object#rto{
margin: 0;
border: none: padding: 0;
;
left: 0;
top: 0
z-index: 1;
}
button#wtop{
;
top: 1%;
right: 1%;
z-index: 999;
width:38px;
font-size: 10px;
font-weight: bold;
padding: 2px 4px;
}
/*]]>*/
</style>
</head>
<body style="background:#1E1D11">
<object type="application/x-shockwave-flash" data="http://actualidad.rt.com/static/jwplayer/player.swf" id="rto" height='274' width='470'>
<param name="movie" value="http://actualidad.rt.com/static/jwplayer/player.swf" />
<param name="loop" value="false" />
<param name="menu" value="false" />
<param name="play" value="true" />
<param name="wmode" value="transparent" />
<param name='allowfullscreen' value='true'>
<param name='allowscriptaccess' value='always'>
<param name="flashvars" value="&autostart=true&backcolor=0x000000&bandwidth=1016&controlbar=over&file=RT_Spanish_2&frontcolor=0xffffff&image=http%3A%2F%2Factualidad.rt.com%2Fstatic%2Fvideo-player-bg.png&lightcolor=0xffffff&mute=false&plugins=viral-2d&screencolor=0x404040&skin=http%3A%2F%2Factualidad.rt.com%2Fstatic%2Fjwplayer%2Fplayer_skin.zip&streamer=rtmp%3A%2F%2Frt.fms.visionip.tv%2Flive%3Fautostart%3Dtrue&viral.onpause=false" />
<param name="bgcolor" value="#1E1D11" />
</object>
<button onclick="ontop();" id="wtop">top</button>
</body>
</html>

Look at the ontop() function and how alternate top and notop status using nircmdc command line utilitiy
Thats all

Regards

  • Proposed as answer by Emprear Friday, February 15, 2013 3:38 PM
Free Windows Admin Tool Kit Click here and download it now
February 15th, 2013 3:36pm

Hi SpankyJ,

There's actually an easier solution: use the setInterval method and let mshta bring the window in focus every n ms. Set the HTA's properties to remove the sysmenu, scroll bars, etc and you're done!

<script language="vbscript">

window.setInterval "setfocus()", 100

Function setfocus

 window.focus()

End Function
</script>
<hta:application
 applicationname="MyHTA" 
 border="none"
 borderstyle="complex"
 caption="My HTML Application"
 contextmenu="no"
 icon="myicon.ico"
 innerborder="yes"
 maximizebutton="no"
 minimizebutton="no"
 navigable="no"
 scroll="no"
 selection="no"
 showintaskbar="no"
 singleinstance="yes"
 sysmenu="no"
 version="1.0"
 windowstate="normal"
>


April 1st, 2013 4:21pm

Hi SpankyJ,

There's actually an easier solution: use the setInterval method and let mshta bring the window in focus every n ms. Set the HTA's properties to remove the sysmenu, scroll bars, etc and you're done!

<script language="vbscript">

window.setInterval "setfocus()", 100

Function setfocus

 window.focus()

End Function
</script>
<hta:application
 applicationname="MyHTA" 
 border="none"
 borderstyle="complex"
 caption="My HTML Application"
 contextmenu="no"
 icon="myicon.ico"
 innerborder="yes"
 maximizebutton="no"
 minimizebutton="no"
 navigable="no"
 scroll="no"
 selection="no"
 showintaskbar="no"
 singleinstance="yes"
 sysmenu="no"
 version="1.0"
 windowstate="normal"
>


Free Windows Admin Tool Kit Click here and download it now
April 1st, 2013 4:21pm

Works like a charm, simple is normally better...

Thanks


  • Edited by RMaldi Thursday, August 28, 2014 12:52 AM
August 28th, 2014 12:51am

Works like a charm, simple is normally better...

Thanks


  • Edited by RMaldi Thursday, August 28, 2014 12:52 AM
Free Windows Admin Tool Kit Click here and download it now
August 28th, 2014 12:51am

Not sure why but so long as I try using vbscript my code will not work.

Have switched over to javascript and the hta recognizes it fine now. Anyone else running into that problem I suggest switching over to javascript; more functionality anyways.

October 10th, 2014 5:46pm


<script language="vbscript">

window.setInterval "setfocus()", 100

Function setfocus

 window.focus()

End Function

Works fine for me!!! Thanks!
Free Windows Admin Tool Kit Click here and download it now
April 16th, 2015 7:54am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics