RichEdit printing customization

Source code sample needed:

Can anyone point me to a sorce code sample which demonstrates how to customize the RichEdit printing to add my own headers and footers to it?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Why don't you use Quickreport to print your RichEdit. It very flexible and the only limiet is your imagination !

The idea and solution is simple. I hope you have some basic experience in component writing,
In the vcl source directory you'll find a file called comctrls.pas. look for a function :

This is the one that does the printing.

What I would do is use the PageRect property of the TRichEdit to set it to the area which you want to let the richedit print its normal printing, and then you have some blank space for you.

To print on this blank space, you need to add a simple call to a function or event that passes on the canvas for the printer, and then you can do what you need.

Here's the function. Create a new component derived from TRichEdit, add a new event and a new function called PrintCustom. Use the code from Print and add the call to the event like I did here (in 2 places) :

procedure TCustomRichEdit.Print(const Caption: string);
  Range: TFormatRange;
  LastChar, MaxLen, LogX, LogY, OldMap: Integer;
  FillChar(Range, SizeOf(TFormatRange), 0);
  with Printer, Range do
    Title := Caption;
    hdc := Handle;
    hdcTarget := hdc;
    LogX := GetDeviceCaps(Handle, LOGPIXELSX);
    LogY := GetDeviceCaps(Handle, LOGPIXELSY);
    if IsRectEmpty(PageRect) then
      rc.right := PageWidth * 1440 div LogX;
      rc.bottom := PageHeight * 1440 div LogY;
    else begin
      rc.left := PageRect.Left * 1440 div LogX; := PageRect.Top * 1440 div LogY;
      rc.right := PageRect.Right * 1440 div LogX;
      rc.bottom := PageRect.Bottom * 1440 div LogY;
    rcPage := rc;
    LastChar := 0;
    MaxLen := GetTextLen;
    chrg.cpMax := -1;
    // ensure printer DC is in text map mode
    OldMap := SetMapMode(hdc, MM_TEXT);
    SendMessage(Handle, EM_FORMATRANGE, 0, 0);    // flush buffer
      // call you r event here (first page)
      MyPrintEvent(Canvas); // the canvas is Printer.Canvas
        chrg.cpMin := LastChar;
        LastChar := SendMessage(Self.Handle, EM_FORMATRANGE, 1, Longint(@Range));
        if (LastChar < MaxLen) and (LastChar <> -1) then
          // now for the new page
          MyPrintEvent(Canvas); // the canvas is Printer.Canvas
      until (LastChar >= MaxLen) or (LastChar = -1);
      SendMessage(Handle, EM_FORMATRANGE, 0, 0);  // flush buffer
      SetMapMode(hdc, OldMap);       // restore previous map mode

Hope it helps.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
skanadeAuthor Commented:

>>Why don't you use Quickreport to print your RichEdit.

Thanks! I thought QuickReport is only useful for databases and not for individual controls. I tried it but it doesn't work and the online help is hopelessly poor and missing for QRRichEdit component altogether. I tried putting the QRRichEdit on a detail band and connecting it to my RichEdit but it doesn't work. I tried it by itself without a band but it doesn't print full. Then I tried AutoStrech and it gives a GPF. Is there any better document somewhere?

skanadeAuthor Commented:

Thanks! This is exactly what I needed.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.