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

问题描述 投票:0回答:2

我尝试将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毫米的短线。

上面的代码只是我的试错会话的一个快照。希望现在可以有人给我一些线索。

delphi fastreport
2个回答
2
投票

如果我正确理解您的问题,您至少需要考虑以下事项:

  • 首先,使用你的for循环,你创建了乐队,而不是线条。更改逻辑并创建带有作为所有者的对象(备忘录,线条,形状)。
  • 其次,对象的坐标和大小以像素为单位,因此您需要进行额外的计算。

对象的坐标和大小以像素为单位。由于所有对象的«Left,»«Top,»«Width,»和«Height»属性都具有«Extended»类型,因此您可以指出非整数值。定义了以下常量,用于将像素转换为厘米和英寸:

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
投票

这是我的最终解决方案:

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;
© www.soinside.com 2019 - 2024. All rights reserved.