callwright
Programmer
I am trying to drag and drop a TImage on a form. Problem is that the TImage does not move from it's original location until the mouse button is released. What I need is for the TImage to remain visible while following the mouse movement. This is a somewhat dorky question, but I've found that information on this particular topic is almost impossible to find on the Internet or in books.
Here's what I've got ...
A project with one form containing a single TImage:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, jpeg;
type
TForm1 = class(TForm)
Image1: TImage;
procedure FormDragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure FormDragDrop(Sender, Source: TObject; X, Y: Integer);
procedure FormCreate(Sender: TObject);
procedure Image1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
downY: integer;
mouseIsDown: boolean;
implementation
{$R *.dfm}
procedure TForm1.FormDragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
if mouseIsDown then
begin
Image1.Top := Y;
Image1.Repaint;
end;
Accept := (Source is TImage);
end;
procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if Button = mbLeft then
Image1.BeginDrag(true);
end;
procedure TForm1.FormDragDrop(Sender, Source: TObject; X, Y: Integer);
begin
TImage(Source).Top := Y;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
mouseIsDown := false;
end;
procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
mouseIsDown := false;
end;
procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
mouseIsDown := true;
end;
end.
Here's what I've got ...
A project with one form containing a single TImage:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, jpeg;
type
TForm1 = class(TForm)
Image1: TImage;
procedure FormDragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure FormDragDrop(Sender, Source: TObject; X, Y: Integer);
procedure FormCreate(Sender: TObject);
procedure Image1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
downY: integer;
mouseIsDown: boolean;
implementation
{$R *.dfm}
procedure TForm1.FormDragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
if mouseIsDown then
begin
Image1.Top := Y;
Image1.Repaint;
end;
Accept := (Source is TImage);
end;
procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if Button = mbLeft then
Image1.BeginDrag(true);
end;
procedure TForm1.FormDragDrop(Sender, Source: TObject; X, Y: Integer);
begin
TImage(Source).Top := Y;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
mouseIsDown := false;
end;
procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
mouseIsDown := false;
end;
procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
mouseIsDown := true;
end;
end.