Guest_imported
New member
- Jan 1, 1970
- 0
How to move a label, a editbox, a combobox, a listbox, or other components at run-time?
Give-me an example
Thanks
Give-me an example
Thanks
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
procedure TForm1.FormCreate(Sender: TObject);
begin
Application.OnMessage := AWndProc;
end;
procedure TForm1.AWndProc(var AMsg: TMsg; var Handled: Boolean);
var
APoint : TPoint;
ARect : TRect;
begin
case AMsg.message of
WM_MOUSEMOVE :
begin
if (GetParent(AMsg.hwnd) = 0) then Exit;
if ((AMsg.wParam and MK_RBUTTON) > 0) then
begin
GetWindowRect(AMsg.hwnd, ARect);
APoint.x := LOWORD(AMsg.lParam);
APoint.y := HIWORD(AMsg.lParam);
Windows.ClientToScreen(AMsg.hwnd, APoint);
Windows.ScreenToClient(GetParent(AMsg.hwnd), APoint);
APoint.x := APoint.x + Round((ARect.Left - ARect.Right) / 2);
APoint.y := APoint.y + Round((ARect.Top - ARect.Bottom) / 2);
SetWindowPos(AMsg.hwnd, 0, APoint.x, APoint.y, 0, 0, SWP_NOZORDER or SWP_NOSIZE);
end;
end;
end;
end;
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
procedure Label1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Label1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Label1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
fOffsetX,
fOffsetY : Integer;
implementation
{$R *.dfm}
procedure TForm1.Label1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
Label1.Tag := 1;
end;
procedure TForm1.Label1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if Label1.Tag = 1 then
begin
fOffsetX := X;
fOffsetY := Y;
end;
end;
procedure TForm1.Label1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
Label1.Tag := 0;
Label1.Left := Label1.Left + fOffsetX;
Label1.Top := Label1.Top + fOffsetY;
fOffsetX := 0;
fOffsetY := 0;
end;
end.
procedure TForm1.FormCreate(Sender: TObject);
begin
Application.OnMessage := AWndProc;
end;
procedure TForm1.AWndProc(var AMsg: TMsg; var Handled: Boolean);
var
APoint, PT : TPoint;
ARect : TRect;
AForm : TForm;
i : Integer;
begin
// AControl variable is declaered in public section of a form :
// AControl : TControl;
case AMsg.message of
WM_RBUTTONDOWN :
begin
AForm := Screen.ActiveForm;
for i := 0 to AForm.ControlCount - 1 do
begin
PT.x := LOWORD(AMsg.lParam);
PT.y := HIWORD(AMsg.lParam);
APoint.x := AForm.Controls[i].Left;
APoint.y := AForm.Controls[i].Top;
if (AForm.Controls[i] is TWinControl)then
begin
MapWindowPoints(GetParent(AMsg.hwnd), 0, APoint, 1);
MapWindowPoints(AMsg.hwnd, 0, PT, 1);
end
else
begin
MapWindowPoints(AForm.Controls[i].Parent.Handle, 0, APoint, 1);
MapWindowPoints(AForm.Controls[i].Parent.Handle, 0, PT, 1);
end;
ARect.Left := APoint.x;
ARect.Right := ARect.Left + AForm.Controls[i].Width;
ARect.Top := APoint.y;
ARect.Bottom:= ARect.Top + AForm.Controls[i].Height;
if PtInRect(ARect, PT) then
begin
AControl := AForm.Controls[i];
Break;
end;
end;
end;
WM_RBUTTONUP :
begin
AControl := nil;
end;
WM_MOUSEMOVE :
begin
if Assigned(AControl)and((AMsg.wParam and MK_RBUTTON) > 0)then
begin
APoint.x := LOWORD(AMsg.lParam);
APoint.y := HIWORD(AMsg.lParam);
MapWindowPoints(AMsg.hwnd, 0, APoint, 1);
if (AControl is TWinControl)then
Windows.ScreenToClient(GetParent(TWinControl(AControl).Handle), APoint)
else
APoint := AControl.Parent.ScreenToClient(APoint);
AControl.Left := APoint.x - Round(AControl.Width / 2);
AControl.Top := APoint.y - Round(AControl.Height / 2);
end;
end;
end;
end;