By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,130 Members | 2,228 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,130 IT Pros & Developers. It's quick & easy.

Limiting Mouse Cursor Movement

P: n/a
Hi,

I'm trying to limit the movement of the mouse cursor in using a VB app. The
idea is to stop the cursor straying on to the second monitor of a dual
screen set-up (i.e. a projector) while this program is running.

I've found and have been working with Knowledge Base Article 179192
(http://support.microsoft.com/default...b;en-us;179192) which
limits the mouse movement to inside a form, but I'd like to adapt this to be
limited inside a rectangle on the screen which I define from pre-calculated
coordinates. I'm not managing to adapt the example code to do this. Any help
would be greatly appreciated.
Many thanks in advance,

Robin Shuff
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
A derivation of the code at
http://www.mvps.org/vbnet/code/helpers/clipcursor.htm ...

Option Explicit

'--------------------------------------------------------------
' Copyright 1996-2003 VBnet, Randy Birch, All Rights Reserved.
' Terms of use http://www.mvps.org/vbnet/terms/pages/terms.htm
'--------------------------------------------------------------

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 Function ClipCursor Lib "user32" _
(lpRect As Any) As Long

Private Declare Function GetClientRect Lib "user32" _
(ByVal hwnd As Long, _
lpRect As RECT) As Long

Private Declare Function OffsetRect Lib "user32" _
(lpRect As RECT, _
ByVal x As Long, _
ByVal y As Long) As Long

Private Declare Function GetDesktopWindow Lib "user32" () As Long

Private Sub Command1_Click()

Unload Me

End Sub
Private Sub Form_Unload(Cancel As Integer)

'Release the cursor restriction
ClipCursor ByVal 0&

End Sub
Private Sub Form_Load()

Dim rc As RECT
Dim topcorner As POINT

GetClientRect GetDesktopWindow(), rc

If OffsetRect(rc, topcorner.x, topcorner.y) <> 0 Then

Call ClipCursor(rc)
Me.Caption = "Cursor is Trapped!"

End If

End Sub
--

Randy Birch
MVP Visual Basic
http://www.mvps.org/vbnet/
Please respond only to the newsgroups so all can benefit.
"Robin Shuff" <th*********@hotmail.com> wrote in message
news:bq*********@aspen.sucs.soton.ac.uk...
: Hi,
:
: I'm trying to limit the movement of the mouse cursor in using a VB app.
The
: idea is to stop the cursor straying on to the second monitor of a dual
: screen set-up (i.e. a projector) while this program is running.
:
: I've found and have been working with Knowledge Base Article 179192
: (http://support.microsoft.com/default...b;en-us;179192) which
: limits the mouse movement to inside a form, but I'd like to adapt this to
be
: limited inside a rectangle on the screen which I define from
pre-calculated
: coordinates. I'm not managing to adapt the example code to do this. Any
help
: would be greatly appreciated.
:
:
: Many thanks in advance,
:
: Robin Shuff
:
:
Jul 17 '05 #2

P: n/a
Note ... that presumes the current screen (the screen to restrict the cursor
on) starts at 0, 0. If the app is run on the "other" screen, some
calculation of the start/end of that screen may be required. There are some
routines at http://www.mvps.org/vbnet/code/core/index.html that could
assist.

--

Randy Birch
MVP Visual Basic
http://www.mvps.org/vbnet/
Please respond only to the newsgroups so all can benefit.
"Randy Birch" <rg************@mvps.org> wrote in message
news:lI****************@news01.bloor.is.net.cable. rogers.com...
: A derivation of the code at
: http://www.mvps.org/vbnet/code/helpers/clipcursor.htm ...
:
: Option Explicit
:
: '--------------------------------------------------------------
: ' Copyright 1996-2003 VBnet, Randy Birch, All Rights Reserved.
: ' Terms of use http://www.mvps.org/vbnet/terms/pages/terms.htm
: '--------------------------------------------------------------
:
: 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 Function ClipCursor Lib "user32" _
: (lpRect As Any) As Long
:
: Private Declare Function GetClientRect Lib "user32" _
: (ByVal hwnd As Long, _
: lpRect As RECT) As Long
:
: Private Declare Function OffsetRect Lib "user32" _
: (lpRect As RECT, _
: ByVal x As Long, _
: ByVal y As Long) As Long
:
: Private Declare Function GetDesktopWindow Lib "user32" () As Long
:
: Private Sub Command1_Click()
:
: Unload Me
:
: End Sub
:
:
: Private Sub Form_Unload(Cancel As Integer)
:
: 'Release the cursor restriction
: ClipCursor ByVal 0&
:
: End Sub
:
:
: Private Sub Form_Load()
:
: Dim rc As RECT
: Dim topcorner As POINT
:
: GetClientRect GetDesktopWindow(), rc
:
: If OffsetRect(rc, topcorner.x, topcorner.y) <> 0 Then
:
: Call ClipCursor(rc)
: Me.Caption = "Cursor is Trapped!"
:
: End If
:
: End Sub
:
:
: --
:
: Randy Birch
: MVP Visual Basic
: http://www.mvps.org/vbnet/
: Please respond only to the newsgroups so all can benefit.
:
:
: "Robin Shuff" <th*********@hotmail.com> wrote in message
: news:bq*********@aspen.sucs.soton.ac.uk...
: : Hi,
: :
: : I'm trying to limit the movement of the mouse cursor in using a VB app.
: The
: : idea is to stop the cursor straying on to the second monitor of a dual
: : screen set-up (i.e. a projector) while this program is running.
: :
: : I've found and have been working with Knowledge Base Article 179192
: : (http://support.microsoft.com/default...b;en-us;179192) which
: : limits the mouse movement to inside a form, but I'd like to adapt this
to
: be
: : limited inside a rectangle on the screen which I define from
: pre-calculated
: : coordinates. I'm not managing to adapt the example code to do this. Any
: help
: : would be greatly appreciated.
: :
: :
: : Many thanks in advance,
: :
: : Robin Shuff
: :
: :
:
:
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.