如果起点和终点的长度不够呢?楼主问题问得不严谨啊!
试试,中文和英文混合都可以
procedure TForm1.Button1Click(Sender: TObject);
const
MyTextRect :TRect = (Left: 10; Top: 10; Right: 210; Bottom: 210);
var
I, perWidth, nCount: Integer;
boLeadByte : Boolean;
sCurText : string;
function ConfimTextCount():Integer;
var
i: Integer;
begin
Result := 0;
for i := 1 to Length(Edit1.Text) do
begin
if boLeadByte then
begin
boLeadByte := False;
continue;
end;
boLeadByte := IsDBCSLeadByte(Ord(Edit1.Text));
Inc(Result);
end;
end;
begin
perWidth := (MyTextRect.Right - MyTextRect.Left) div ConfimTextCount();
if perWidth < Canvas.TextWidth('W') then
Raise Exception.Create('No enugh space');
Canvas.FillRect(MyTextRect);
boLeadByte := false;
nCount := 1;
for I := 1 to Length(Edit1.Text) do
begin
if boLeadByte then
begin
boLeadByte := False;
continue;
end;
boLeadByte := IsDBCSLeadByte(Ord(Edit1.Text));
sCurText := PChar(@Edit1.Text);
if not boLeadByte then
SetLength(sCurText, 1)
else
SetLength(sCurText, 2);
Canvas.TextOut(MyTextRect.Left + (nCount - 1) * perWidth,
MyTextRect.Top,
sCurText
);
Inc(nCount);
end;
end;