473,769 Members | 5,072 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6945
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 GetDesktopWindo w Lib "user32" () As Long

Private Sub Command1_Click( )

Unload Me

End Sub
Private Sub Form_Unload(Can cel As Integer)

'Release the cursor restriction
ClipCursor ByVal 0&

End Sub
Private Sub Form_Load()

Dim rc As RECT
Dim topcorner As POINT

GetClientRect GetDesktopWindo w(), 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*********@ho tmail.com> wrote in message
news:bq******** *@aspen.sucs.so ton.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.c able.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 GetDesktopWindo w Lib "user32" () As Long
:
: Private Sub Command1_Click( )
:
: Unload Me
:
: End Sub
:
:
: Private Sub Form_Unload(Can cel As Integer)
:
: 'Release the cursor restriction
: ClipCursor ByVal 0&
:
: End Sub
:
:
: Private Sub Form_Load()
:
: Dim rc As RECT
: Dim topcorner As POINT
:
: GetClientRect GetDesktopWindo w(), 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*********@ho tmail.com> wrote in message
: news:bq******** *@aspen.sucs.so ton.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
17311
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 seems to be ignored completely, even when I try with window.setTimeout. So my two questions are: (1) Most important: Is there anything I can do so that I don't have to wait for the next mouse event before the cursor gets repainted. (2) Why...
10
18083
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 I've Googled so much my brain hurts. http://www.ghpkendal.co.uk/TestPages/Test.htm Move your cursor over the yellow area and you should see the mouse coordinates echoed above.
3
2241
by: Wapr | last post by:
Hi, How can I limit to movement of the cursor on my form to a specified rectangle? thanks
6
10816
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 to leave it standing on its left (or right) side. There are many ways to show a "turned" interface in this case, but the problem is that when he moves the mouse right, in a turned screen the mouse cursor goes down (or up), when he moves it up...
3
3612
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 keystrokes to the programme, and mouse strokes. Can you please tell me how I would do this assuming the programme is called "c:\programme.exe"
4
6976
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 just gotta fix it. I need to make some calculations on the measurements and VB6 is my language. Yes, the system mouse will corrupt the measurement, but it's an auditing function and that's acceptable.
6
3116
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 movement. So when user stops moving mouse, 20ms later the cursor itself stops moving... I tried using: Windows.Forms.Cursor.Current.Position = New System.Drawing.Point(x,y) and forms of sleep/timer events but cant seem to get it to work...
0
2311
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 takes in order to ensure all windows receive the notification that the mouse moved and how windows moves the mouse cursor. What process handles the movement of the mouse cursor and at what point does windows move the mouse cursor vs posting the...
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9996
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7410
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6674
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5304
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3963
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.