Delphi中如何获取RichEdit的文本范围

发布于 2024-09-09 01:22:56 字数 275 浏览 3 评论 0原文

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

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

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?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

蓝梦月影 2024-09-16 01:22:56

再次使用 EM_FORMATRANGE 进行测量,请参阅 EM_FORMATRANGE 消息 在 MSDN 上:

wParam 指定是否渲染
文本。如果该参数非零
值,文本被渲染。
否则,仅测量文本。

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

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

有关更完整的示例,请参阅 本文,或一般任何 RichEdit 预览/打印代码...

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

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

最近可好 2024-09-16 01:22:56

我从我的 Delphi LightSaber 库中提取了此代码。
当您向其添加行时,该组件会自行调整大小(而不是显示垂直滚动条)。您对 LineHeight 变量感兴趣:

procedure TRichEditResize.ResizeRichEdit;
var
  LineHeight, NewHeight: Integer;
  DC: HDC;
  TextMetric: TTextMetric;
  NumLines: Integer;
begin
  DC := GetDC(Handle);
  try
    // Get the text metrics, including the height of a line of text
    GetTextMetrics(DC, TextMetric);
    LineHeight := TextMetric.tmHeight- TextMetric.tmInternalLeading;
  finally
    // Release the device context
    ReleaseDC(Handle, DC);
  end;

  // Get the exact number of lines in the RichEdit
  NumLines := Perform(EM_LINEFROMCHAR, WPARAM(MaxInt), 0) + 1;

  NewHeight := LineHeight * NumLines;
  Height := NewHeight;
end;

I extracted this code from my Delphi LightSaber library.
The component resizes itself as you add lines to it (instead of showing the vert scrollbars). You are interested in LineHeight var:

procedure TRichEditResize.ResizeRichEdit;
var
  LineHeight, NewHeight: Integer;
  DC: HDC;
  TextMetric: TTextMetric;
  NumLines: Integer;
begin
  DC := GetDC(Handle);
  try
    // Get the text metrics, including the height of a line of text
    GetTextMetrics(DC, TextMetric);
    LineHeight := TextMetric.tmHeight- TextMetric.tmInternalLeading;
  finally
    // Release the device context
    ReleaseDC(Handle, DC);
  end;

  // Get the exact number of lines in the RichEdit
  NumLines := Perform(EM_LINEFROMCHAR, WPARAM(MaxInt), 0) + 1;

  NewHeight := LineHeight * NumLines;
  Height := NewHeight;
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文