Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Develop an application which doesn't lose it's focus?

Tags:

vb.net

vb6

I want to develop an app which won't allow the user to open or jump to another application while it is open. It should be in Visual Basic. For example, if my application is open (running) and the user tries to open any other windows application like "media player" then it shouldn't open. The app should not even allow "task manager" to run. The application should completely block the windows environment while it is running.

like image 282
user1349964 Avatar asked Apr 22 '12 19:04

user1349964


1 Answers

A very good question. :)

Is is possible to achieve it in VB?

The answer is Yes!

Is it Easy?

Definitely not!

However here are few tips on how to approach the problem.

1) Disable the Task Manager

Sub DisableTaskManager()
    Shell "REG add HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\System /v DisableTaskMgr /t REG_DWORD /d 1 /f", vbNormalFocus
End Sub

Sub EnableTaskManager()
    Shell "REG add HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\System /v DisableTaskMgr /t REG_DWORD /d 0 /f", vbNormalFocus
End Sub

2) Ensure your program is always on top

a) Hide the task bar

Option Explicit

'~~> http://allapi.mentalis.org/apilist/FindWindow.shtml
Private Declare Function FindWindow Lib "user32" Alias _
"FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName _
As String) As Long

'~~> http://allapi.mentalis.org/apilist/SetWindowPos.shtml
Private Declare Function SetWindowPos Lib "user32" _
(ByVal hwnd As Long, ByVal hWndInsertAfter As Long, _
ByVal x As Long, ByVal y As Long, ByVal cx As Long, _
ByVal cy As Long, ByVal wFlags As Long) As Long

Private Const SWP_HIDEWINDOW = &H80
Private Const SWP_SHOWWINDOW = &H40

'~~> Show/Hide Taskbar
Sub Sample()
    '~~> To show the taskbar
    ShowTskBar True

    '~~> To hide the taskbar
    ShowTskBar False
End Sub

Sub ShowTskBar(ShouldI As Boolean)
    Dim Sid As Long

    Sid = FindWindow("Shell_traywnd", "")

    If ShouldI = True Then
        If Sid > 0 Then _
        Sid = SetWindowPos(Sid, 0, 0, 0, 0, 0, SWP_SHOWWINDOW)
    Else
        If Sid > 0 Then _
        Sid = SetWindowPos(Sid, 0, 0, 0, 0, 0, SWP_HIDEWINDOW)
    End If
End Sub

b) Show your application Always on top

'~~> http://www.allapi.net/apilist/SetWindowPos.shtml
Private Declare Function SetWindowPos Lib "user32" _
(ByVal hwnd As Long, ByVal hWndInsertAfter As Long, _
ByVal x As Long, ByVal y As Long, ByVal cx As Long, _
ByVal cy As Long, ByVal wFlags As Long) As Long

Const HWND_TOPMOST = -1
Const HWND_NOTOPMOST = -2
Const SWP_NOSIZE = &H1
Const SWP_NOMOVE = &H2
Const SWP_NOACTIVATE = &H10
Const SWP_SHOWWINDOW = &H40

Private Sub Form_Activate()
    SetWindowPos Me.hwnd, HWND_TOPMOST, 0, 0, 0, 0, _
    SWP_NOACTIVATE Or SWP_SHOWWINDOW Or SWP_NOMOVE Or SWP_NOSIZE
End Sub

b) Show your application in maximized mode

Maximize your form so that the desktop shows only your form as it shows in a Kiosk application. Depending on the need you can also disable the minimize button or the title bar. In such a case do remember to add a button so that user can click that to exit the form.

3) Disable the Start Menu

This code depends on the Windows version that you are using. Do a search on Google, you will find plenty of examples.

Similarly, you have to take care of few small small things but this post will give you a good start. If you are looking for a complete solution in one place then I doubt you will ever get it ;)

HTH

like image 85
Siddharth Rout Avatar answered Sep 20 '22 12:09

Siddharth Rout