PrintPreview vs. TMemo (and bug in TPrintPreview)

Please post bug reports, feature requests, or any question regarding the DELPHI AREA projects here.

PrintPreview vs. TMemo (and bug in TPrintPreview)

Postby Johnny_Bit » June 15th, 2003, 8:33 pm

Hi @ll

I've tried to make print prevew of memo by richedit, but nothing shows on preview.

My question: How make PrintPreview of TMemo including margins, footer and header of page?
Last edited by Johnny_Bit on June 18th, 2003, 5:49 pm, edited 1 time in total.
Johnny_Bit
VIP Member
VIP Member
 
Posts: 455
Joined: June 15th, 2003, 9:56 am

Printing a TMemo

Postby richardchaven » June 17th, 2003, 7:02 am

Code: Select all
NewPage;

Canvas.DrawRect(..., WordWrap);


I'm really tired, but does this help at all?
richardchaven
Active Member
Active Member
 
Posts: 20
Joined: May 30th, 2003, 1:30 pm

Postby Johnny_Bit » June 17th, 2003, 2:55 pm

well, I don't know :-( I've got this code for printing on printer canvas, but i want to change it so it be compatible with PrintPreview:
Code: Select all
procedure TMainForm.PrintFooter(var R: TRect; LineHeight: Integer);
var
  S : String;
begin
  with Printer do begin

    { utwórz łańcuch zawierający numer strony }
    S := Format('Page %d', [PageNumber]);


    { ustal współrzędne prostokąta, którym wyświetlany będzie łańcuch }
    { zawierający numer strony }

    R.Top := PageHeight - (lineHeight * 2);
    R.Bottom := R.Top + lineHeight;


    { wyświetl centrycznie łańcuch zawierający numer strony }
    DrawText(Handle, PChar(S), -1, R, DT_CENTER);

    { podkreśl tekst zawierający numer strony }
    Canvas.MoveTo(0, R.Top - 2);
    Canvas.LineTo(R.Right, R.Top - 2);
  end;
end;

procedure TMainForm.FilePrintClick(Sender: TObject);
var
  I            : Integer;
  LineHeight   : Integer;
  LinesPerPage : Integer;
  LineCount    : Integer;
  R            : TRect;
  S            : string;
begin
  { wyświetl dialog drukowania }
  if PrintDialog.Execute then begin

    { ustal tytuł drukowanego obiektu }
    Printer.Title := 'Scratch - ' + OpenDialog.FileName;

    { ustaw czcionkę drukarkową zgodnie z czcionką Memo }
    Printer.Canvas.Font := Memo.Font;



    { Oblicz wysokość linii - na podstawie rozmiaru czcionki }
    { i rozdzielczości drukarki. Dodaj 40% na odstęp między liniami }

    LineHeight := Abs(
      MulDiv(Printer.Canvas.Font.Size,
      GetDeviceCaps(Printer.Handle, LOGPIXELSY), 72));
    Inc(LineHeight, (LineHeight * 4) div 10);

    { oblicz, ile linii zmieści się na stronie; uwzględnij }
    { trójliniowy margines                                 }

    LinesPerPage := (Printer.PageHeight div lineHeight) - 4;

    { rozpocznij drukowanie w 4. linii, by zostawić miejsce }
    { na nagłówek }
    LineCount := 4;

    { wydrukuj nagłówek }
    Printer.BeginDoc;
    R.Top    := LineHeight;
    R.Left   := 20;
    R.Right  := Printer.PageWidth;
    R.Bottom := LineHeight * 2;
    DrawText(Printer.Handle,
      PChar(OpenDialog.FileName), -1, R, DT_CENTER);

    { drukowanie poszczególnych linii }
    for I := 0 to Pred(Memo.Lines.Count) do
    begin

      { po dojściu do końca strony zresetuj licznik linii }
      { i przejdź do nowej strony                         }

      Inc(LineCount);
      if LineCount = LinesPerPage then begin
        PrintFooter(R, LineHeight);
        LineCount := 4;
        Printer.NewPage;
      end;

      { pobierz kolejną linię Memo }
      S := Memo.Lines.Strings[I];
      Printer.Canvas.TextOut(0, LineCount * LineHeight, S);
    end;

    { zakończ drukowanie }
    PrintFooter(R, LineHeight);
    Printer.EndDoc;
  end;
end;


(comments are in Polish, don't care 'bout them)
Johnny_Bit
VIP Member
VIP Member
 
Posts: 455
Joined: June 15th, 2003, 9:56 am

Postby richardchaven » June 17th, 2003, 3:37 pm

Looking at the Preview.pas source code, I think if you do not use PrintPreview.Canvas, it will not show up.

Replace 'Printer.' with 'PrintPreview.' and 'Printer.BeginDoc' with 'PrintPreview.NewPage' and see what happens.

Cheers
richardchaven
Active Member
Active Member
 
Posts: 20
Joined: May 30th, 2003, 1:30 pm

Postby Johnny_Bit » June 17th, 2003, 4:01 pm

I did so, but page widith and height are incorrect. also DPI.
Johnny_Bit
VIP Member
VIP Member
 
Posts: 455
Joined: June 15th, 2003, 9:56 am

Postby richardchaven » June 18th, 2003, 6:31 am

PageHeight and PageWidth are in the .Units measure, not necessarily Pixels. You can use ConvertUnit or change the .Units property to mmPixels

Cheers
richardchaven
Active Member
Active Member
 
Posts: 20
Joined: May 30th, 2003, 1:30 pm

