unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    { Private declarations }
    CompList:TList;
    procedure PanelMouseDown(Sender: TObject; Button: TMouseButton;
       Shift: TShiftState; X, Y: Integer);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
 CompList:=TList.Create;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
 CompList.Free;
end;

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
 TempPanel:TPanel;
begin
 TempPanel:=TPanel.Create(Form1);

 TempPanel.Left:=X;
 TempPanel.Top:=Y;

 TempPanel.Width:=20;
 TempPanel.Height:=20;

 TempPanel.OnMouseDown:=PanelMouseDown;

 TempPanel.Tag:=CompList.Add(TempPanel);

 Form1.InsertControl(TempPanel); //Вставляю панель на форму
end;

procedure TForm1.PanelMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
 index, i:Integer;
begin
 Label1.Caption:=IntToStr(TPanel(CompList.Items[TPanel(Sender).Tag]).Left);
 Label2.Caption:=IntToStr(TPanel(Sender).Left);

 if Button=mbRight then
  begin
   index:=TPanel(Sender).Tag;
   TPanel(CompList.Items[index]).Free;
   CompList.Delete(index);

   for i:=index to CompList.Count -1 do
    TPanel(CompList.Items[i]).Tag:=TPanel(CompList.Items[i]).Tag-1;
  end;
end;

end.
 