Rendering with Delphi 11 a TRichEdit's contents on form.canvas differs in size on printer.canvas

55 Views Asked by At

In my application (Delphi 11, win 10) I render on an image canvas the content of a TRichEdit component with the following code.

procedure TmainForm.printRTF(RE : TRichEdit; aRect : TRect);// aRect calculated with ppi=127
var fmt : TFormatRange;
    res : integer;
begin
    // aCanvas is Image.canvas or printer.canvas
    // and aRect represents a frame in which I want to render the TRichEdit
    // and for the screen is calculated with ppi=127 to print in real size as on printer

    aCanvas.polygon(aRect); // It draws the exactly same frame both screen/printer 

    RE.lines.LoadFromFile(RTFfileName);
    with fmt do begin
        HDC := aCanvas.Handle;
        hdcTarget := HDC;

        if aCanvas = Image.canvas
        then res := pixelsPerInch
        else res := PrnResX; // printer's resolution
        rc.left := round(aRect.left*1440/res);
        rc.top  := round(aRect.top *1440/res);
        rc.right:= round(aRect.right*1440/res);
        rc.bottom := round(aRect.bottom*1440/res);
        rcPage := rc;
        chrg.cpMin := 0;
        chrg.cpMax := -1;
        SetBkMode(aCanvas.Handle, OPAQUE);
        RE.Perform(EM_FORMATRANGE, 0, integer(@fmt));

        RE.Perform(EM_FORMATRANGE, 1, integer(@fmt));

        RE.Perform(EM_FORMATRANGE, 0, 0);
    end;
end;

I measure on the screen the width and height of the text with the ruler and find, for example, W=140 mm, H=70 mm. I render to the printer canvas and has W=180mm, H=90mm. I display the same text in MsWord and it has W=180mm, H=90mm (with 130% zoom to show the actual size of an A4 page). I assume that TRichEdit renders its content with pixelsPerInch=96 (logical) resolution and not with ppi=127 (physical resolution) which the screen has. How can I force TRichEdit to show on the screen the same size as the printer?

0

There are 0 best solutions below