【问题标题】:FASTREPORT Adding objects dynamically in report scriptFASTREPORT 在报告脚本中动态添加对象
【发布时间】:2020-10-13 13:51:19
【问题描述】:

我尝试将 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


    【解决方案1】:

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

    • 使用for 循环可以创建带,而不是线条。您可以尝试更改逻辑并创建以乐队为所有者的对象(备忘录、线条、形状)。
    • 对象的坐标和大小以像素为单位设置,因此您需要额外计算。

    来自文档:

    对象的坐标和大小以像素为单位。自“左”以来, 所有对象的«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;
    

    【讨论】:

    • 这很有帮助,我已经有了一个文件报告,因此我使用了 FastReport 的脚本功能,所以情况略有不同。当然,我不需要为每一行创建一个孩子。那只是我以前的简单解决方案,我必须在一行中添加大量孩子。然后我让所有的孩子都看不见,然后,在一个循环中,将可见性提高到所需的数量。这不是一个令人满意的解决方案,所以我对这个新解决方案非常满意。但我不能在脚本中使用 CreateUniqueName 函数。谁能告诉我为什么?我用的是 FR5。
    • @larand 我正在使用 FR4.3,但我认为 CreateUniqueName 在报告中不是有效调用,仅在 Delphi 代码中。谢谢
    【解决方案2】:

    这是我的最终解决方案:

    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;
    

    【讨论】:

      猜你喜欢
      • 2015-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多