如何在Delphi中获取RichEdit的文本范围 [英] How to get text extent of RichEdit in Delphi

查看:354
本文介绍了如何在Delphi中获取RichEdit的文本范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何像在TCanvas上使用TextWidth和TextHeight一样,在TRichEdit控件中获取文本的宽度和高度吗?

Does anyone know how to get the width and height of text in a TRichEdit control, in the same way that you would use TextWidth and TextHeight on a TCanvas?

我需要知道这样做的原因是我有一个不可见的窗体上的RichEdit,我可以使用Richedit.Perform(EM_FORMATRANGE,...)将其内容复制到画布上.问题是EM_FORMATRANGE需要一个TFormatRange类型的参数,在其中指定了目标rect,但是我不知道rect应该是什么,因为我事先不知道RichEdit中内容的大小.希望有道理.

The reason I need to know this doing this is I have a RichEdit on a non-visible form that I copy the contents of to a canvas using Richedit.Perform(EM_FORMATRANGE, ...). The problem is that the EM_FORMATRANGE requires a parameter of type TFormatRange in which the target rect is specified, but I don't know what the rect should be because I don't know in advance the size of the contents in the RichEdit. Hope that makes sense.

推荐答案

再次使用EM_FORMATRANGE进行测量,请参见

Again use EM_FORMATRANGE for measuring, see EM_FORMATRANGE Message on MSDN:

wParam 指定是否渲染 文本.如果此参数为非零 值,将呈现文本. 否则,将只测量文本.

wParam Specifies whether to render the text. If this parameter is a nonzero value, the text is rendered. Otherwise, the text is just measured.

通常,您已经有一个目标区域,该目标区域具有宽度和高度,您可以在其中进行绘图,例如在纸上打印或在预定义的表面上生成预览.对于获得所需高度的预定义宽度,最简单的示例可能是;

Generally you would already have a destination area, which has a width and height, where you'd do the drawing, like printing on a paper or producing a preview on a predefined surface. A most simple example for a predefined width to get the required height could be;

var
  Range: TFormatRange;
  Rect: TRect;
  LogX, LogY, SaveMapMode: Integer;
begin
  Range.hdc := ACanvas.Handle;
  Range.hdcTarget := ACanvas.Handle;

  LogX := GetDeviceCaps(Range.hdc, LOGPIXELSX);
  LogY := GetDeviceCaps(Range.hdc, LOGPIXELSY);

  Range.rc.Left := 0;
  Range.rc.Right := RichEdit1.ClientWidth * 1440 div LogX; // Any predefined width
  Range.rc.Top := 0;
  Range.rc.Bottom := Screen.Height * 1440 div LogY; // Some big number
  Range.rcPage := Range.rc;
  Range.chrg.cpMin := 0;
  Range.chrg.cpMax := -1;
  RichEdit1.Perform(EM_FORMATRANGE, 0, Longint(@Range));

  ShowMessage(IntToStr(Range.rc.Bottom * LogY div 1440)); // Get the height
  RichEdit1.Perform(EM_FORMATRANGE, 0, 0); // free cache


有关完整示例的信息,请参见


For a more complete example see this article, or in general any RichEdit previewing/printing code...

这篇关于如何在Delphi中获取RichEdit的文本范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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