如何获取 DrawText(); 的换行文本的新高度?
代码示例
procedure TForm1.Button1Click(Sender: TObject);
var
r: Trect;
s: String;
begin
R := Rect(0,0, 300, 100);
s := 'WordWrapTextOut(TargetCanvas: TCanvas; var x, y: integer; S: string; maxwidth, lineheight: integer);';
DrawText(Canvas.Handle, PChar(s), length(s), R, DT_WORDBREAK or DT_LEFT);
end;
我想将文本换行为 300px 宽度,但如何获得新的高度?有什么办法或者解决办法吗?
code sample
procedure TForm1.Button1Click(Sender: TObject);
var
r: Trect;
s: String;
begin
R := Rect(0,0, 300, 100);
s := 'WordWrapTextOut(TargetCanvas: TCanvas; var x, y: integer; S: string; maxwidth, lineheight: integer);';
DrawText(Canvas.Handle, PChar(s), length(s), R, DT_WORDBREAK or DT_LEFT);
end;
I want to wrap the text in 300px width but how can I get the new Height? Is there a way or any solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
绘制文本的高度为
绘制文本
。The height of the drawn text is the returned value of
DrawText
.如果您想在绘制文本之前更新矩形,您可以使用 DT_CALCRECT。然后,DrawText 会将矩形修改为新的高度(如果需要,还可以修改宽度)。如果您只需要高度,请使用 Andreas Rejbrand 所示的返回值。
这是一个示例:
我建议阅读文档以获取更多详细信息:
http://msdn.microsoft.com/en-我们/库/dd162498(v=vs.85).aspx
If you want to update your rectangle before drawing the text you could use DT_CALCRECT. DrawText will then modify your rectangle to the new height (and width if necessary). If you only need the height though use the return value as Andreas Rejbrand showed.
Here's a sample of this:
I would recommend reading the docs for more details:
http://msdn.microsoft.com/en-us/library/dd162498(v=vs.85).aspx
正如此处提到的 你可以通过调用 DrawText 函数与
DT_CALCRECT< /code> 标志指定了实际上不会绘制任何内容的内容;它只是计算适当的矩形并将其返回到变量
R
。这意味着如果您使用以下示例调用它两次,您将绘制出换行的文本。这是因为第一次调用 DT_CALCRECT 计算矩形(并通过执行此操作修改 R 变量),第二次调用在修改后的矩形区域中绘制文本。
As was mentioned here you can get it by calling DrawText function with
DT_CALCRECT
flag specified what actually won't paint anything; it just calculates appropriate rectangle and returns it to variableR
.What means if you call it twice using the following example, you'll get drawn the wrapped text. It's because the first call with
DT_CALCRECT
calculates the rectangle (and modifyR
variable by doing it) and the second call draws the text in that modified rectangle area.