Qreport关于中文换行有没有什么好的解决办法啊(100分)

  • 主题发起人 主题发起人 freeze06
  • 开始时间 开始时间
F

freeze06

Unregistered / Unconfirmed
GUEST, unregistred user!
大家有没有什么关于Qreport中字段的自动折行的好的解决方法啊……
AutoStretch属性好像不能识别中文字符,各位大侠是怎么解决的啊
 
procedure TQRFormNY.QRLabelPrint(sender: TObject;
var Value: String);
var
SenderWidth: integer;
Line, Part, Final: string;
Posbegin
, PosEnd: integer;
i, j: integer;
LastIsEng: boolean;
begin
SenderWidth := (Sender as TQRCustomLabel).Width;
Part := '';
Final := '';
Line := Value;
// 首先删除行内所有换行
while pos(#10, Line) > 0do
Delete(Line, Pos(#10, Line), 1);
// 增加一个回车以方便下面的处理.
if Line[Length(Line)] <> #13 then
Line := Line + #13;
//循环查找#13,如果找到则处理找到范围内的数据,一直到全部处理完毕
Posbegin
:= 0;
PosEnd := 0;
for i := 0 to Length (Line)do
begin
if Line = #13 then
begin
Posbegin
:= PosEnd + 1;
// 这样可以在处理下一段的时候方便地生成开始和结束位置.
PosEnd := i;
Part := '';
j := Posbegin
;
while j <= PosEnddo
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 QuickRepNY.TextWidth ((Sender as TQRCustomLabel).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;

 
hfghfghfg (2003-03-17 13:13:00)
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, QuickRpt, QRCtrls, ExtCtrls, StdCtrls;
type
TForm1 = class(TForm)
QuickRep1: TQuickRep;
TitleBand1: TQRBand;
QRLabel1: TQRLabel;
Button1: TButton;
procedure QRLabel1Print(sender: TObject;
var Value: string);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.QRLabel1Print(sender: TObject;
var Value: string);
var
str: widestring;
nowstr: string;
i: integer;
begin
self.Font := TQRLabel(sender).Font;
str := Value;
Value := '';
nowstr := '';
for i := 1 to length(str)do
begin
if Canvas.TextWidth(nowstr + str) > TQRLabel(sender).Width then
begin
nowstr := '';
Value := Value + #13;
nowstr := nowstr + str;
Value := Value + str;
end
else
begin
nowstr := nowstr + str;
Value := Value + str;
end;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
QuickRep1.Preview;
end;

end.


hfghfghfg (2003-03-17 13:16:00)
object Form1: TForm1
Left = 192
Top = 107
Width = 544
Height = 375
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
Scaled = False
PixelsPerInch = 96
TextHeight = 13
object QuickRep1: TQuickRep
Left = 160
Top = 32
Width = 794
Height = 1123
Frame.Color = clBlack
Frame.DrawTop = False
Frame.DrawBottom = False
Frame.DrawLeft = False
Frame.DrawRight = False
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -13
Font.Name = 'Arial'
Font.Style = []
Functions.Strings = (
'PAGENUMBER'
'COLUMNNUMBER'
'REPORTTITLE')
Functions.DATA = (
'0'
'0'
'''''')
Options = [FirstPageHeader, LastPageFooter]
Page.Columns = 1
Page.Orientation = poPortrait
Page.PaperSize = A4
Page.Values = (
100
2970
100
2100
100
100
0)
PrinterSettings.Copies = 1
PrinterSettings.Duplex = False
PrinterSettings.FirstPage = 0
PrinterSettings.LastPage = 0
PrinterSettings.OutputBin = Auto
PrintIfEmpty = True
SnapToGrid = True
Units = MM
Zoom = 100
object TitleBand1: TQRBand
Left = 38
Top = 38
Width = 718
Height = 203
Frame.Color = clBlack
Frame.DrawTop = False
Frame.DrawBottom = False
Frame.DrawLeft = False
Frame.DrawRight = False
AlignToBottom = False
Color = clWhite
ForceNewColumn = False
ForceNewPage = False
Size.Values = (
537.104166666667
1899.70833333333)
BandType = rbTitle
object QRLabel1: TQRLabel
Left = 32
Top = 0
Width = 89
Height = 185
Frame.Color = clBlack
Frame.DrawTop = False
Frame.DrawBottom = False
Frame.DrawLeft = False
Frame.DrawRight = False
Size.Values = (
489.479166666667
84.6666666666667
0
235.479166666667)
Alignment = taLeftJustify
AlignToBand = False
AutoSize = False
AutoStretch = False
Caption =
'QRLabel1QRLabel1QRLabel1QRLabel1QRLabel1QRLabel1QRLabel1QRLabel1' +
'QRLabel1QR哈哈哈哈哈哈哈哈哈哈Label1'
Color = clWhite
OnPrint = QRLabel1Print
Transparent = False
WordWrap = True
FontSize = 10
end
end
end
object Button1: TButton
Left = 40
Top = 32
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 1
OnClick = Button1Click
end
end

hfghfghfg (2003-03-17 13:23:00)
 
使用qrrichedit
 
////////////////////////////////////////////////////
// 功能:QRLabel的显示换行(报表用)
// 参数: 1.vLabel:要换行的QRLabel名
// 2.vValueStr:QRLabel的值(就是QRLabel的Caption)
// 3.vMaxLine:这个QRLabel一行最多能显示几个字节
// 4.vMaxLine:这个QRLabel最多能显示几行
// 5.vLabel_Top:QRLabel的初始Top值,因为报表在预览跟打印的时候都会
// 调用到这个过程,也就是QRLabel的Top改变了两次,所以不能在这个
// 过程里直接取得 QRLabel.Top,而必须传入原始Top
// 前提:必须把QRLabel的属性Transparent设为True
// 注意:在改变QRLabel大小(Width和Height)或字体(Font.Size)的时候
// 必须相应的改变vLineMaxSize和vMaxLine
// 缺陷:没有改变QRLabel的Height,所以才要把Transparent设为True
// 编写日期:2003/06/27
// 修改日期:2003/06/30(增加了第5个参数,具体描述看上面第5个参数的说明)
////////////////////////////////////////////////////
Procedure QRLabel_AutoSize(var vLabel: TQRLabel;
vValueStr: string;
vLineMaxSize,vMaxLine:word;vLabel_Top:Integer);
var
j,iCutLength{一行最多可以显示的字节数},iQRMLine{实际行数}: integer;
sLine,sCuted: string;
bIsDBCS{是否是汉字字节}: boolean;
QRMemo1:TQRMemo;
begin
QRMemo1:=TQRMemo.Create(Application);
sLine:=vValueStr;
QRMemo1.Lines.Clear;
While Length(Trim(sLine)) > 0do
begin
iCutLength:=vLineMaxSize;
sCuted := Copy(sLine, 1, iCutLength);//从头取出 iCutLength 长的字符串
bIsDBCS := False;//先假设没有半个字符串

for j := 1 to iCutLengthdo
//从头到尾逐个检查
begin
if bIsDBCS then
//如果上一个字节是汉字的前半部分
bIsDBCS := False //则此时本字节是汉字的后半部分,所以将是否前半个汉字检测标志设为假
else
if Windows.IsDBCSLeadByte(byte(sCuted[j])) then
bIsDBCS := True;//否则检查本字节,并根据结果设置标志
end;

//如果最后一个字节是汉字的前半部分, 则少截取一个字符,避免乱码
if bIsDBCS then
Dec(iCutLength);
QRMemo1.Lines.Add(Copy(sLine, 1, iCutLength));
sLine := Copy(sLine, iCutLength + 1, Length(sLine) - iCutLength);//取下一串字符串
end;

vLabel.Caption:=QRmemo1.Lines.Text;
//下面的代码处理Label的位置。
iQRMline:=QRMemo1.Lines.Count;
if iQRMline<vMaxLine then

vLabel.Top:=vLabel_Top+(((vLabel.Height div vMaxLine)*(vMaxLine-iQRMLine)) Div 2);
QRMemo1.Free;
end;
 
写加修改
用了好几天
真的有点舍不得
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=620364
 
多人接受答案了。
 
后退
顶部