Использование визуальных классов в скриптах (пример)

Post Reply
Eugene
Posts: 2805
Joined: Tue Sep 16, 2008 7:30 pm

Использование визуальных классов в скриптах (пример)

Post by Eugene »

Требуемая версия программы 1.28.5 и выше

Code: Select all

const
  ciFormWidth  = 300;
  ciFormHeight = 200;
  
var
  ParametersForm: TForm;
  
function CreateButton(aParent: TWinControl; const aCaption: string; aLeft, aTop, aWidth, aHeight, aModalResult: Integer): TButton;
begin
  Result := TButton.Create(ParametersForm);
  Result.SetBounds(aLeft, aTop, aWidth, aHeight);
  Result.Caption := aCaption;
  Result.ModalResult := aModalResult;
  Result.Parent := aParent
end;

function CreateGroupBox(aParent: TWinControl; const aCaption: string; aLeft, aTop, aWidth, aHeight: Integer): TGroupBox;
begin
  Result := TGroupBox.Create(ParametersForm);
  Result.SetBounds(aLeft, aTop, aWidth, aHeight);
  Result.Caption := aCaption;
  Result.Parent := aParent
end;

function CreateCheckBox(aParent: TWinControl; const aCaption: string; aLeft, aTop, aWidth, aHeight: Integer): TCheckBox;
begin
  Result := TCheckBox.Create(ParametersForm);
  Result.SetBounds(aLeft, aTop, aWidth, aHeight);
  Result.Caption := aCaption;
  Result.Parent := aParent
end;

procedure CreateCheckBoxes(aParent: TWinControl; const aCaptions: array of string; var aCheckBoxes: array of TCheckBox);
var
  i, iLeft, iTop: Integer;
begin
  SetLength(aCheckBoxes, Length(aCaptions));
  iLeft := 10; iTop := 20;
  for i := 0 to Length(aCaptions) - 1 do begin
    aCheckBoxes[i] := CreateCheckBox(aParent, aCaptions[i], iLeft, iTop, aParent.Width - 20, 20);
    iTop := iTop + 25
  end
end;

var
  i: Integer;
  ParametersCheckBoxes: array of TCheckBox; 
  ParametersGroupBox: TGroupBox;
begin
  ParametersForm := TForm.Create(Application);
  try
    ParametersForm.ClientWidth := ciFormWidth;
    ParametersForm.ClientHeight := ciFormHeight;
    ParametersForm.Caption := 'Создание подкаст-лент'; 
    ParametersForm.Position := poScreenCenter;
    ParametersGroupBox := CreateGroupBox(ParametersForm, 'Параметры', 10, 10, ciFormWidth - 20, ciFormHeight - 55); 
    CreateButton(ParametersForm, 'OK', ciFormWidth - 170, ciFormHeight - 35, 75, 25, mrOK);
    CreateButton(ParametersForm, 'Отмена', ciFormWidth - 85, ciFormHeight - 35, 75, 25, mrCancel);;
    CreateCheckBoxes(ParametersGroupBox, ['Параметр 1', 'Параметр 2', 'Параметр 3', 'Параметр 4', 'Параметр 5'], ParametersCheckBoxes);
    if ParametersForm.ShowModal = mrOK then begin
      for i := 0 to Length(ParametersCheckBoxes) - 1 do
        if ParametersCheckBoxes[i].Checked then
          ShowMessage('Параметр ' + IntToStr(i + 1) + ' включен')
    end  
  finally
    ParametersForm.Free
  end
end.
Страница загрузки документации по FastScript - http://www.fast-report.com/ru/download/ ... nload.html
slawa321
Posts: 224
Joined: Wed Sep 14, 2011 12:06 pm

Re: Использование визуальных классов в скриптах (пример)

Post by slawa321 »

Мой вариант этого примера ,мне кажется короче.

Code: Select all

const
  ciFormWidth  = 300;
  ciFormHeight = 250;
  
var
  ParametersForm: TForm;
  i: Integer;
  ParametersCheckBoxes: array of TCheckBox; 
  ParametersGroupBox: TGroupBox;
  b1,b2:TButton;
  
function CreateControl(aControl,aParent: TControl; const aCaption: string; aLeft, aTop, aWidth, aHeight: Integer): Variant;
begin
if aControl=TForm then Result := TForm.Create(Application) else Result := aControl.Create(aParent);
  Result.SetBounds(aLeft, aTop, aWidth, aHeight);
  Result.Caption := aCaption;
  Result.Parent := aParent
end;

procedure CreateCheckBoxes(aParent: TWinControl; const aCaptions: array of string; var aCheckBoxes: array of TCheckBox);
var
  i, iLeft, iTop: Integer;
begin
  SetLength(aCheckBoxes, Length(aCaptions));
  iLeft := 10; iTop := 20;
  for i := 0 to Length(aCaptions) - 1 do begin
    aCheckBoxes[i] := CreateControl(TCheckBox,aParent, aCaptions[i], iLeft, iTop, aParent.Width - 20, 20);
    iTop := iTop + 25
  end
end;

procedure b1_cl();
 begin 
    for i := 0 to Length(ParametersCheckBoxes) - 1 do
    if ParametersCheckBoxes[i].Checked then
    ShowMessage('Параметр ' + IntToStr(i + 1) + ' включен')
 end;
 
begin

  try
    ParametersForm:= CreateControl(TForm,nil,'Создание подкаст-лент',0,0,ciFormWidth,ciFormHeight);
    ParametersForm.Position := poScreenCenter;
    ParametersGroupBox := CreateControl(TGroupBox,ParametersForm, 'Параметры', 10, 10, ciFormWidth - 30, ciFormHeight - 95); 
    b1:=CreateControl(TButton,ParametersForm, 'OK', ciFormWidth - 240, ciFormHeight - 75, 75, 25);
    b1.ModalResult:= mrOK ;
    b2:=CreateControl(TButton,ParametersForm, 'Отмена', ciFormWidth - 155, ciFormHeight - 75, 75, 25);
    b2.ModalResult:= mrCancel ;
    CreateCheckBoxes(ParametersGroupBox, ['Параметр 1', 'Параметр 2', 'Параметр 3', 'Параметр 4', 'Параметр 5'], ParametersCheckBoxes);
    b1.OnClick :=@b1_cl;
    ParametersForm.ShowModal;
  finally
    ParametersForm.Free
  end
end.
Для массивов контроллов тоже можно сделать общую функцию.
Моя мультимедия :Телевизор Samsung LE46D550K1, Телефон HTC HD2
Post Reply