Delphi7 Canvas.TextOut can't write new lines

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-12 03:09:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!