bestlong 怕失憶論壇

 

 

搜索
bestlong 怕失憶論壇 論壇 Delphi FastReport 報表元件 FastReport 2 中文換行及亂碼問題
查看: 7563|回復: 2
go

FastReport 2 中文換行及亂碼問題 [複製鏈接]

Rank: 9Rank: 9Rank: 9

1#
發表於 2006-6-29 23:07 |只看該作者 |倒序瀏覽 |打印
主要改 fr_class.pas 中的 WrapLine 函數,其中有一段:
  1. else if s[last] = ' ' then
  2.   OutLine(Copy(s, beg, last - beg)) else
  3.   OutLine(Copy(s, beg, last - beg + 1)); //造成亂碼的根本原因
複製代碼

改為
  1. else if s[last] = ' ' then
  2.   OutLine(Copy(s, beg, last - beg)) else
  3.   if ByteType(s,last) =  mbLeadByte then //判斷是否是中文字
  4.     OutLine(Copy(s, beg, last - beg ))  
  5.   else
  6.     OutLine(Copy(s, beg, last - beg + 1));
複製代碼

後面的
  1. LoopPos := cur;
  2. beg := last + 1; last := beg;
複製代碼

要改為:
  1. LoopPos := cur;
  2. if ByteType(s,last) =  mbLeadByte then  
  3. begin
  4.   beg := last ; last := beg;
  5. end
  6. else
  7. begin
  8.   beg := last + 1; last := beg;
  9. end;
複製代碼


資料來源:http://www.delphibbs.com
我是雪龍
http://blog.bestlong.idv.tw
http://www.bestlong.idv.tw

Rank: 9Rank: 9Rank: 9

2#
發表於 2006-6-29 23:30 |只看該作者
我試過,fr245中中文折行仍有問題,我是這樣解決的。
1、在 WrapMemo 方法中的 WrapLine 程序中加入子程序(在變數宣告之後)
  1. procedure WrapLine(const s:String);
  2. var
  3.   i, cur, beg, last, LoopPos: Integer;
  4.   WasBreak, CRLF: Boolean;
  5.   //以下為加入部分
  6.   procedure AdjustLast;
  7.   var
  8.     ls: string;
  9.     ci,ni: Integer;
  10.   begin
  11.     ls := Copy(s, beg, last - beg + 1);
  12.     ci := 0;
  13.     for ni := 1 to Length(ls) do
  14.       if (ls[ni] >= #$A0) then Inc(ci);
  15.     ni := ci mod 2;
  16.     if (ni <> 0) and (ls[Length(ls)] >= #$A0) then
  17.       Dec(last);
  18.   end;
複製代碼

2.在最後一次調用OutLine的地方作如下修改:
  1. if WasBreak then
  2.   OutLine(Copy(s, beg, last - beg + 1) + '-')
  3. else if s[last] = ' ' then
  4.   OutLine(Copy(s, beg, last - beg))
  5. else begin
  6.   AdjustLast;  //此為新加的程序
  7.   OutLine(Copy(s, beg, last - beg + 1)); //在fr242中該行就是這樣,在fr245中原來沒有 + 1,但輸出後last卻減1,不知為何
  8. end;
複製代碼
我是雪龍
http://blog.bestlong.idv.tw
http://www.bestlong.idv.tw

Rank: 9Rank: 9Rank: 9

3#
發表於 2006-6-29 23:36 |只看該作者
  1. procedure WrapLine(const s: wideString);  //解決中文換行亂碼
  2.   var
  3.     i, cur, beg, last, LoopPos: Integer;
  4.     WasBreak, CRLF: Boolean;
  5.   begin
  6.     CRLF := False;
  7.     LoopPos := 0;
  8.     for i := 1 to Length(s) do
  9.       if (s[i] = #10) or (s[i] = #13) then
  10. //    if s[i] in [#10, #13] then
  11.       begin
  12.         CRLF := True;
  13.         break;
  14.       end;
  15.     last := 1; beg := 1;
  16.     if not CRLF and ((Length(s) <= 1) or (WCanvas.TextWidth(s) <= maxwidth)) then
  17.       OutLine(s + #1)
  18.     else
  19.     begin
  20.       cur := 1;
  21.       while cur <= Length(s) do
  22.       begin
  23.       if (s[cur] = #10) or (s[cur] = #13) then
  24. //      if s[cur] in [#10, #13] then
  25.         begin
  26.           OutLine(Copy(s, beg, cur - beg) + #1);
  27.           while (cur < Length(s)) and
  28.          ((s[cur] = #10) or (s[cur] = #13))
  29.         //  (s[cur] in [#10, #13])
  30.           do Inc(cur);
  31.           beg := cur; last := beg;
  32.       if (s[cur] = #10) or (s[cur] = #13) then
  33. //      if s[cur] in [#13, #10] then
  34.             Exit else
  35.             continue;
  36.         end;
  37.         if s[cur] <> ' ' then
  38.         if WCanvas.TextWidth(Copy(s, beg, cur - beg + 1)) > maxwidth then
  39.         begin
  40.           WasBreak := False;
  41.           if (Flags and flWordBreak) <> 0 then
  42.           begin
  43.             i := cur;
  44.             while (i <= Length(s)) and
  45.             not ((s[i] = ' ') or (s[i] = ' ') or (s[i] = '.') or (s[i] = ',') or(s[i] = '-')) do
  46.             //not (s[i] in spaces) do
  47.               Inc(i);
  48.             b := BreakWord(Copy(s, last + 1, i - last - 1));
  49.             if Length(b) > 0 then
  50.             begin
  51.               i := 1;
  52.               cur := last;
  53.               while (i <= Length(b)) and
  54.                 (WCanvas.TextWidth(Copy(s, beg, last - beg + 1 + Ord(b[i])) + '-') <= maxwidth) do
  55.               begin
  56.                 WasBreak := True;
  57.                 cur := last + Ord(b[i]);
  58.                 Inc(i);
  59.               end;
  60.               last := cur;
  61.             end;
  62.           end
  63.           else
  64.             if last = beg then last := cur;
  65.           if WasBreak then
  66.             OutLine(Copy(s, beg, last - beg + 1) + '-')
  67.           else if s[last] = ' ' then
  68.             OutLine(Copy(s, beg, last - beg)) else
  69.           begin
  70.             OutLine(Copy(s, beg, last - beg));
  71.             Dec(last);
  72.           end;
  73.           if ((Flags and flWordBreak) <> 0) and not WasBreak and (last = cur - 1) then
  74.             if LoopPos = cur then
  75.             begin
  76.               beg := cur + 1;
  77.               cur := Length(s);
  78.               break;
  79.             end
  80.             else
  81.               LoopPos := cur;
  82.           beg := last + 1; last := beg;
  83.         end;
  84. //      if s[cur] in spaces then last := cur;
  85.         if s[cur] = ' ' then last := cur;
  86.         Inc(cur);
  87.       end;
  88.       if beg <> cur then OutLine(Copy(s, beg, cur - beg + 1) + #1);
  89.     end;
  90.   end;
複製代碼
我是雪龍
http://blog.bestlong.idv.tw
http://www.bestlong.idv.tw
‹ 上一主題|下一主題

Archiver|怕失憶論壇

GMT+8, 2024-4-19 21:10 , Processed in 0.014365 second(s), 11 queries .

Powered by Discuz! X1.5

© 2001-2010 Comsenz Inc.