如何获取 DrawText(); 的换行文本的新高度?

发布于 2024-11-16 18:21:40 字数 391 浏览 5 评论 0原文

代码示例

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

停滞 2024-11-23 18:21:40

绘制文本的高度为绘制文本

HeightOfText := DrawText(...

The height of the drawn text is the returned value of DrawText.

HeightOfText := DrawText(...
葮薆情 2024-11-23 18:21:40

如果您想在绘制文本之前更新矩形,您可以使用 DT_CALCRECT。然后,DrawText 会将矩形修改为新的高度(如果需要,还可以修改宽度)。如果您只需要高度,请使用 Andreas Rejbrand 所示的返回值。

这是一个示例:

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);';
  if DrawText(Canvas.Handle, PChar(s),  length(s), R, DT_CALCRECT or DT_WORDBREAK or DT_LEFT) <> 0 then
  begin
    DrawText(Canvas.Handle, PChar(s),  length(s), R, DT_WORDBREAK or DT_LEFT);
    r.Top := r.Bottom;
    r.Bottom := r.Bottom * 2;
    DrawText(Canvas.Handle, PChar(s),  length(s), R, DT_WORDBREAK or DT_LEFT);
  end;
end;

我建议阅读文档以获取更多详细信息:
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:

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);';
  if DrawText(Canvas.Handle, PChar(s),  length(s), R, DT_CALCRECT or DT_WORDBREAK or DT_LEFT) <> 0 then
  begin
    DrawText(Canvas.Handle, PChar(s),  length(s), R, DT_WORDBREAK or DT_LEFT);
    r.Top := r.Bottom;
    r.Bottom := r.Bottom * 2;
    DrawText(Canvas.Handle, PChar(s),  length(s), R, DT_WORDBREAK or DT_LEFT);
  end;
end;

I would recommend reading the docs for more details:
http://msdn.microsoft.com/en-us/library/dd162498(v=vs.85).aspx

擦肩而过的背影 2024-11-23 18:21:40

正如此处提到的 你可以通过调用 DrawText 函数与 DT_CALCRECT< /code> 标志指定了实际上不会绘制任何内容的内容;它只是计算适当的矩形并将其返回到变量R

procedure TForm1.Button1Click(Sender: TObject);
var
  R: TRect;
  S: String;
begin
  R := Rect(0, 0, 20, 20);
  S := 'What might be the new high of this text ?';
  DrawText(Canvas.Handle, PChar(S), Length(S), R, DT_WORDBREAK or DT_LEFT or DT_CALCRECT);
  ShowMessage('New height might be '+IntToStr(R.Bottom - R.Top)+' px');
end;

这意味着如果您使用以下示例调用它两次,您将绘制出换行的文本。这是因为第一次调用 DT_CALCRECT 计算矩形(并通过执行此操作修改 R 变量),第二次调用在修改后的矩形区域中绘制文本。

procedure TForm1.Button1Click(Sender: TObject);
var
  R: TRect;
  S: String;
begin
  R := Rect(0, 0, 20, 20);
  S := 'Some text which will be stoutly wrapped and painted :)';
  DrawText(Canvas.Handle, PChar(S), Length(S), R, DT_WORDBREAK or DT_LEFT or DT_CALCRECT);
  DrawText(Canvas.Handle, PChar(S), Length(S), R, DT_WORDBREAK or DT_LEFT);
end;

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 variable R.

procedure TForm1.Button1Click(Sender: TObject);
var
  R: TRect;
  S: String;
begin
  R := Rect(0, 0, 20, 20);
  S := 'What might be the new high of this text ?';
  DrawText(Canvas.Handle, PChar(S), Length(S), R, DT_WORDBREAK or DT_LEFT or DT_CALCRECT);
  ShowMessage('New height might be '+IntToStr(R.Bottom - R.Top)+' px');
end;

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 modify R variable by doing it) and the second call draws the text in that modified rectangle area.

procedure TForm1.Button1Click(Sender: TObject);
var
  R: TRect;
  S: String;
begin
  R := Rect(0, 0, 20, 20);
  S := 'Some text which will be stoutly wrapped and painted :)';
  DrawText(Canvas.Handle, PChar(S), Length(S), R, DT_WORDBREAK or DT_LEFT or DT_CALCRECT);
  DrawText(Canvas.Handle, PChar(S), Length(S), R, DT_WORDBREAK or DT_LEFT);
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文