473,385 Members | 2,069 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Limiting Mouse Cursor Movement

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
2 6914
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Csaba2000 | last post by:
I have set onmousedown to change the cursor, but this setting is ignored (IE 5.5; NN 6.1 on Win 2K Pro) until the mouse is either moved or the mouse button is released. On Opera 7.01, the setting...
10
by: Danny | last post by:
How can I get the coordinates of the mouse cursor in Mozilla browsers as well as Opera and IE6? I'm struggling to understand how to capture mouse movement events with Mozilla/Netscape/Firefox and...
3
by: Wapr | last post by:
Hi, How can I limit to movement of the cursor on my form to a specified rectangle? thanks
6
by: Carlos García-Carazo | last post by:
Hello, I am working on a C# application for an industrial machine, using Windows Forms, where the user could look at the screen from a 90 degree rotated position, like he could turn the monitor...
3
by: garyusenet | last post by:
I have a programme written in C++, the programme is unmanaged and is an executable, i don't have any source code. I'm writing a C# program. I want to (a) start the programme minimized. (b) send...
4
by: mike | last post by:
I have the opportunity to rescue a project that uses a mouse to sense the relative position of a machine. The hardware is built...just needs to be programmed. Stop snickering!!! I didn't do it...I...
6
by: c.k. | last post by:
I was wondering if there was a way to give mouse movement on an empty form some lag/delay? For instance, user moves mouse, and the cursor is 20ms (possible adjustable from some option) behind the...
0
by: Studlyami | last post by:
I am trying to find how windows translate the input from a HID device to an actual windows message; specifically, the mouse. I am trying to figure out how when the mouse moves the process windows...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.