FASTREPORT在报表脚本中动态添加对象

时间:2019-03-11 18:47:39

标签: delphi fastreport

我尝试将TLineView对象添加到报表中。 行数取决于报表数据集检索到的特定数量。

我已经将代码放入脚本初始化部分,并且在一个非常试验性的测试版本中,它看起来像这样:

var nol, i: integer;
child, newChild: TfrxChild;
noteLine1, noteLine2: TfrxLineView;
page: TfrxPage;                                 
begin
  page := ReportName;                                                                 
  nol := <DS_MAIN."VOLUME"> /2;
  nol := nol + <DS_MAIN."VOLUME"> mod 2;
  child3.child := TfrxChild.create(nil);
  newchild := child3.child;
  newChild.Visible := true;                                                        
  noteLine1 := TfrxLineView.create(newChild);
  noteLine1.name := 'nl1000';                                                
  noteLine1.Top := 0.73;
  noteLine1.Width := 7.5;
  noteLine1.Left := 3;
  noteLine1.Visible := true;                                                          
  noteLine1.Parent.Objects.Remove(noteLine1);                                                                                                     
  noteLine1.Parent.Objects.Add(noteLine1);                                                                                                     
//  newChild.Objects.Add(noteLine1);
  noteLine2 := TfrxLineView.create(newChild);
  noteLine2.name := 'nl1001';                                                
  noteLine2.Top := 0.73;
  noteLine2.Width := 7.5;
  noteLine2.Left := 11.2;
  newChild.Objects.Add(noteLine2);                                                                                          
  noteLine2.Visible := true;                                                          

  for i := 1 to nol do begin
    Child := TfrxChild.create(nil);
    NewChild.child := Child;
    newChild := child;                                            
  end;
end.

我没有得到两条并排的线,而是在它们之间留有间隙,而只得到一条短线,长度约为3-4毫米。

上面的代码只是我的反复试验环节的一个片段。 希望现在能有任何人给我一些线索。

2 个答案:

答案 0 :(得分:2)

如果我正确理解了您的问题,则需要至少考虑以下几点:

  • 首先,通过您的for循环,您可以创建乐队,而不是线条。更改逻辑并创建带区作为所有者的对象(备注,线条,形状)。
  • 第二,对象的坐标和大小以像素为单位设置,因此您需要进行其他计算。
  

对象的坐标和大小以像素为单位设置。自《左》以来   所有对象的“顶部”,“宽度”和“高度”属性都具有   «扩展»类型,您可以指出非整数值。下列   定义常数以将像素转换为厘米和   英寸:

     

fr01cm = 3.77953;
  fr1cm = 37.7953;
  fr01in = 9.6;
  fr1in = 96;

接下来是一个工作示例,该示例生成五个TfrxLineView对象。只需在您的表单上添加一个空报告并添加报告标题栏即可:

procedure TfrmMain.btnPreviewClick(Sender: TObject);
var
   nol, i: integer;
   left: Extended;
   band: TfrxReportTitle;
   line: TfrxLineView;
begin
   // Band
   band := (report.Report.FindObject('ReportBand') as TfrxReportTitle);
   // Lines generation
   left := 3;
   nol := 5;
   for i := 1 to nol do begin
      line := TfrxLineView.Create(band);
      line.CreateUniqueName;
      line.Top   := 0.73;
      line.Width := fr1cm * 2;
      line.Left  := left;
      left := left + line.Width + 30;
   end;
   // Report preview
   report.ShowReport(False);
end;

答案 1 :(得分:1)

这是我的最终解决方案:

procedure Child8OnBeforePrint(Sender: TfrxComponent);
var nol, i: integer;
left1, left2: extended;                               
child, newChild: TfrxChild;
noteLine1, noteLine2, line: TfrxLineView;
page: TfrxPage;
band: TfrxChild;                                   
begin
  nol := <DS_MAIN."VOLUME"> /2;
  nol := nol + <DS_MAIN."VOLUME"> mod 2;
   band := TfrxChild(TRP_ORDER_NOTE.FindObject('Child9'));
   // Lines generation
   left1 := 3*fr1cm;
   left2 := 11.2*fr1cm;
   for i := 1 to nol do begin
      line := TfrxLineView.Create(band);
      line.Name := 'noteLine'+intToStr(1+2*(i-1+trunc(random*1000000))); //Panic solution
      line.Top   := fr1cm*(0.73 + (i-1)*0.75);
      line.Width := 7.5*fr1cm;
      line.Left  := left1;
      if (<DS_MAIN."VOLUME"> mod 2 > 0 ) and (i = nol) then
        exit                                   
      else 
      begin
        line := TfrxLineView.Create(band);
        line.Name := 'noteLine'+intToStr(2*i+trunc(random*1000000));
        line.Top   := fr1cm*(0.73 + (i-1)*0.75);
        line.Width := 7.5*fr1cm;
        line.Left  := left2;
      end;                
   end;

end;
相关问题