Board index » delphi » Draw text on printer canvas vs screen canvas

Draw text on printer canvas vs screen canvas

Hi,

I try to draw text to print. It work pretty good on a screen device context. But it doesn't work on a printer device context. My text is misaligned an truncated. Somebody can help me to correct it?

I use the code :

procedure MyForm.PrintContentNextPage(var aRectangle: TRect);
var
  sText         : string;
  aRect         : TRect;
  nWidthNeeded,
  f_PrintFromLine,
  nHeightNeeded : integer;
begin
    sText := 'I want to print this text with wordwrap and center alignment;';

    f_Canvas.Font.Size   := 18;
    f_Canvas.Font.Style  := [fsBold];
    f_Canvas.Font.Color  := clRed;

    aRect.Top     := 0;
    aRect.Left    := 0;
    aRect.Bottom  := aRectangle.Bottom - aRectangle.Top - 8;
    aRect.Right   := aRectangle.Right  - aRectangle.Left - 8;

    nHeightNeeded := Windows.DrawText(  f_Canvas.Handle,
                                PChar( sText), -1,
                                aRect,
                                DT_CALCRECT or DT_WORDBREAK);

    nWidthNeeded := aRect.Right - aRect.Left;
    aRect.Left := aRectangle.Left
                + ( (aRectangle.Right - aRectangle.Left - nWidthNeeded) DIV 2);
    aRect.Right := aRect.Left + nWidthNeeded;
    aRect.Top   := aRectangle.Top;
    aRect.Bottom:= aRect.Top + nHeightNeeded;

    Windows.DrawText( f_Canvas.Handle,
              PChar( sText), -1,
              aRect,
              DT_WORDBREAK);
  end;
end;

 

Re:Draw text on printer canvas vs screen canvas


Quote
Christian Rocher wrote:

> I try to draw text to print. It work pretty good on a screen device context. But it doesn't work on a printer device context. My text is misaligned an truncated. Somebody can help me to correct it?

Perhaps you could take a look at this:
http://www.efg2.com/Lab/OtherProjects/PrinterProjects.htm

Specifically the problems with using 'draw' on printers.

--
Charles Hacker
Lecturer in Electronics and Computing
School of Engineering
Griffith University - Gold Coast
Australia

Other Threads