We help IT Professionals succeed at work.

Restrict mouse within window

Kapusta
Kapusta asked
on
I would like to restrict the movement of the mouse pointer/cursor within a Delphi 3 application's window.  Assuming I'm working at 1024x768, and my form is some odd size smaller than this.  When the form appears, I want to be able to keep the mouse within the form.  When I close the form, I want the mouse to have free roam again.  How do I do this?
Comment
Watch Question

Commented:
Hi Kapusta,

Yes you can use ClipCursor.

var
  r: TRect;
begin
  r := BoundsRect;
  ClipCursor(@r)
end;

ClipCursor(nil) will free up the cursor

I am not sure if BoundsRect returns the correct coordinates. You need the screen coordinates.

If it doesn't work with BoundsRect you can do

r := Rect(Left, Top, Left + Width, Top + Height);

before the call to clipcursor.

Regards Jacco
listening !!

Explore More ContentExplore courses, solutions, and other research materials related to this topic.