Maintain focus on vb form or it's controls

Posted on 2002-05-14
Last Modified: 2010-05-02
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?
Question by:nickwoolley
  • 3
  • 2
LVL 43

Accepted Solution

TimCottee earned 50 total points
ID: 7008178

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

Author Comment

ID: 7008216
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:
LVL 43

Expert Comment

ID: 7008240
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.

Author Comment

ID: 7008991
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.
LVL 43

Expert Comment

ID: 7009303
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.

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question