因为QRlabel的长度固定,而内容和字体大小由用户决定,导致不能用字符串长度作为折行条件。问如何判断内容过长而调用我的折行函数??谢谢大虾指教……

解决方案 »

  1.   

    to gulf1234(小顾) :
    WordWrap属性对于中文内容无效,因为中文内容不存在空格,所以要判断加入空格达到折行目的。
    to wudi_1982:
    因为qrlabel固定长度,字体不定,用户很难判断第几个字折行。所以只能程序判断。另问:
    qrlabel长度固定,能判断内容长度是否超过qrlabel长度吗(即qrlabel能否显示所有内容)???
      

  2.   

    //换行
    procedure row_change(qr: tquickrep; sender: TQRCustomLabel; var Value:
      String); 
    var
      SenderWidth: integer;
      Line, Part, Final: string;
      PosBegin, PosEnd: integer;
      i, j: integer;
      LastIsEng: boolean;
    begin
      SenderWidth := Sender.Width;
      Part := '';
      Final := '';
      Line := Value;  // 首先删除行内所有换行
      while pos(#10, Line) > 0 do
        Delete(Line, Pos(#10, Line), 1);  // 增加一个回车以方便下面的处理.
      if Line[Length(Line)] <> #13 then Line := Line + #13;  //循环查找#13,如果找到则处理找到范围内的数据,一直到全部处理完毕
      PosBegin := 0;
      PosEnd := 0;
      for i := 1 to Length(Line) do
      begin
        if Line[i] = #13 then
        begin
          PosBegin := PosEnd + 1;
            // 这样可以在处理下一段的时候方便地生成开始和结束位置.
          PosEnd := i;      Part := '';
          j := PosBegin;
          while j <= PosEnd do
          begin
            if Line[j] < #128 then
            begin // an ascii character
              Part := Part + Line[j];
              j := j + 1;
              LastIsEng := true;
            end
            else
            begin // a DBCS character
              Part := Part + Line[j] + Line[j + 1];
              j := j + 2;
              LastIsEng := false;
            end;
            // 判断目前是否已经超长
            if qr.TextWidth(Sender.Font, part) > SenderWidth then
            begin
              // 需要剔除刚才的字符,
              if LastIsEng then
              begin
                Delete(Part, Length(Part), 1);
                j := j - 1;
              end
              else
              begin
                Delete(Part, Length(Part) - 1, 2);
                j := j - 2;
              end;
              //加入回车,
              Part := Part + #13;
              Final := Final + Part;
              Part := '';
            end;
          end;
          Final := Final + Part;
        end;
      end;
      // 删除刚刚加入的回车
      if Final[Length(Final)] = #13 then
        Delete(Final, Length(Final), 1);
      Value := Final;
    end;把上面的过程放到QRDBTEXT、QRDBLABEL或QRDBMEMO的ONPINT事件中
    如:
    procedure TListRep_SDForm_PL.QRDBTxtMake_KYPrint(sender: TObject;
      var Value: String);
    begin
        sysdb.row_change(qr_AB, Sender as TQRCustomLabel , Value );
    end;