CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2001
    Location
    County Durham, England
    Posts
    238

    Locking User in Application

    I'm developing an Examination Program and need to lock the user in the program, I have disabled the CTRL-ALT-Del no problem. but say for example my App window is 800x600 and the screen resolution in 1024x768. Users can still activate Icons on the desktop and also the Taskbar.

    Does anybody know of a way of confining a user solely to my application ?

    many thanks in advance . .


  2. #2
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Locking User in Application

    Do not know direct answer, but you can modify your app screen size in load event getting the size of screen (it should be expressed in pixel, not in twips, so you will have to make some conversions). Now I cannot test, but try with screen.height and screen.width, or look for a window Api (sorry, do not remember which one). You should also look for an api which will take your form always on top...I know it is not much, but I hope it helped you

    Special thanks to Lothar "the Great" Haensler. Come back soon, you Guru.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  3. #3
    Join Date
    Mar 2001
    Location
    County Durham, England
    Posts
    238

    Re: Locking User in Application

    Cheers Cesare

    I have already got the Form Always on top but I Didn't want to size my form to the Screen size as the Applications I have really needs to be a fixed size, thanx anyway though . .


  4. #4
    Join Date
    Dec 1999
    Location
    Dublin, Ireland
    Posts
    1,173

    Re: Locking User in Application

    There is a way, but it involves subclassing.

    1st you need to download the EventVB subclassing control from http://www.merrioncomputing.com/Download/EventVB.dll and then add a reference to it in your visual basic project.

    Then in your main form's declarations:

    private withevents ApiLink as EventVB.APIFunctions
    private withevents frmThis as ApiWindow

    private Declare Function SetForegroundWindow Lib "user32" Alias "SetForegroundWindow" (byval hwnd as Long) as Long
    private Declare Function SetFocus Lib "user32" Alias "SetFocus" (byval hwnd as Long) as Long





    And start up the subclasser in your form's Load event:

    private Sub Form_Load()

    '\\ Link the api dispenser
    set ApiLink = new EventVB.APIFunctions
    '\\ Link the form
    set frmThis = new ApiWindow
    frmThis.hwnd = me.hwnd

    '\\ Start the subclassing
    ApiLink.SubclassedWindows.Add frmThis

    End Sub




    You must unsubclass the window in the unload event:

    public Sub Form_Terminate()

    ApiLink.SubclassedWindows.Remove frmThis.hWnd
    set frmThis = nothing
    set ApiLink = nothing


    End Sub




    Now whenever your application loses the focus it will recieve a ActiveApplicationChanged message.
    You need to regain control in this message.

    private Sub frmThis_ActiveApplicationChanged(byval ActivatingThisApp as Boolean, byval hThread as Long, Cancel as Boolean)

    If Not ActivatingThisApp then
    Call SetForegroundWindow(me.hwnd)
    End If

    End Sub




    HTH,
    Duncan

    -------------------------------------------------
    Ex. Datis: Duncan Jones
    Merrion Computing Ltd
    http://www.merrioncomputing.com
    '--8<-----------------------------------------
    NEW -The printer usage monitoring application
    '--8<------------------------------------------

  5. #5
    Join Date
    Feb 2000
    Location
    Ireland
    Posts
    808

    Re: Locking User in Application

    You could use the clipcursor API to restrict the mouse pointer to your form

    private Declare Function ClipCursor Lib "user32" (lpRect as Any) as Long
    private Declare Function GetWindowRect Lib "user32" (byval hwnd as Long, lpRect as RECT) as Long
    private Declare Function GetDesktopWindow Lib "user32" () as Long
    private Type RECT
    left as Long
    top as Long
    right as Long
    bottom as Long
    End Type
    private Sub Clip()
    Dim myrect as RECT
    Call GetWindowRect(Form1.hwnd, myrect)
    Call ClipCursor(myrect)
    End Sub
    private Sub Release()
    Dim myhwnd as Long
    Dim myrect as RECT
    myhwnd = GetDesktopWindow()
    Call GetWindowRect(myhwnd, myrect)
    Call ClipCursor(myrect)
    End Sub
    private Sub Form_Load()
    Clip
    End Sub

    private Sub Form_Unload(Cancel as Integer)
    Release
    End Sub





  6. #6
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: Locking User in Application

    You could try using the following two APIs to control where the mouse is. These API gain control and track where the mouse is. The following program needs a commandBox on a standard form.
    In conjunction with this simple program, there are ways to position the cursor so that if the user moves it outside your application you can move it back. I however do not have a sample of how to do this piece, but here is the Mouse Capture piece. Scrounge on Planet-Source-Code.com/vb,or your favorite Code haunt, for the mouse move part.

    option Explicit

    private Declare Function SetCapture Lib "user32" (byval hwnd as Long) as Long
    private Declare Function ReleaseCapture Lib "user32" () as Long


    'Code:
    'Put this code in MouseMove event. In this example, I put a
    'CommandButton on a
    'form with the name Command1

    private Sub Command1_MouseMove(Button as Integer, Shift as Integer, X _
    as Single, Y as Single)
    static CtrMov as Boolean
    static Counter as Long
    Counter = Counter + 1
    With Command1 'Change this 'Command1' to your control name
    If (X &lt; 0) Or (Y &lt; 0) Or (X &gt; .Width) Or (Y &gt; .Height) then
    ReleaseCapture
    CtrMov = false
    Command1.BackColor = &HFF&
    Command1.Caption = " Not Over " & Counter
    Command1.Refresh
    'Put here your code to LostMouseFocus
    'for example:
    me.print "LostMouseFocus"

    else
    SetCapture .hwnd
    If CtrMov = false then ' do this only once per "over"
    CtrMov = true
    Command1.BackColor = &HFFFFFF
    Command1.Caption = " Over " & Counter
    Command1.Refresh
    'Put here your code to GetMouseFocus
    'for example:
    me.print "GetMouseFocus"

    End If
    End If
    End With
    End Sub






    John G

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured