打印时总是漏打两个汉字,请高手帮忙看一看代码?
如下代码是功能是打印一段文字,到一定的数目字就换行打印,直至打印为至,我设最大的打印字符数是40,但打到31个字符就换行,但是换行后,竟不见了中间的四个字符(也就是两个汉字),请高手帮忙,我想知道如何控制栏宽,而不缺汉字
procedure TFrmBillPrint.QRDBText2Print(sender: TObject;
var Value: String);
var
bExit : boolean;
iLen : integer;
strSource, strTemp : string;
begin
if Length(Value) <= 40 then //40
begin
QRDBText16.AutoSize:=True;
exit;
end
else
begin
QRDBText16.AutoSize:=False;
QRDBText16.Height:=59;
QRDBText16.Width:=300; //264
end;
strSource := Value;
Value := ' ';
//防止截断汉字。
while true do
begin
iLen := 1;
while iLen < 40 do
begin
if ord(strSource[iLen]) > 128 then
inc(iLen, 2)
else
inc(iLen);
end;
dec(iLen);
strTemp := Copy(strSource, 1, iLen);
//加回车。
if Value = ' ' then
Value := strTemp
else
Value := Value + #13 + strTemp;
strSource := Copy(strSource, iLen + 1, Length(strSource) - iLen);
if Length(strSource) <= 40 then
begin
Value := Value + #13 + strSource;
exit;
end;
end;
end;
[解决办法]
函数写错了,
dec(iLen);//这里不能直接就dec
function AddEnter(OldStr: String; Count: Integer): String;
var
n: Integer;
s: String;
begin
Result := ' ';
s := OldStr;
while Length(s) > Count do
begin
n := 0;
while n < Count - 1 do
begin
Inc(n);
if Ord(s[n]) and $80 > 0 then
Inc(n);
end;
if (n = Count - 1) and (Ord(s[Count]) and $80 = 0) then
Inc(n);
Result := Result + Copy(s, 1, n) + #13;
Delete(s, 1, n);
end;
Result := Result + s;
end;
[解决办法]
procedure TFrmBillPrint.QRDBText2Print(sender: TObject;
var Value: String);
var
bExit : boolean;
iLen : integer;
strSource, strTemp : string;
begin
if Length(Value) <= 40 then //40
begin
QRDBText16.AutoSize:=True;
end
else
begin
QRDBText16.AutoSize:=False;
QRDBText16.Height:=59;
QRDBText16.Width:=300; //264
Value := AddEnter(Value, 40);
end;
end;