Moving TImage with Mouse

Moving TImage with Mouse

Post by Derek Pinkerto » Tue, 20 Jun 2000 04:00:00



I have done this with a TShape but it's the same principal

const Draging = 1;
          Still = 2;

in the mouse down event for the shape do the following

TImage(sender).Tag := Draging;

in the mouse up event for the shape do the following

TImage(sender).Tag:= Still;

in the mouse move event for the shape to the following

with sender as TImage do
 if Tag = Draging then
  begin
   Top := Top + Y;
   Left := Left + x;
  end;

Since x and y will be negative if they move in a left or up motion it will
move the image in the same direction as the mouse.  This will produce a
small side effect of the mouse moving to the upper left corner of the image,
to avoid this, save the coords of the mouse in the mouse down event and add
that to your left and top in the move event, sorry I don't have an example
of this

 
 
 

Moving TImage with Mouse

Post by John Colli » Wed, 21 Jun 2000 04:00:00


How do you code a routine so you can click on a bmp in a TImage and
move it with the mouse. I can do it but seems everytime I do, the
mouse coordinates just add to the top and left of the TImage and it
eventually disappears off the screen.
TIA
John

 
 
 

Moving TImage with Mouse

Post by Thérèse Hanque » Wed, 21 Jun 2000 04:00:00


Hi Derek,

Quote:> with sender as TImage do
>  if Tag = Draging then
>   begin
>    Top := Top + Y;
>    Left := Left + x;
>   end;

