type
TForm1 = class(TForm)
Image1: TImage;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
procedure WMDropFiles(var Msg: TWMDropFiles); message WM_DROPFILES;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.WMDropFiles(var Msg: TWMDropFiles);
var
CFileName: array[0..255] of Char;
begin
try
if DragQueryFile(Msg.Drop, 0, CFileName, SizeOf(CFileName)) > 0 then
begin
Image1.Picture.LoadFromFile(CFileName);
Msg.Result := 0;
end;
finally
DragFinish(Msg.Drop);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
DragAcceptFiles(Handle, TRUE);
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
DragAcceptFiles(Handle, FALSE);
end;
{Here is a way to set up a form to support dragging components around
on it at run time. Each control that you want to move in this fashion
should have its mouse down, mouse move, and mouse up events set to
ControlMouseDown, ControlMouseMove and ControlMouseUp, respectively}
Type
TCracker = Class(TControl);
{ Needed since TControl.MouseCapture is protected
by declaring a descendant class we can typecast the control
to this and access its protected methods with in this unit.}
{ Control event handlers are attached to both memo and image mouse
events. }
procedure TForm1.ControlMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
downX:= X;
downY:= Y;
dragging := True;
TCracker(Sender).MouseCapture := True;
end;
procedure TForm1.ControlMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
If dragging Then with Sender As TControl Do Begin
Left := X-downX+Left;
Top := Y-downY+Top;
End;
end;
procedure TForm1.ControlMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
If dragging then Begin
dragging := False;
TCracker(Sender).MouseCapture := False;
End;
end;