Go Back   Forum Care Forums > Development Reference Area > Visual Basic

Reply
 
LinkBack Thread Tools Display Modes
Limiting Mouse Cursor Movement
Old
  (#1)
Robin Shuff
Guest
 
Posts: n/a
Default Limiting Mouse Cursor Movement - 06-04-2007, 08:52 AM

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


   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Re: Limiting Mouse Cursor Movement
Old
  (#2)
Randy Birch
Guest
 
Posts: n/a
Default Re: Limiting Mouse Cursor Movement - 06-04-2007, 08:52 AM

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" <EMAIL REMOVED> wrote in message
news:bq30od$45$EMAIL REMOVED...
: 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
:
:


   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: Limiting Mouse Cursor Movement
Old
  (#3)
Randy Birch
Guest
 
Posts: n/a
Default Re: Limiting Mouse Cursor Movement - 06-04-2007, 08:52 AM

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
***ist.

--

Randy Birch
MVP Visual Basic
http://www.mvps.org/vbnet/
Please respond only to the newsgroups so all can benefit.


"Randy Birch" <EMAIL REMOVED> wrote in message
news:lI8xb.6252$EMAIL REMOVED. 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" <EMAIL REMOVED> wrote in message
: news:bq30od$45$EMAIL REMOVED...
: : 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
: :
: :
:
:


   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Contact Us - Forum Care Forums - Archive - Top