Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

A new HTA based message box

821 views
Skip to first unread message

Tom Lavedas

unread,
Jun 25, 2010, 12:01:32 PM6/25/10
to
About ten days ago I adapted a Mayayana solution to create a
"chromeless" msgbox replacement. The thread can be found here:
http://groups.google.com/group/microsoft.public.scripting.vbscript/browse_frm/thread/5ab6ebda92e63a9c#.

At the time I figured the only thing objectionable about the approach
was that it flashed a large HTA window before it could be adjusted to
the desired size. I asked if anyone might have a solution. Having
received no response, I kept thinking about that problem. I was
pretty sure a fix could be coded into the JavaScript used to create
the HTA in the first place, but couldn't get it to work. Today I had
a 'eureka' moment and after some fiddling got it worked out.

Hear is an example that presents a request for credentials and
provides a simple UID/Password dialog box as an example of its use
(watch for wordwrap) ...

' A simple example of HTABox
-----------' Main -------------
with HTABox("lightgrey", 150, 300, 400, 500)
.document.title = "Credentials"
.msg.innerHTML = "UserID: &nbsp; &nbsp; <input type=text size=20
id=UID>"_
& "<br>Password: <input type=password id=PW
size=22><p>" _
& "<input type=submit value=Submit
onclick='done.value=true'>"
.UID.value = createobject("wscript.network").username
.PW.focus
do until .done.value : wsh.sleep 50 : loop
sUID = .UID.value : sPW = .PW.value
.close
end with

wsh.echo "UID:", sUID, "Password:", sPW
'--------- Main Ends ------------

' Author: Tom Lavedas, June 2010
Function HTABox(sBgColor, h, w, l, t)
Dim IE, HTA