Postby Johnny_Bit » June 18th, 2003, 5:46 pm

Thanks, but I found solution just few minutes after disconect (by RichEdit way), but I've found bug in TPrintPreview: when I tried to change zoom to 100% then ecteption EOutOfResources raised, when I chenged Zoom to 50% it looked like zoom 500% or more! whats wrong 'bout it? [it hepend not only in my program, aslo in RichEdit print demo but it doesn't hapend in General demo.

And one more thing: why pages can't be scroled like in M$ word or Adobe Acrobat Reader(next page under previous)?
Johnny_Bit
VIP Member
VIP Member
 
Posts: 455
Joined: June 15th, 2003, 9:56 am

Postby richardchaven » June 18th, 2003, 6:17 pm

Look in their demo for paging: you have to set the CurrentPage property
richardchaven
Active Member
Active Member
 
Posts: 20
Joined: May 30th, 2003, 1:30 pm

Postby Johnny_Bit » June 19th, 2003, 9:33 am

OK, but what 'bout bug in zoom?

ps. CurrentPage sucks!
Johnny_Bit
VIP Member
VIP Member
 
Posts: 455
Joined: June 15th, 2003, 9:56 am

Postby richardchaven » June 19th, 2003, 3:39 pm

I don't know why you would get OutOfResources; I think you need to email the vendor so they take a look at it.

Why does .CurrentPage suck? If they have one canvas, how elese are they supposed to keep track?
richardchaven
Active Member
Active Member
 
Posts: 20
Joined: May 30th, 2003, 1:30 pm

Postby Johnny_Bit » June 19th, 2003, 6:46 pm

I don't know what you mean by sending mail to vendor (exacly this part. mail to vendor of what?), besides try out the RichText print demo and change zoom to 100%

It might be caused by MulDiv (in UpdateZoom procedure)...

It's strange but when i changed zoom to 8 it looks like 100% in Word or Acrobat. and i can bet that it's bug in Update zoom
Johnny_Bit
VIP Member
VIP Member
 
Posts: 455
Joined: June 15th, 2003, 9:56 am

Postby Johnny_Bit » June 21st, 2003, 2:16 pm

I found finally reason of bug! It was in CalculateViewSize function, in zsZoomOther section, whole function should be:
Code: Select all
function TPrintPreview.CalculateViewSize(const Space: TPoint): TPoint;
begin
  with FPaperView do
    case FZoomState of
      zsZoomOther:
      begin
        Result.X := ActualWidth(MulDiv(FPixels.X, FZoom*Screen.PixelsPerInch,GetDeviceCaps(Printer.Handle, LOGPIXELSX) * 100));{ActualWidth(MulDiv(FPixels.X, FZoom, 100));}
        Result.Y := ActualWidth(MulDiv(FPixels.Y, FZoom*Screen.PixelsPerInch,GetDeviceCaps(Printer.Handle, LOGPIXELSY) * 100));{ActualHeight(MulDiv(FPixels.Y, FZoom, 100));}
      end;
      zsZoomToWidth:
      begin
        Result.X := Space.X;
        Result.Y := ActualHeight(MulDiv(ActualWidth(Result.X), FPixels.Y, FPixels.X));
      end;
      zsZoomToHeight:
      begin
        Result.Y := Space.Y;
        Result.X := ActualWidth(MulDiv(ActualHeight(Result.Y), FPixels.X, FPixels.Y));
      end;
      zsZoomToFit:
      begin
        if (FPixels.Y / FPixels.X) < (Space.Y / Space.X) then
        begin
          Result.X := Space.X;
          Result.Y := ActualHeight(MulDiv(ActualWidth(Result.X), FPixels.Y, FPixels.X));
        end
        else
        begin
          Result.Y := Space.Y;
          Result.X := ActualWidth(MulDiv(ActualHeight(Result.Y), FPixels.X, FPixels.Y));
        end;
      end;
    end;
end;
Johnny_Bit
VIP Member
VIP Member
 
Posts: 455
Joined: June 15th, 2003, 9:56 am

Postby richardchaven » June 24th, 2003, 5:59 pm

Please summarize the changes you made.
richardchaven
Active Member
Active Member
 
Posts: 20
Joined: May 30th, 2003, 1:30 pm

Postby Johnny_Bit » June 24th, 2003, 7:02 pm

orginal code is in comment. changes ware maded only for
Code: Select all
Result.X := ActualWidth(MulDiv(FPixels.X, FZoom*Screen.PixelsPerInch,GetDeviceCaps(Printer.Handle, LOGPIXELSX) * 100));{ActualWidth(MulDiv(FPixels.X, FZoom, 100));<-orginal}
        Result.Y := ActualWidth(MulDiv(FPixels.Y, FZoom*Screen.PixelsPerInch,GetDeviceCaps(Printer.Handle, LOGPIXELSY) * 100));{ActualHeight(MulDiv(FPixels.Y, FZoom, 100));<-orginal}


it was in lines 1767 and 1768 of unit preview.pas in function CalculateViewSize (protected function of TPrintPreview). Is it enought?
Johnny_Bit
VIP Member
VIP Member
 
Posts: 455
Joined: June 15th, 2003, 9:56 am

Postby richardchaven » June 25th, 2003, 12:15 am

Thank you
richardchaven
Active Member
Active Member
 
Posts: 20
Joined: May 30th, 2003, 1:30 pm

Next

Return to DELPHI AREA Projects

Who is online

Users browsing this forum: No registered users and 34 guests

cron