Solved

Maintain focus on vb form or it's controls

Posted on 2002-05-14
5
238 Views
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?
0
Comment
Question by:nickwoolley
  • 3
  • 2
5 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 50 total points
ID: 7008178
Nick:

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
    'URL: http://www.allapi.net/
    'E-Mail: KPDTeam@Allapi.net
    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 = client.top
    '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
0
 

Author Comment

by:nickwoolley
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 allapi.net is not continuing to be updated: http://www.allapi.net/goodbye/
0
 
LVL 43

Expert Comment

by:TimCottee
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.
0
 

Author Comment

by:nickwoolley
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.
0
 
LVL 43

Expert Comment

by:TimCottee
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

910 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now