0

Please Help me. I have this Delphi code.(Delphi 11.1) ShowMessage shows me 62. On the printed paper I see this:

XT is the band founded by Kristofer Steen, David Sandström,

from all lines in the RichEdit.

TEXT is the band founded by Kristofer Steen, David Sandström, Fredrik Bäckström and Jon F Brännström. All, except Bäckström, were ex-members of hardcore band Refused.[1] Stylistically, they have little in common with Refused. Their debut album, Text, is a mix of spoken word, music of various styles, and ambient sound effects, often producing an ethereal, avant-garde sound

But I have fmtRange.chrg.cpMin := 2; fmtRange.chrg.cpMax := 5; I have to get 4 characters. What is wrong here?

procedure TForm1.Button8Click(Sender: TObject);
Var
  printarea: Trect;
  richedit_outputarea: TRect;
  printresX, printresY: Integer;
  fmtRange: TFormatRange;
  nextChar : Integer;
Begin
  Printer.beginDoc;
  richedit1.Perform( EM_FORMATRANGE, 0, 0);
  try
    With Printer.Canvas Do
    Begin
      printresX := GetDeviceCaps( handle, LOGPIXELSX );
      printresY := GetDeviceCaps( handle, LOGPIXELSY );
      printarea := Rect( printresX,            // 1 inch left margin
                         printresY * 3 div 2,  // 1.5 inch top margin
                         Printer.PageWidth - printresX, // 1 inch right margin
                         Printer.PageHeight - printresY * 3 div 2 // 1.5 inch bottom margin
                       );
      richedit_outputarea := Rect( printarea.left * 1440 div printresX,
                                   printarea.top * 1440 div printresY,
                                   printarea.right * 1440 div printresX,
                                   printarea.bottom* 1440 div printresY );
      fmtRange.hDC := Handle;
      fmtRange.hdcTarget := fmtRange.hDC;
      fmtRange.rc := richedit_outputarea;
      fmtRange.rcPage := Rect( 0, 0,
                               Printer.PageWidth * 1440 div printresX,
                               Printer.PageHeight * 1440 div printresY );
      fmtRange.chrg.cpMin := 2;
      fmtRange.chrg.cpMax := 5;
      nextChar := richedit1.Perform( EM_FORMATRANGE, longint(1), longint(@fmtRange));
      showmessage(nextChar.ToString);
      richedit1.Perform( EM_FORMATRANGE, 0, 0);
    End;
  finally
    Printer.EndDoc;
  end;
end;
Alekperov
  • 1
  • 1
  • 1
  • 5
  • 1
    One thing I notice from [How do I print the contents of a rich text control?](https://devblogs.microsoft.com/oldnewthing/20070112-02/?p=28423) that is missing in your code is sending [`EM_SETTARGETDEVICE`](https://learn.microsoft.com/en-us/windows/win32/controls/em-settargetdevice) to the RichEdit before formatting it. – Remy Lebeau May 24 '22 at 14:58
  • On version 11.1, the Richedit component was broken. On version 10.4.2 everything works correctly. – Alekperov May 26 '22 at 07:25
  • "*the Richedit component was broken*" - can you elaborate? – Remy Lebeau May 26 '22 at 08:33
  • "But I have fmtRange.chrg.cpMin := 2; fmtRange.chrg.cpMax := 5; I have to get 4 characters." – Alekperov May 27 '22 at 08:10
  • And what is that suppose to prove about one version being broken but another is not? A RichEdit is an OS component, and `EM_FORMATRANGE` is an OS message. They operate the same way regardless of the Delphi version you are using. So there has to be something else going on. – Remy Lebeau May 27 '22 at 08:21
  • What else can happen if version 10.4.2 works, but 11.1 does not work? Have you read my entire post? You can check it yourself; – Alekperov May 27 '22 at 09:12
  • "*What else can happen if version 10.4.2 works, but 11.1 does not work?*" - the `TRichEdit` component was updated in Delphi 11 to use the Win32 RichEdit 4.1 control. And it has a new [`RenderRange()`](https://docwiki.embarcadero.com/Libraries/en/Vcl.ComCtrls.TCustomRichEdit.RenderRange) method. "*Have you read my entire post?*" - yes. "*You can check it yourself*" - actually, I can't, as I don't have those versions installed. – Remy Lebeau May 27 '22 at 15:43

0 Answers0