{*******************************************************}
{                                                       }
{       Pascal Script Source File                       }
{       Run by RemObjects Pascal Script in CnWizards    }
{                                                       }
{       Generated by CnPack IDE Wizards                 }
{                                                       }
{*******************************************************}

program SwapControlPos;

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  CnWizUtils;

// Please Add this Script to Script Library, and Run it from the Corresponding
// item of the Dropdown Menu under "Run" ToolButton in Script Window.

var
  ControlList: TList;
  TmpInt: Integer;
  Control1, Control2: TControl;
begin
  ControlList := TList.Create;
    
  try
    // Get Selected Controls in Current Form.
    if not CnOtaGetSelectedControlFromCurrentForm(ControlList) then
    begin
      WriteLn('NO Form or No Control Selected. Please Select 2 Control at Least.');
      Exit;
    end;

    if ControlList.Count <= 1 then
    begin
      WriteLn('Please Select 2 Control at Least.');
      Exit;
    end;

    if (ControlList[0] <> 0) and (ControlList[1] <> 0) then
    begin
      // Pascal Script does NOT Support Integer to Object Convertion (TObject(0))
      // So we use function CnIntToObject to do it.
      Control1 := TControl(CnIntToObject(ControlList[0]));
      Control2 := TControl(CnIntToObject(ControlList[1]));

      TmpInt := Control1.Left;
      Control1.Left := Control2.Left;
      Control2.Left := TmpInt;

      TmpInt := Control1.Top;
      Control1.Top := Control2.Top;
      Control2.Top := TmpInt;

      WriteLn('Swap OK.');
    end;
  finally
    ControlList.Free;
  end;
end.
 
