如何以编程方式确定TMemo文本行的高度? [英] How do I determine the height of a line of text in a TMemo programmatically?

查看:341
本文介绍了如何以编程方式确定TMemo文本行的高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个TMemo,我想总是让它足够高以显示它包含的行数。不幸的是,我不太清楚如何计算。我不能将它从 .Font.Size 属性,因为这将根据DPI而有所不同。我不能使用 TCanvas.TextHeight ,因为TMemo似乎没有有画布。

I've got a TMemo, and I want to always make it exactly high enough to display the number of lines it contains. Unfortunately, I don't quite know how to calculate that. I can't base it off the .Font.Size property, because that will vary based on DPI. And I can't use TCanvas.TextHeight because TMemo doesn't seem to have a canvas.

任何人都知道如何做到这一点?

Anyone know how to do this right?

推荐答案

您需要使用TCanvas。您可以在后台创建一个TBitMap,并使用它的TCanvas(将Memo的Font属性分配给Bitmap.Canvas的一个),或者使用另一个组件的TCanvas。像这样:

You need to use a TCanvas for this. You can either create a TBitMap in the background and use its TCanvas (after assigning the Memo's Font property to the Bitmap.Canvas' one), or use a TCanvas from another component. Somthing like this:

BMP:=TBitMap.Create;
TRY
  BMP.Canvas.Font.Assign(Memo.Font);
  TotalHeight:=0;
  FOR LineNo:=1 TO Memo.Lines.Count DO INC(TotalHeight,BMP.Canvas.TextHeight(Memo.Lines[PRED(I)]))
FINALLY
  FreeAndNIL(BMP)
END;

编辑:

或者也许这一个:

BMP:=TBitMap.Create;
TRY
  BMP.Canvas.Font.Assign(Memo.Font);
  LineHeight:=BMP.Canvas.TextHeight('Wq');
  TotalHeight:=Memo.Lines.Count*LineHeight
FINALLY
  FreeAndNIL(BMP)
END;

这篇关于如何以编程方式确定TMemo文本行的高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