11-04-2010 07:40 AM
In one of my programs I have a good sized button. I want to restrict the mouse movements to within the boundaries of this button. In other words... I dont want the mouse cursor to leave this button.
I've searched and tried different things but cant seem to get it to work.
Any ideas?
11-04-2010 08:19 AM
11-04-2010 08:31 AM - edited 11-04-2010 08:32 AM
11-05-2010 12:53 PM
WayneS1324,
Another way to prevent the mouse from 'leaving' a button is to use the Acquire Input Data to get the mouse coordinates and check both the X and Y axis to make sure the mouse has not left the bounds of the button. If the cursor is outside the button bounds you can use a call to user32.dll to reset the button position. Attached is a simple example showing how you can do this. This example can easily be modified to restrict the mouse to any rectangular region.
Regards,
Sam K
Applications Engineer
National Instruments
11-05-2010 01:08 PM
@sam Kristoff wrote:
... If the cursor is outside the button bounds you can use a call to user32.dll to reset the button position.
Hi, Sam,
Just one small remark.
If the cursor outside, then cursor is _already_ outside. So, if the mouse moves quickly, then sometimes you will see it outside of button for short time. In additional you have to poll mouse coordinates. The only way to clip mouse 100% within area and without polling is WinAPI suggested above. Of course, every medal has two sides: disadvantage of this method - if you will break VI's execution, then mouse remain clipped 😉
Andrey.
11-05-2010 03:30 PM - edited 11-05-2010 03:35 PM
Elegant!
Here's an example, thanks for the great tip, Andrey!
11-05-2010 03:38 PM
I am aware of a bug with my above method: you must ensure the Origin of the FP is set to (0,0). This offset can be accounted for programmatically to make the solution more robust, and I'll leave that as an exercise for the reader.
11-05-2010 03:48 PM