2005. június 12., vasárnap

Dragging non-windowed controls at run-time

Problem/Question/Abstract:

Someone asked how to drag a TImage for example, and just playing with code I came up with this quick (and dirty?) solution.

Answer:

There's an article here on how to drag windowed controls "Dragging controls and forms the easy way", but that code doesn't work for TImages for example

My solution for such thing is simply put the Image inside a TPanel and from the Image OnMouseDown call the code of the TPanel, thus resulting in being able to move the image, here's the code:

procedure TForm1.Panel1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
const
SC_DragMove = $F012; {this is magic (undocumented)}
begin
ReleaseCapture;
panel1.perform(WM_SysCommand, SC_DragMove, 0);
end;

procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
panel1mousedown(Sender, Button, Shift, X, Y)
end;

The same would work for resizing, or all the other things that can be done changing the constant (given that the image is aligned using alClient)


Nincsenek megjegyzések:

Megjegyzés küldése