Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How can i keep the Mouse-Cursor inside a TPaintBox (Delphi 2) ?

420 views
Skip to first unread message

Andreas Schaedlich

unread,
Jul 12, 1998, 3:00:00 AM7/12/98
to
Hi to everybody !

can anyone tell me, how i can keep the Mouse Cursor inside a
PaintBox (do not allow the Cursor to move outside the PaintBox)?

Rick Rogers (TeamB)

unread,
Jul 12, 1998, 3:00:00 AM7/12/98
to
On Sun, 12 Jul 1998 13:21:23 +0200, Andreas Schaedlich
<Andreas.S...@t-onlinde.de> wrote:

> can anyone tell me, how i can keep the Mouse Cursor inside a
> PaintBox

You can't and/or shouldn't, because doing so violates just about every
principle of Windows. You can, however, use the SetCapture Windows API
to direct mouse input to a window, regardless of whether the cursor is
within the borders of that window.

--
Rick Rogers (TeamB) | Fenestra Technologies
http://www.fenestra.com/

Yorai Aminov (TeamB)

unread,
Jul 12, 1998, 3:00:00 AM7/12/98
to
On Sun, 12 Jul 1998 13:21:23 +0200, Andreas Schaedlich
<Andreas.S...@t-onlinde.de> wrote:

>can anyone tell me, how i can keep the Mouse Cursor inside a

>PaintBox (do not allow the Cursor to move outside the PaintBox)?

Use the ClipCursor function. However, Rick is right, and this behavior
is not recommended under Windows if it can be avoided.


---
Yorai Aminov (TeamB)
http://ourworld.compuserve.com/homepages/yaminov

Earl F. Glynn

unread,
Jul 12, 1998, 3:00:00 AM7/12/98
to

Andreas Schaedlich wrote in message <35A89C33...@t-onlinde.de>...
>Hi to everybody !

>
>can anyone tell me, how i can keep the Mouse Cursor inside a
>PaintBox (do not allow the Cursor to move outside the PaintBox)?

I know Rick and Yorai seem to say this should never be done, and I generally
agree with them, but I have locked the cursor into drawing areas while
drawing. If you don't, capturing "exit" events gets somewhat awkward, so
you just never let the user draw outside of the area.

I usually only lock the cursor into a drawing area OnMouseDown and release
it OnMouseUp.

Here's an example of how to lock the cursor into the ImageArea (TImage):

PROCEDURE TFormLineStretch.RestrictCursorToDrawingArea;
VAR
CursorClipArea : TRect;
ImageOriginInScreenCoordinates: TPoint;
BEGIN
// Restrict cursor to Image area with WinAPI call
ImageOriginInScreenCoordinates :=
ClientToScreen( Point(ImageArea.Left,
ImageArea.Top) );

CursorClipArea := Bounds(ImageOriginInScreenCoordinates.X,
ImageOriginInScreenCoordinates.Y,
ImageArea.Width, ImageArea.Height);

Windows.ClipCursor(@CursorClipArea)
END {RestrictCursorToDrawingArea};


PROCEDURE TFormLineStretch.RemoveCursorRestrictions;
BEGIN
Windows.ClipCursor(NIL)
END {RemoveCursorRestrictions};

efg
_________________________________________
efg's Computer Lab: http://infomaster.net/external/efg

Earl F. Glynn E-Mail: Earl...@att.net
MedTech Research Corporation, Lenexa, KS USA


David Ullrich

unread,
Jul 12, 1998, 3:00:00 AM7/12/98
to
Earl F. Glynn wrote:
>
> Andreas Schaedlich wrote in message <35A89C33...@t-onlinde.de>...
> >Hi to everybody !
> >
> >can anyone tell me, how i can keep the Mouse Cursor inside a
> >PaintBox (do not allow the Cursor to move outside the PaintBox)?
>
> I know Rick and Yorai seem to say this should never be done, and I generally
> agree with them, but I have locked the cursor into drawing areas while
> drawing. If you don't, capturing "exit" events gets somewhat awkward, so
> you just never let the user draw outside of the area.
>
> I usually only lock the cursor into a drawing area OnMouseDown and release
> it OnMouseUp.

Actually what Rick said was BAD was SetCapture, which is not
at all the same thing as the ClipCursor in your example. Of course
ClipCursor is dangerous as well - if you clip the cursor in OnMouseDown
then release it in OnMouseUp that has to be safe IF the region you
clip the cursor to IS inside your client region (so that you're guaranteed
to get the corresponding MouseUp).

It can't be all that bad a thing to do since the Delphi
IDE does it (when you mousedown on the form the cursor's clipped
to the form until you release the button), as well as lots of
paint programs, etc. (And experimenting with ClipCursor is good
because you get practice in figuring out how to restart Windows
without using the mouse... after that happens a few times you
put ClipCursor(nil) in a few key spots and then you just have
to remember how to hit Ctrl-Alt-Delete without the mouse.)

--
David Ullrich

sig.txt not found

0 new messages