问题
I am trying to replace all "#" with new lines to draw:
Canvas.TextOut(0,0,''+StringReplace('a#b','#',#13#10,[rfReplaceAll]));
but nothing.
TextOut prints "a#b" like the the replaced part doesn't even exist (But it does exist ofcourse): ab
Instead of this:
a
b
#13#10
is the new line (Windows) right?
Then why this isn't working?
Thank you.
回答1:
You need to use DrawText
to produce multi-line text:
var
R: TRect;
....
R := Rect(0, 0, Width, Height);
DrawText(
Canvas.Handle,
PChar(StringReplace('a#b','#',#13#10,[rfReplaceAll])),
-1,
R,
0
);
You may very well want to use different flags in the final parameter, but I'm sure you can read the documentation and work out what you need.
回答2:
Textout is basically a wrapper for Windows.ExtTextOut which doesn't support multiple lines. An alternative is to use DrawText:
var
r: TRect
s: string
begin
s := StringReplace('a#b','#',#13#10,[rfReplaceAll]);
r.Left := 10;
r.Top := 10;
DrawText(Canvas.Handle, PWideChar(s), Length(s), r, DT_NOPREFIX or DT_WORDBREAK);
end;
If you're using Delphi 7 then you will probably want to replace the PWideChar with PChar.
来源:https://stackoverflow.com/questions/10277324/delphi7-canvas-textout-cant-write-new-lines