How to get text extent of RichEdit in Delphi

落花浮王杯 提交于 2019-12-01 23:08:04
Sertac Akyuz

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!