Created: April 24, 1995
When developing an application in Visual Basic®, you may need to perform a task that should not be interrupted. This article explains how to use the Windows® application programming interface (API) SetSysModalWindow and LockInput functions to disable task-switching.
The Windows® application programming interface (API) SetSysModalWindow function can be used to prevent a user from switching to a different Windows-based application while your program is executing. The ALT+TAB, CTRL+ESC, ALT+F4, and ALT+ESC keystroke combinations will not bring up the Task Manager or any other application—these keystrokes will simply be ignored. For a discussion of the SetSysModalWindow function, see "Additional References" below.
In addition, the LockInput function can be used to force all input to your Visual Basic® application only. No other application will receive any mouse or keyboard data. The Declare statement for the LockInput function is as follows (note that it must be typed as a single line of code):
Declare Function LockInput Lib "User" (ByVal hReserved As Integer, ByVal
hwndInput As Integer, ByVal fLock As Integer) As Integer
The LockInput function requires three arguments:
hReserved | An integer value that must be set to a value of zero. |
hwndInput | An integer value containing the window's handle. This is the window that will receive all input. |
fLock | An integer value set to TRUE (nonzero) to lock input or FALSE (zero) to unlock input. |
When your program is terminated, you must use the LockInput function to restore input to other Windows-based programs. In addition, you must destroy the system modal window; otherwise the user will not be able to switch to any other Windows-based applications and will have to reboot the computer system.
The program below shows how you can prevent a user from switching to another Windows-based application while your program is executing.
ClipControls = False
ControlBox = false
MaxButton = False
MinButton = False
Declare Function GetActiveWindow Lib "User" () As Integer
Declare Function SetFocusAPI Lib "User" Alias "SetFocus" (ByVal Hwnd As Integer)
As Integer
Declare Function SetSysModalWindow Lib "User" (ByVal Hwnd As Integer) As Integer
Declare Function LockInput Lib "User" (ByVal hReserved As Integer, ByVal
hwndInput As Integer, ByVal fLock As Integer) As Integer
Dim TopHwnd As Integer
Sub Form_Load()
Dim X As Integer
Show
DoEvents
TopHwnd = GetActiveWindow()
X = SetFocusAPI(TopHwnd)
X = SetSysModalWindow(TopHwnd)
X = LockInput(0, TopHwnd, 1)
End Sub
Sub Form_Unload(Cancel As Integer)
X = LockInput(0, TopHwnd, 0)
End Sub
Sub Command1_Click()
X = LockInput(0, TopHwnd, 0)
End
End Sub
Knowledge Base Q72674. "How to Create a System-Modal Program/Window in Visual Basic." (Development Library, Knowledge Base and Bug Lists)