randomize : nRnd = Int(1000000 * rnd)
sCmd = "mshta.exe ""javascript:" _
& "{with (new ActiveXObject(""InternetExplorer.Application""))
{" _
& "PutProperty('" & nRnd & "',window);" _
& "with (GetProperty('" & nRnd & "')){" _
& "resizeTo(" & w & "," & h & ");moveTo(" & l & "," & t &
")}}}"""

with CreateObject("WScript.Shell")
.Run sCmd, 1, False
do until .AppActivate("javascript:{with ") : WSH.sleep 10 : loop
end with ' WSHShell

For Each IE In CreateObject("Shell.Application").windows
If IsObject(IE.GetProperty(nRnd)) Then
set HTABox = IE.GetProperty(nRnd)
HTABox.document.title = "HTABox"
HTABox.document.write _
"<HTA:Application contextMenu=no border=thin " _
& "minimizebutton=no maximizebutton=no sysmenu=no />" _
& "<body scroll=no style='background-color:" _
& sBgColor & ";font:normal 10pt Arial' " _
& "onbeforeunload='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=msg>&nbsp;</span><center></body>"
Exit Function
End If
Next

' I can't imagine how this line can be reached, but just in case
MsgBox "HTA window not found." : wsh.quit

End Function
' ---------- code ends -------------

I chose to make the dialog box as simple as possible, purposely devoid
of 'bells and whistles'. It merely returns a handle to the HTA window
that is created. It does provided some fundamental features such as
the setting of background color, height, width, left position and top
position. The actual controls for the object are left to the user to
define through the 'msg' object that the routine creates. A user's
intention to continue is signaled via the hidden 'done' control, by
setting its value to True. However, a script is free to close the
window at any time by issuing a 'close' command. The script example
provides some illustrative code and logic.

Enjoy.
_____________________
Tom Lavedas

James Whitlow

unread,
Jun 25, 2010, 1:49:00 PM6/25/10
to
"Tom Lavedas" <tglb...@verizon.net> wrote in message
news:b9622e6d-0213-4d67...@k39g2000yqb.googlegroups.com...

> About ten days ago I adapted a Mayayana solution to create a
> "chromeless" msgbox replacement. The thread can be found here:
> http://groups.google.com/group/microsoft.public.scripting.vbscript/browse_frm/thread/5ab6ebda92e63a9c#.
>
> At the time I figured the only thing objectionable about the approach
> was that it flashed a large HTA window before it could be adjusted to
> the desired size. I asked if anyone might have a solution. Having
> received no response, I kept thinking about that problem. I was
> pretty sure a fix could be coded into the JavaScript used to create
> the HTA in the first place, but couldn't get it to work. Today I had
> a 'eureka' moment and after some fiddling got it worked out.

Nice piece of work, Tom!

I don't really have a need for the message box, per se, but I will likely
be borrowing your technique for some other projects. Having an HTA launched
from a WSF really opens the door to being able to have pseudo multi-threaded
HTA. The entire body of the HTA can be neatly packaged inside of a
<resource> in the WSF.


Mayayana

unread,
Jun 25, 2010, 6:27:08 PM6/25/10
to
That works nicely for me. I tried to adjust it
to get a more msgbox-like border, but that
doesn't seem to work. borderstyle=static seems
to be the only thing that has any effect at all,
and that produces a somewhat odd border.

Todd Vargo

unread,
Jun 25, 2010, 10:27:10 PM6/25/10
to

James Whitlow wrote:
> "Tom Lavedas" <tglb...@verizon.net> wrote in message
> news:b9622e6d-0213-4d67...@k39g2000yqb.googlegroups.com...
>> About ten days ago I adapted a Mayayana solution to create a
>> "chromeless" msgbox replacement. The thread can be found here:
>>
snip...

>>
>> At the time I figured the only thing objectionable about the approach
>> was that it flashed a large HTA window before it could be adjusted to
>> the desired size. I asked if anyone might have a solution. Having
>> received no response, I kept thinking about that problem. I was
>> pretty sure a fix could be coded into the JavaScript used to create
>> the HTA in the first place, but couldn't get it to work. Today I had
>> a 'eureka' moment and after some fiddling got it worked out.
>
> Nice piece of work, Tom!
>
> I don't really have a need for the message box, per se, but I will likely
> be borrowing your technique for some other projects. Having an HTA
> launched from a WSF really opens the door to being able to have pseudo
> multi-threaded HTA. The entire body of the HTA can be neatly packaged
> inside of a <resource> in the WSF.

Yes, nice work Tom. The only thing that bothers me is when run using the
WSCRIPT.EXE host, after the HTA closes and control returns to the script,
the window focus does not return to the running script. In otherwords, any
MsgBox thereafter will be opened as minimized. The only thing that seems to
display the MsgBox in the foreground following the HTA is if the
vbSystemModal flag is used.

--
Todd Vargo

(Post questions to group only. Remove "z" to email personal messages)

Csaba Gabor

unread,
Jun 26, 2010, 6:42:36 AM6/26/10
to
Thanks for a very interesting post/threads highlighting some
of the distinctions between HTAs and simple IE.

If I understand correctly, the philosophy of the HTA is,
"Gee, this IE was initiated by VBScript, therefore
perhaps its not unreasonable to give the script proper
access." So why isn't the same philosophy followed
when VBScript initiates an IE? (that question is
meant to be rhetorical except for Microsoft system
designers/architects). There are, after all, now two
ways to gain this end - one by the means shown in the
post and the other by creating (or copying onto) a
.HTA, no? And doesn't that effectively undo all the
security introduced by such distinction?

Csaba Gabor from Vienna


On Jun 25, 6:01 pm, Tom Lavedas <tglba...@verizon.net> wrote:
> About ten days ago I adapted a Mayayana solution to create a

> "chromeless" msgbox replacement. The thread can be found here:http://groups.google.com/group/microsoft.public.scripting.vbscript/br....

Mayayana

unread,
Jun 26, 2010, 9:01:07 AM6/26/10
to
|
| If I understand correctly, the philosophy of the HTA is,
| "Gee, this IE was initiated by VBScript, therefore
| perhaps its not unreasonable to give the script proper
| access." So why isn't the same philosophy followed
| when VBScript initiates an IE? (that question is
| meant to be rhetorical except for Microsoft system
| designers/architects). There are, after all, now two
| ways to gain this end - one by the means shown in the
| post and the other by creating (or copying onto) a
| .HTA, no? And doesn't that effectively undo all the
| security introduced by such distinction?
|
I'm wondering about that, too. HTAs were introduced
as a way to continue using IE/script utilities in the face
of increasing IE security. In Win98 I blocked HTAs, never
used IE online, and used .html files for scripted utilities.
HTAs seemed like an unnecessary security risk.

But post-Win9x, an HTA is really the only way to do
much of anything in IE without being blocked by security
restrictions. I still wouldn't take IE online. I now think
of it as an HTA-based scripting GUI. But the methods
we're using to get file open dialogs and custom message
boxes really shouldn't be possible from the point of view
of Microsoft's IE security approach.

I wouldn't be surprised if MS "fixes" that functionality,
the same way they've blocked the production of custom
message boxes, offscreen IE instances, etc.


Dave "Crash" Dummy

unread,
Jun 26, 2010, 11:37:31 AM6/26/10
to

I used to get around a lot of those IE security restrictions by making
Zone 0 visible then unblocking everything, but I haven't figured out how
to do that in Windows 7, so I'm using HTA, instead. I've had to rewrite
a lot of homebuilt utilities, including my browser home page.
--
Crash

"Patriotism is the last refuge of a scoundrel."
~ Samuel Johnson ~

Mayayana

unread,
Jun 26, 2010, 12:06:14 PM6/26/10
to
| I used to get around a lot of those IE security restrictions by making
| Zone 0 visible then unblocking everything, but I haven't figured out how
| to do that in Windows 7, so I'm using HTA, instead. I've had to rewrite
| a lot of homebuilt utilities, including my browser home page.

It keeps getting harder. IE security has become
a mess piled onto a mess piled onto a mess. I wrote
a script to demonstrate the *8* Registry values
currently involved with IE settings:

http://www.jsware.net/jsware/zips/ielocal.zip

Linked from this info. page:

http://www.jsware.net/jsware/iewacky.php5

I don't think I've actually tried the script in Win7.
Like you, I only use HTAs now. I got tired of figuring
out all of those convoluted "secret" settings. IE is
really designed to be controlled by sys. admins in
corporations, used by employees on intranets.


Al Dunbar

unread,
Jun 26, 2010, 6:48:53 PM6/26/10
to

"Csaba Gabor" <dan...@gmail.com> wrote in message
news:766088c9-f311-459a...@t10g2000yqg.googlegroups.com...


> Thanks for a very interesting post/threads highlighting some
> of the distinctions between HTAs and simple IE.
>
> If I understand correctly,

you don't...

> the philosophy of the HTA is,
> "Gee, this IE was initiated by VBScript, therefore
> perhaps its not unreasonable to give the script proper
> access."

As I understand it, the HTA was created as a way to develop applications
having the same level of security restrictions as .exe's (i.e. none). There
is no distinction about how the HTA (or HTML web page) is started and
whether or not it is being manipulated by script not contained in the HTA
itself.

> So why isn't the same philosophy followed
> when VBScript initiates an IE? (that question is
> meant to be rhetorical except for Microsoft system
> designers/architects). There are, after all, now two
> ways to gain this end - one by the means shown in the
> post and the other by creating (or copying onto) a
> .HTA, no? And doesn't that effectively undo all the
> security introduced by such distinction?

The idea is that an application you develop (or purchase) is generally more
trustworthy than whatever web site your browser might take you to.

/Al

Pegasus [MVP]

unread,
Jun 29, 2010, 4:23:53 PM6/29/10
to

"Tom Lavedas" <tglb...@verizon.net> wrote in message
news:b9622e6d-0213-4d67...@k39g2000yqb.googlegroups.com...

This is great stuff and I have been looking for something like this for
quite some time. It is fairly easy to adapt the code for different
applications even for someone like me who knows nothing at all about HTA and
Java. However, I do have a problem with an application that requires a
banner panel across the top of the screen. Just an informative message, no
OK button. It runs in parallel with some independent process. To close the
panel I replace this line

do until .done.value : wsh.sleep 50 : loop

with something like this:

do until oFSO.FileExists(sSemaphore) : loop

where the semaphore file is created by the parallel process. This works but
here is a snag: The moment the program drops out of the Do loop, IE
generates a dialog box asking if I really want to close the current window.
How can I prevent this box from popping up?

Tom Lavedas

unread,
Jun 29, 2010, 4:51:02 PM6/29/10
to
On Jun 29, 4:23 pm, "Pegasus [MVP]" <n...@microsoft.com> wrote:
> "Tom Lavedas" <tglba...@verizon.net> wrote in message

>
> news:b9622e6d-0213-4d67...@k39g2000yqb.googlegroups.com...
>
>
>
> > About ten days ago I adapted a Mayayana solution to create a
> > "chromeless" msgbox replacement.  The thread can be found here:
> >http://groups.google.com/group/microsoft.public.scripting.vbscript/br....

Immediately before your script issues the Close, set the value of
'done' equal to True, as in ...

with HTABox (...)
do until oFSO.FileExists(sSemaphore) : loop
.done.value = true
.close
end with

Obviously, I didn't test that ;-}
_____________________
Tom Lavedas

Pegasus [MVP]

unread,
Jun 29, 2010, 5:30:53 PM6/29/10
to
>
> Immediately before your script issues the Close, set the value of
> 'done' equal to True, as in ...
>
> with HTABox (...)
> do until oFSO.FileExists(sSemaphore) : loop
> .done.value = true
> .close
> end with
>
> Obviously, I didn't test that ;-}
> _____________________
> Tom Lavedas

I just tested it myself - works like a charm. Thanks for the quick response!

Tom Lavedas

unread,
Jun 29, 2010, 10:34:41 PM6/29/10
to

Glad I could help.

BTW, I tweaked the code a bit (I figured out that the JavaScript part
could be simplified):

with HTABox("lightgrey", 150, 300, 400, 500)
.document.title = "Credentials"
.msg.innerHTML = "UserID: &nbsp; &nbsp; <input type=text size=20
id=UID>"_
& "<br>Password: <input type=password id=PW
size=22><p>" _
& "<input type=submit value=Submit
onclick='done.value=true'>"
.UID.value = createobject("wscript.network").username
.PW.focus
do until .done.value : wsh.sleep 50 : loop
sUID = .UID.value : sPW = .PW.value

.done.value = true ' Only needed if box to be closed before return
.close
end with

wsh.echo "UID:", sUID, "Password:", sPW

Function HTABox(sBgColor, h, w, l, t)
Dim IE, HTA

randomize : nRnd = Int(1000000 * rnd)

sCmd = "mshta.exe ""javascript:{new " _
& "ActiveXObject(""InternetExplorer.Application"")" _
& ".PutProperty('" & nRnd & "',window);" _
& "window.resizeTo(" & w & "," & h & ");" _
& "window.moveTo(" & l & "," & t & ")}"""

with CreateObject("WScript.Shell")
.Run sCmd, 1, False

do until .AppActivate("javascript:{new ") : WSH.sleep 10 : loop
end with ' WSHShell

For Each IE In CreateObject("Shell.Application").windows
If IsObject(IE.GetProperty(nRnd)) Then
set HTABox = IE.GetProperty(nRnd)
HTABox.document.title = "HTABox"
HTABox.document.write _
"<HTA:Application contextMenu=no border=thin " _
& "minimizebutton=no maximizebutton=no sysmenu=no />" _
& "<body scroll=no style='background-color:" _
& sBgColor & ";font:normal 10pt Arial;" _

& "border-Style:outset;border-Width:3px'" _


& "onbeforeunload='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=msg>&nbsp;</span><center></body>"
Exit Function
End If
Next

' I can't imagine how this line can be reached, but just in case
MsgBox "HTA window not found."
wsh.quit

End Function
__________________
Tom Lavedas

Tom Lavedas

unread,
Jun 30, 2010, 8:32:06 AM6/30/10
to
On Jun 25, 10:27 pm, "Todd Vargo" <tlva...@sbcglobal.netz> wrote:
> James Whitlow wrote:
> > "Tom Lavedas" <tglba...@verizon.net> wrote in message

You could just reuse the same HTABox to display your results,
something like this ...

with HTABox("lightgrey", 150, 300, 400, 500)
.document.title = "Credentials"

.msg.innerHTML = "UserID: &nbsp; &nbsp; " _
& "<input type=text size=20 id=UID>"_


& "<br>Password: <input type=password id=PW size=22><p>" _
& "<input type=submit value=Submit onclick='done.value=true'>"
.UID.value = createobject("wscript.network").username
.PW.focus
do until .done.value : wsh.sleep 50 : loop
sUID = .UID.value : sPW = .PW.value

' Show the results by reusing the HTABox
' Optionally change box style parameters
.resizeto 250, 125 : .moveto 500, 400
.document.body.style.borderwidth = "1px"
.document.title = "Results"
.msg.innerHTML = "UID: " & sUID & " Password: " & sPW & "<p>" _
& "<input type=submit id=ok value=OK onclick='done.value=true'>"
.done.value = false ' reset exit control to wait
timeout = 3000 ' set an automatic close timeout
.OK.focus
do until .done.value or n > TimeOut: wsh.sleep 50 : n=n+50 : loop
.done.value = true ' suppress IE close message
.close
end with
_____________________
Tom Lavedas

\Rems

unread,
Jun 30, 2010, 11:11:59 AM6/30/10
to
On 26 jun, 04:27, "Todd Vargo" <tlva...@sbcglobal.netz> wrote:

> >  Nice piece of work, Tom!
>

> >  I don't really have a need for themessagebox, per se, but I will likely


> > be borrowing your technique for some other projects. Having anHTA
> > launched from a WSF really opens the door to being able to have pseudo

> > multi-threadedHTA. The entire body of theHTAcan be neatly packaged


> > inside of a <resource> in the WSF.
>
> Yes, nice work Tom. The only thing that bothers me is when run using the

> WSCRIPT.EXE host, after theHTAcloses and control returns to the script,


> the window focus does not return to the running script. In otherwords, any
> MsgBox thereafter will be opened as minimized. The only thing that seems to

> display the MsgBox in the foreground following theHTAis if the


> vbSystemModal flag is used.
>
> --
> Todd Vargo
>

> (Post questions to group only. Remove "z" to email personal messages)- Tekst uit oorspronkelijk bericht niet weergeven -
>
> - Tekst uit oorspronkelijk bericht weergeven -


There is a somewhat ugly workaround to get the focus back to
wscript.exe => .sendkeys "^"

Awesome piece of work Tom!

I noticed when I test the script that iexplore.exe processes where not
quit after the script is ended.
Therefore I added IE.quit to the script.

I also perform a resize of the box twice - the first time there is
just a minimal box centered on the screen.
And after the color of the body has been set, the script resize the
box to its final size.
Additionally the intWindowStyle for CreateObject("WScript.Shell").RUN
have I set to 2

A sample is below (sorry for the other changes, these are personal),

' A simple example of HTABox --------------------------------

Dim IE
sWidth = 300
sHeight = 210

with HTABox(sHeight, sWidth)
.document.title = String(18, chr(160)) & "Credentials"
.msg.innerHTML = "UserID: &nbsp; &nbsp; <input type=text " _
& "size=20 id=UID><br>Password: <input "_
& "type=password id=PW size=22><p><input " _
& "type=submit value=Submit onclick=" _
& "'done.value=true'>"


.UID.value = createobject("wscript.network").username
.PW.focus
do until .done.value : wsh.sleep 50 : loop
sUID = .UID.value : sPW = .PW.value
.close
end with

IE.quit : CreateObject("WScript.Shell").SendKeys "^"


wsh.echo "UID:", sUID, "Password:", sPW

'--------- Main Ends ------------


' Author: Tom Lavedas, June 2010

Function HTABox(h, w)
Dim l, t
With createobject("internetexplorer.application")
l = .width
t = .height
.quit
End with

Dim IHTA
nRnd = hex(Timer*100)

sCmd = "mshta.exe ""javascript:{new " _
& "ActiveXObject(""InternetExplorer.Application"")" _

& ".PutProperty('" & nRnd & "',window);" _
& "window.resizeTo();window.moveTo(" & l/2 & "," & t/2 & ")}"""

with CreateObject("WScript.Shell")
.Run sCmd, 2, False
do until .AppActivate("javascript:{new ") : WSH.sleep 10 : loop
end with ' WSHShell

For Each IE In CreateObject("Shell.Application").windows
If IsObject(IE.GetProperty(nRnd)) Then
set HTABox = IE.GetProperty(nRnd)
HTABox.document.title = "HTABox"
HTABox.document.write _
"<HTA:Application contextMenu=no border=thin " _
& "minimizebutton=no maximizebutton=no sysmenu=no />" _

& "<body scroll=no style=filter:progid:DXImageTrans" _
& "form.Microsoft.Gradient(GradientType=0,StartColor" _
& "Str='#86cceb',endColorStr='#5589ab');font:normal " _
& "10pt Arial' onbeforeunload='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=msg>&nbsp;</span><center></body>"

l = (l - w) /2
t = (t - h) /2
HTABox.moveTo l,t
HTABox.resizeTo w,h


Exit Function
End If
Next

' I can't imagine how this line can be reached, but just in case
MsgBox "HTA window not found." : wsh.quit

End Function
' ---------- code ends -------------

\Rems

Tom Lavedas

unread,
Jun 30, 2010, 11:39:02 AM6/30/10
to
> {snip}

> ' ---------- code ends -------------
>
> \Rems

OK, I think the problem is that I didn't DIM IE in the subroutine, as
I should have. If that variable is made local it will release the
instance of IE upon exit of the function.

' A simple example of HTABox --------------------------------

'Dim IE
sWidth = 300
sHeight = 210

with HTABox(sHeight, sWidth)
.document.title = String(18, chr(160)) & "Credentials"
.msg.innerHTML = "UserID: &nbsp; &nbsp; <input type=text " _
& "size=20 id=UID><br>Password: <input "_
& "type=password id=PW size=22><p><input " _
& "type=submit value=Submit onclick=" _
& "'done.value=true'>"
.UID.value = createobject("wscript.network").username
.PW.focus
do until .done.value : wsh.sleep 50 : loop
sUID = .UID.value : sPW = .PW.value
.close
end with

CreateObject("WScript.Shell").SendKeys "^"

wsh.echo "UID:", sUID, "Password:", sPW

'--------- Main Ends ------------

' Author: Tom Lavedas, June 2010
Function HTABox(h, w)

Dim l, t, IE, sCmd

If that doesn't work for some reason (it worked on my XPSP3 equipped
system), you can add an IE.QUIT right after the GetProperty line to
release IE. It's not needed after that point.
_____________________
Tom Lavedas

\Rems

unread,
Jul 1, 2010, 4:34:02 AM7/1/10
to
> > I also perform a resize of theboxtwice - the first time there is
> > just a minimalboxcentered on the screen.

> > And after the color of the body has been set, the script resize the
> >boxto its final size.
>   MsgBox "HTAwindow not found." :  wsh.quit

>
> End Function
> ' ---------- code ends -------------
>
> If that doesn't work for some reason (it worked on my XPSP3 equipped
> system), you can add an IE.QUIT right after the GetProperty line to
> release IE.  It's not needed after that point.
> _____________________
> Tom Lavedas- Tekst uit oorspronkelijk bericht niet weergeven -

>
> - Tekst uit oorspronkelijk bericht weergeven -


Thanks Tom.
Actually you did DIM'd IE in the subroutine but iexpore.exe didn't
close on my computer (I use WinXPSP3 IE8).
I added IE.QUIT right after the GetProperty line now, that works very
well for me.

thanks,
\Rems

0 new messages