I don't understand how this can work, because X and Y in the OnMouseMove
event give the position of the mouse a the moment the event is fired, and
not the difference of positions (and thus X and Y are never negative as long
as the mouse remains in the client area).
As you said, you have to save each time the previous coordinates of the
mouse in a field of the form variable (let's say PreviousPoint : TPoint) if
you want to compute this difference and add it to the Top and Left
properties, for example like this:

MyImage.Top := MyImage.Top + (X-PreviousPoint.X);
MyImage.Left := MyImage.Left + (Y-PreviousPoint.Y);
PreviousPoint.X := X;
PreviousPoint.Y := Y;

Of course PreviousPoint must be initialised in the OnMouveDown event.

I used to do that to move objects on a form, but now I find it much easier
to use the drag functions, mostly if you have a lot of different objects you
want to be able to move and you don't want to implement too much in the
mouse events.

What I do is just set the DragMode property of the object to move to
dmAutomatic (but you can still let it Manual and implement a test in the
OnStartDrag event). Then in the OnEndDrag event of the object to move, set:

Left := X;
Top := Y;

Last thing to do, tell the form to accept that you drag the image to a new
location: in the OnDragOver event of the form, set this line:

 if Source is TImage then Accept := True;

This should do the job.

Thrse

 
 
 

Moving TImage with Mouse

Post by Andy Brow » Wed, 21 Jun 2000 04:00:00


Have you looked at
    ScreenToClient() &
    ClientToScreen()


Quote:> How do you code a routine so you can click on a bmp in a TImage and
> move it with the mouse. I can do it but seems everytime I do, the
> mouse coordinates just add to the top and left of the TImage and it
> eventually disappears off the screen.
> TIA
> John

 
 
 

Moving TImage with Mouse

Post by John Colli » Wed, 21 Jun 2000 04:00:00


Works like a charm. Thanks.
John
 
 
 

Moving TImage with Mouse

Post by Derek Pinkerto » Wed, 21 Jun 2000 04:00:00


Sorry I wasn't looking at my code at the time that I wrote that, you're
right I computed the new coords with the saved coords, I never thought of
using the drag events, but that looks like it would be a better solution.
 
 
 

Moving TImage with Mouse

Post by Thérèse Hanque » Thu, 22 Jun 2000 04:00:00


Hi Derek,

Funny we discuss that because I just spent the whole day of yesterday trying
to replace my custom code by the drag events of a TShape... and this morning
I erased all and came back to my old code.
Drag events are very easy to use, but there is one problem, it's that the
control captures the mouse, and thus when you move the mouse inside the
object itself, it is not considered as "dragging" (you can see that by the
cursor). This can make it very difficult to be precise in moving your object
to a given place, unless you have taken it by the appropriate side.
I even tried writing and using a component derived of TShape that doesn't
capture the mouse, but to no avail.
Also with the code I indicated, you don't see the object moving, it just
"jumps" at its new location once you release the mouse, but that can be
cured by using the OnDragOver event of the form. Apparently it fires in the
same way as the OnMouseMove event (every few pixels it seems).

Thrse


Quote:> Sorry I wasn't looking at my code at the time that I wrote that, you're
> right I computed the new coords with the saved coords, I never thought of
> using the drag events, but that looks like it would be a better solution.

 
 
 

Moving TImage with Mouse

Post by John Colli » Thu, 22 Jun 2000 04:00:00


Thanks, that helps a lot. John
 
 
 

Moving TImage with Mouse

Post by Harm » Thu, 22 Jun 2000 04:00:00


Here's what I use to move a bitmap within a TImage.  It's fast and
flicker-free, although it's probably not the most elegant solution.

I use this to do a 'paste as selection' in one of my apps;  it allows the
user to paste an image and move it to the desired location within an
existing image.

This example is stripped down;  it doesn't do any error checking, and I
normally force all the images to be bitmaps.  To set up the sample:

Drop a TButton, a TImage, and a TPanel on the form.

Change the TImage to AutoSize true and load a bitmap into it.  Change the
name to ImageMain.

Remove the caption from the panel, and change the BevelOuter to bvNone.
Change the name to PanelMove.

Drop another Timage onto the PanelMove (not the form).  Load a bitmap into
it (smaller then the one in ImageMain) and change it to AutoSize true.
Change the name to ImageMove.  Change the cursor to crHandPoint.

Change PanelMove to be AutoSize true.

Hook up the code to the events for the controls.

Run the app, click the button to 'paste' the ImageMove into ImageMain.

This stripped down version allows the ImageMove to move anywhere on the
form.  To force it to stay within the ImageMain boundaries, you could put
the ImageMain on a panel, then put PanelMove into that.  The parent panel
won't allow it to move outside its borders.  It would require that the
z-order be correct, and the calculations for position would need to be
changed.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, StdCtrls, clipbrd;

type
  TForm1 = class(TForm)
    ImageMain: TImage;
    Button1: TButton;
    PanelMove: TPanel;
    ImageMove: TImage;
    procedure Button1Click(Sender: TObject);
    procedure ImageMoveMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure ImageMoveMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
  private
    { Private declarations }
    Pasting : Boolean;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
   // if Clipboard.HasFormat(CF_BITMAP) then begin
  //  ImageMove.Picture.Assign(Clipboard);

DrawFocusRect(ImageMove.Canvas.Handle,RECT(0,0,ImageMove.Width,ImageMove.Hei
ght));
    PanelMove.Left := ImageMain.Left;
    PanelMove.Top := ImageMain.Top;
    PanelMove.Visible := TRUE;
  // end;
end;

procedure TForm1.ImageMoveMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
const
  SC_DragMove = $F012;
begin
  ReleaseCapture;
  Pasting := TRUE;
  PanelMove.perform(WM_SysCommand, SC_DragMove, 0);
end;

procedure TForm1.ImageMoveMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  if Pasting = TRUE then begin

DrawFocusRect(ImageMove.Canvas.Handle,RECT(0,0,ImageMove.Width,ImageMove.Hei
ght));
    ImageMain.Canvas.CopyRect(RECT(PanelMove.Left - ImageMain.Left,
      PanelMove.Top - ImageMain.Top,
      PanelMove.Left + PanelMove.Width - ImageMain.Left,
      PanelMove.Top + PanelMove.Height - ImageMain.Top),
      ImageMove.Picture.Bitmap.Canvas,
      RECT(0,0,ImageMove.Width,ImageMove.Height));
    PanelMove.Visible := FALSE;
    Pasting := FALSE;
    Screen.Cursor := crDefault;
  end;
end;
end.

--
Harm

http://www.users.uswest.net/~sharman1/

Cool Graphics, for free!

Quote:> How do you code a routine so you can click on a bmp in a TImage and
> move it with the mouse. I can do it but seems everytime I do, the
> mouse coordinates just add to the top and left of the TImage and it
> eventually disappears off the screen.
> TIA
> John

 
 
 

1. Help Moving a TImage over a TImage

Marco,

That's the route I suggest.  To resize the images, use the CopyImage()
Win32 function.  See the following post for a code example...

http://groups.google.com/groups?selm=3B33B78F.4200BDB6%40cornell.edu

Best of luck,
--
Damon (TeamB)
BCBCAQ--http://bcbcaq.bytamin-c.com

2. how to publish for shockwave ?? Thank you

3. Moving Timage over another Timage causes flickering ??

4. using 'skins' for GUI ?

5. Mouse move and mouse enter Events

6. OpenGL lighting

7. The mouse move make graphic data move

8. 3D selection and/or ray tracing

9. Moving Rectangle upon the TImage...

10. Moving around a Timage

11. Help move a TImage diagonally.

12. Need help troubleshooting moving a TImage from point A to B

13. TImage and Mouse Events