我有一个TMemo,我想总是让它足够高,以显示它包含的行数.不幸的是,我不太清楚如何计算.我不能将它从.Font.Size房产中取出,因为这会因DPI而异.我不能使用TCanvas.TextHeight,因为TMemo似乎并不具备的画布.
有谁知道怎么做对吗?
我看到一个问题,我认为TMemo上的所有线条在高度上都相同,但有些线条可能是空的......
因此,当它们在TMemo上不是零高度时,获得空的高度将给出零.
所以解决方案可能会像Memo.Lines.Count*LineHeight那样
请注意,Canvas.TextHeight可能无法获取Lineheight,因为Canvas.TextHeight会为文本提供或多或少精确的最小高度...我的意思是它不会为文本'ABC'提供与'ABCp'相同的高度等等......
我建议(如果不想调用Windows API)使用Font.Height,但如果是负数,则不测量每行的内部前导...
所以我建议你做下一步(测试):
我个人在TForm OnCreate事件上执行此操作(以确保它只执行一次),只是为了确保不会更改可视字体大小,并且MyMemo.Font.Height包含每行的内部前导:
MyMemo.Font.Height:=Abs(MyMemo.Font.Size*MyMemo.Font.PixelsPerInch div Screen.PixelsPerInch);
Run Code Online (Sandbox Code Playgroud)
确保以前只做一次,否则文本大小会越来越大,就像你运行它的次数一样......这是因为不是总是MyMemo.Font.PixelsPerInch等于Screen.PixelsPerInch ......在我的情况下,他们分别是80和96.
然后,当我需要知道行高时,我只需使用:
Abs(MyMemo.Font.Height*Screen.PixelsPerInch div 72)
Run Code Online (Sandbox Code Playgroud)
这给出了一条TMemo线的精确高度,因为所有线都有相同的高度,总高度为:
MyMemo.Lines.Count*Abs(MyMemo.Font.Height*Screen.PixelsPerInch div 72)
Run Code Online (Sandbox Code Playgroud)
因此,为了使TMemo高度与其包含的文本一样大,我这样做(阅读每行的注释,它们非常重要):
MyMemo.Font.Height:=Abs(MyMemo.Font.Size*MyMemo.Font.PixelsPerInch div Screen.PixelsPerInch); // I do this on the Tform OnCreate event, to ensure only done once
MyMemo.Height:=1+MyMemo.Lines.Count*Abs(MyMemo.Font.Height*Screen.PixelsPerInch div 72); // I do this anywhere after adding the text and/or after editing it
Run Code Online (Sandbox Code Playgroud)
我不想玩Screen.PixelsPerInch你可以这样做(阅读每行的注释,它们非常重要):
MyMemo.Font.Height:=Abs(MyMemo.Font.Height); // This may make text size to visually change, that was why i use the corrector by using MyMemo.Font.PixelsPerInch and Screen.PixelsPerInch
MyMemo.Height:=1+MyMemo.Lines.Count*Abs(MyMemo.Font.Height*MyMemo.Font.PixelsPerInch div 72);
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助任何人.
| 归档时间: |
|
| 查看次数: |
8075 次 |
| 最近记录: |