Solved

Maintain focus on vb form or it's controls

Posted on 2002-05-14
5
235 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

747 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

14 Experts available now in Live!

Get 1:1 Help Now