Maintain focus on vb form or it's controls

I open a form and want to restrict the user to activating controls on this form or related vb forms, ie I don't want the user to move focus to other application windows. Maybe show modal does this, but is there another more controllable method, like detecting when the mouse is not over a vb form?
Who is Participating?
TimCotteeHead of Software ServicesCommented:

Here is a great way of doing this. It limits the mouse to the form using the ClipCursor api call:

Private Type RECT
    left As Long
    top As Long
    right As Long
    bottom As Long
End Type
Private Type POINT
    x As Long
    y As Long
End Type
Private Declare Sub ClipCursor Lib "user32" (lpRect As Any)
Private Declare Sub GetClientRect Lib "user32" (ByVal hWnd As Long, lpRect As RECT)
Private Declare Sub ClientToScreen Lib "user32" (ByVal hWnd As Long, lpPoint As POINT)
Private Declare Sub OffsetRect Lib "user32" (lpRect As RECT, ByVal x As Long, ByVal y As Long)
Private Sub Form_Load()
    'KPD-Team 1999
    Command1.Caption = "Limit Cursor Movement"
    Command2.Caption = "Release Limit"
End Sub
Private Sub Command1_Click()
    'Limits the Cursor movement to within the form.
    Dim client As RECT
    Dim upperleft As POINT
    'Get information about our wndow
    GetClientRect Me.hWnd, client
    upperleft.x = client.left
    upperleft.y =
    'Convert window coördinates to screen coördinates
    ClientToScreen Me.hWnd, upperleft
    'move our rectangle
    OffsetRect client, upperleft.x, upperleft.y
    'limit the cursor movement
    ClipCursor client
End Sub
Private Sub Command2_Click()
    'Releases the cursor limits
    ClipCursor ByVal 0&
End Sub
Private Sub Form_Unload(Cancel As Integer)
    'Releases the cursor limits
    ClipCursor ByVal 0&
End Sub
nickwoolleyAuthor Commented:
Hi Tim, how's u?

What happens if you press a button on the form which opens another form? Ok, I shouldn't be so lazy and just test it out I guess!

Presumably I would need to detect mouse over related vb form, release cursor limits, and reset cursor limits according to second form's details.

Shame to see is not continuing to be updated:
TimCotteeHead of Software ServicesCommented:
I am fine, working hard as usual, coming up to an interesting time as my director is leaving at the end of the month so who knows what will happen when we get a replacement. (perhaps I will even be looking for a new job).

If you spawn another form then the clipping seems to be turned off automatically (I guess that windows implements this somewhere internally in the window create process). So you would be best putting the code in a module and calling it in the form_activate for each applicable window.

The only thing is that this doesn't prevent changing the focus with either alt-tab or similar methods. It just depends on your requirements I guess.

Hope all is well with you.
nickwoolleyAuthor Commented:
Works a treat, thanks Tim!

Is business bouyant with you? We are busy (Casino management software), we've got an order....yes that is news because it's the first big order in the 18 months since I've been here!! I understand that the employment agencies are pretty dead still with regards development positions.
TimCotteeHead of Software ServicesCommented:
Nick, the company is on an ever upward trend, we bought a European subsiduary last year for $50 million and just concluded a deal in Japan for the Contact Lens business of Seiko for $22 million (all out of cash), market share is growing and so on. Nevertheless I have been here for 9 years or so and despite the boss leaving am not likely to make any upward moves in the organisation so perhaps it is time to look elsewhere. Not that I really feel desperate to do so but I think I will keep an eye out for interesting stuff out there. Certainly contract positions are fairly dead at the moment but a full-time development manager or IT manager would be interesting.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.