'prevent user closing a HTA

Using vbscript in a HTA

Aim: To prevent a user closing the HTA (except by my custom button or by using windows taskmanager)

As i understand from various webpages; You cannot prevent the HTA from closing when the close button is clicked in the HTA SYSMENU. Therefore my solution is to prevent the user from clicking close in the first place.

First up is to disable the system menu:

<HTA:APPLICATION SYSMENU="no" />

nb. I will replace the system menu with a custom one - any links to a good guide for this would be appreciated.

Next prevent keyboard shortcuts alt+f4, f5, or escape as follows:

Function Document_onKeyDown()

    Dim alt
    alt = window.event.altKey
    Select Case window.event.keyCode
        Case 27,116
            window.event.keyCode = 0
            window.event.cancelBubble = true
            Document_onKeyDown = False
        Case 115
            If alt Then
                window.event.keyCode = 0
                window.event.cancelBubble = true
                Document_onKeyDown = False
            End If
        Case Else
            Document_onKeyDown = True
    End Select

End Function

Finally I want to prevent the user using the windows taskbar context menu for my HTA app.

How can I prevent the user from seeing a context menu by right clicking my HTA?

nb. The HTA window cannot be full screen



Solution 1:[1]

Don't mess with a user's options to close your application. It's not up to you to decide how a user should work. Instead add a cleanup procedure where you do all the "on close" tasks and call it on the beforeunload event:

<html>
<head>
<title>sample</title>
<HTA:APPLICATION ID="oHTA"
    APPLICATIONNAME="sample"
>
<script language="VBScript">
Sub cleanup
  'clean up stuff here
End Sub
</script>
</head>

<body onbeforeunload=cleanup>
...
</body>
</html>

Solution 2:[2]

With regards:

How can I prevent the user from seeing a context menu by right clicking my HTA?

<HTA:APPLICATION contextMenu="no">

Solution 3:[3]

<HTA:APPLICATION showintaskbar="no">

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 Ansgar Wiechers
Solution 2 LostInCyberSpace
Solution 3 Henry Ecker