Board index » delphi » Still having Mouse juddrr problems - example unit enclosed

Still having Mouse juddrr problems - example unit enclosed

Quote
<johns...@dummyAddress.com> wrote in message

news:MPG.16153e8b9a229a959896c9@newsgroups.borland.com...

Quote

> See earlier thread for details or just compile this unit.

> I've changed the mouse co-ordinates to use the form as
> a reference (which doesn't change or move) and still get
> judder.

Try it this way -

---------------
  private
    { Private declarations }
    FMouseIsDown: Boolean;
    FHitPointX, FHitPointY: Integer;
  ...

procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  FMouseIsDown := true;
  FHitPointX := X;
  FHitPointY := Y;
end;

procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  if FMouseIsDown then
  begin
    TControl(Sender).Left := TControl(Sender).Left+X-FHitPointX;
    TControl(Sender).Top := TControl(Sender).Top+Y-FHitPointY;
  end;
end;

procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  FMouseIsDown := false;
end;
---------------

--
Regards,
Chris Luck

 

Re:Still having Mouse juddrr problems - example unit enclosed


Hi john,

I think the problem is that you implement the mouse events in the TImage and
not in the form. I do the same as you but in the form events, and that works
fine.

Here it's the image that send mouse messages, hence coordinates are related
to its own borders. But as the image itself moves in the process, the
reference of its coordinates also changes from one MouseMove event to the
next. Hence if you compare the coordinates in the two successive MouseMove
events you won't get the "absolute" move of the mouse.

I am not sure that the fact that the form captures the mouse changes the way
the coordinates are interpreted. I also do as Chris, using a boolean
variable to see if mouse has been clicked down on the component that should
be moved.

Also this is a detail, but you could save several lines by not testing
if  Y < startP.Y
and such (also for X), because what you assign is the same thing:
 image1.top - (startP.Y - Y)
is in fact the same as
 image1.top + (Y - startP.Y)
if you remove the parentheses.

If you don't want to use the form's mouse events, you could also explore the
drag events of the image and of the form.

Thrse

Re:Still having Mouse juddrr problems - example unit enclosed


Hi john,

Forgot to add that this should also be changed:

procedure TfmMain.Image1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  startP.X := X + Image1.Left;
  startP.y := y + Image1.Top;
  setCapture(fmMain.Handle);
  capture := true;
end;

Thrse

Re:Still having Mouse juddrr problems - example unit enclosed


Hi john,

Just thought of another and probably simpler way:

procedure TfmMain.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
   Y: Integer);
var CurrentPoint : TPoint;
 begin
   if (capture = false) then exit;

   CurrentPoint.X := X + image1.left;
   CurrentPoint.Y := Y + image1.top;

  image1.top := image1.top + CurrentPoint.X - startP.X ;
  image1.left := image1.left + CurrentPoint.Y - startP.Y ;

  startP.X := CurrentPoint.X;
  startP.Y := CurrentPoint.Y;

 end;

Thrse

Re:Still having Mouse juddrr problems - example unit enclosed


Hi john,

Sorry for the numerous posts (also for other readers in the newsgroup)!

I just tested your code and it confirms that even when the form captures the
mouse, the sent coordinates are relative to the top left corner of the
image.

I also tested the solution I proposed and it seems to work correctly, as
long as you correct an error I made in one of my previous posts (inversion
of X and Y in OnMouseMove). Should be:

procedure TfmMain.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
   Y: Integer);
var CurrentPoint : TPoint;
 begin
   if (capture = false) then exit;

   CurrentPoint.X := X + image1.left;
   CurrentPoint.Y := Y + image1.top;

  image1.left := image1.left + CurrentPoint.X - startP.X ;
  image1.top := image1.top + CurrentPoint.Y - startP.Y ;

  startP.X := CurrentPoint.X;
  startP.Y := CurrentPoint.Y;

 end;

Also you should delete the following lines:

Quote
>   setCapture(fmMain.Handle);

in the OnMouseDown event, and:
Quote
> releaseCapture;

in the OnMouseUp event.

By default, controls are initialised with csMouseCapture in ControlStyle,
meaning that once they are clicked they capture the mouse until the mouse
button is released. This is the behaviour that you need, so the TImage
receives all mouse messages as long as the mouse button is down, even if
your mouse moves outside of the borders of the TImage. In other terms, you
should not change anything in the default way capture is operated.

Thrse

Re:Still having Mouse juddrr problems - example unit enclosed


Hi john,

Quote
> it shouldn't take much to turn it into a generic drag
> routine that doesn't require the start-end drag routines.
> A unit that will drag any object clicked on could be quite
> useful.

I agree. Drag routines are not easy to understand (particularly the
TDragObject issue). Besides the problem with drag routines is that you have
to implement the OnDragOver event for all controls and for the form in order
to be able to move the dragged object everywhere. Seems like a lot of code
for something that could be simpler. And I still can not see how to handle
the mouse movements that happen inside the limits of the dragged object.

Quote
> I'm guessing it would need an understanding of sender and Aowner
> (which I don't as yet but am about to start reading...)
> but not very much more.
> what do you think ?

As those are fundamental concepts, you will surely not loose your time. I
just wonder about the way to integrate a unit with the standard TControl
mouse events. I may be wrong, but it seems that you would need to write a
custom control as a base class for all controls you use.

Thrse

Re:Still having Mouse juddrr problems - example unit enclosed


Hi john,

Thanks. I will have a closer look in the coming days.

Quote
> Thanks for all your help - I hope this helps as a small
> payback.

You're welcome. I learned a few things too at the occasion...

Thrse

Other Threads