Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 344
  • Last Modified:

printing page x of y using a richedit

hi, is there a way to calculate and print "page x of y" using a trichedit?

i defines a region for the text so i have space to create either a footer or header. any way to do this?

thanks a lot

uri
0
urif
Asked:
urif
  • 8
  • 5
  • 4
  • +1
1 Solution
 
kretzschmarCommented:
best would be to use quickreport or
any other report-designer
0
 
urifAuthor Commented:
thx, but it has to be a simpler way, i mean, i only want to print the text i'm editing and etiher at the bottom or top of the page place a page number and filename that's all
0
 
kretzschmarCommented:
i don't think, that this you want will be easier
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
urifAuthor Commented:
it's just playing with the printer or richedit canvas. for example, this is what i use to print giving the page borders

procedure TForm1.Print1Click(Sender: TObject);
var presX, presY:integer;
aster,name:string;
j:integer;
begin
  if PrintDialog1.Execute then
  begin
    with ActiveRich do
    begin
      plaintext:=true;
      presX:=GetDeviceCaps(printer.handle,LOGPIXELSX);
      presY:=GetDeviceCaps(printer.handle,LOGPIXELSY);
      with pagerect do
      begin
        left:=presX div 2; //1 inch left margin //added div 2
        top:=3 * presY div 4;//1.5 inch top margin// changed from div2 to 4
        right:=Printer.PageWidth - 3 * presX div 4; //0.75 inch
        bottom:=Printer.PageHeight - presY; // 1 inch bottom
        Print(PageControl1.ActivePage.Caption);
      end;
    end;
  end;
end;
0
 
lopemCommented:
listening
0
 
urifAuthor Commented:
well, any ideas?
someone over there must be a guru on thr priter's canvas, right?
0
 
kretzschmarCommented:
>someone over there must be a guru on thr priter's canvas
definitivly this i'm not ;-)
0
 
urifAuthor Commented:
well, i guess i'll have to spend the rest of my life trying to figure out something that even notepad can do :(
0
 
raidosCommented:
Hmmm...well...this wouldn't be that difficult would it ?

Just write a function that iterates through the lines of text in the richedit, and decide if that line of text should be printed or not...if it should be printed then just Textout onto the printer canvas.. else just dont..

Geez, even I don't understand that.
Hope you do...i'll se if i can write a simple function for you sometime today

Regards
//raidos
0
 
urifAuthor Commented:
thx!

the only problem is that if i print thru the printer's canvas then the function to handle the borders and stuff won't work anymore (look at my example above)
0
 
raidosCommented:
I haven't tested this but it should work...maybe a little tweaking with aRange necessary..hope it does the trick...


Procedure PrintRichEdit(ARichEdit: TRichEdit; aRange: TPoint; Caption: String);
var
  Range: TFormatRange;
  LastChar, MaxLen, LogX, LogY, OldMap: Integer;
  SaveRect: TRect;
  PageCounter : Integer;
  UseRange : Boolean;
Procedure SetupPrinter;
Begin
  With Printer, Range Do Begin
    If OldMap <> 0 Then
      SetMapMode(hdc, OldMap);
    Title := Caption;
    BeginDoc;
    hdc := Handle;
    hdcTarget := hdc;
    LogX := GetDeviceCaps(Handle, LOGPIXELSX);
    LogY := GetDeviceCaps(Handle, LOGPIXELSY);
    if IsRectEmpty(aRichEdit.PageRect) then
    begin
      rc.right := PageWidth * 1440 div LogX;
      rc.bottom := PageHeight * 1440 div LogY;
    end
    else begin
      rc.left := aRichEdit.PageRect.Left * 1440 div LogX;
      rc.top := aRichEdit.PageRect.Top * 1440 div LogY;
      rc.right := aRichEdit.PageRect.Right * 1440 div LogX;
      rc.bottom := aRichEdit.PageRect.Bottom * 1440 div LogY;
    end;
    rcPage := rc;
    SaveRect := rc;
    LastChar := 0;
    MaxLen := aRichEdit.GetTextLen;
    chrg.cpMax := -1;
    // ensure printer DC is in text map mode
    OldMap := SetMapMode(hdc, MM_TEXT);
  End;
End;

begin
  UseRange := (aRange.Y > 0) And (aRange.X > 0) And (aRange.X <= aRange.Y);
  FillChar(Range, SizeOf(TFormatRange), 0);
  with Printer, Range do
  begin
    OldMap := 0;
    SetupPrinter;
    SendMessage(aRichEdit.Handle, EM_FORMATRANGE, 0, 0);    // flush buffer
    PageCounter := 1;
    try
      repeat
        rc := SaveRect;
        chrg.cpMin := LastChar;
        LastChar := SendMessage(aRichEdit.Handle, EM_FORMATRANGE, 1, Longint(@Range));
        if (LastChar < MaxLen) and (LastChar <> -1) then Begin
          If UseRange And (PageCounter < aRange.X) Then Begin
            Abort;
            SetupPrinter;
          End Else If (UseRange And (PageCounter < aRange.Y)) Or Not UseRange Then
            NewPage;
          Inc(PageCounter);
        End;
      until (LastChar >= MaxLen) or (LastChar = -1) Or (PageCounter >= aRange.Y);
      EndDoc;
    finally
      SendMessage(aRichEdit.Handle, EM_FORMATRANGE, 0, 0);  // flush buffer
      SetMapMode(hdc, OldMap);       // restore previous map mode
    end;
  end;
end;

Regards
//raidos
0
 
raidosCommented:
Doh...

Call the function like this
  PrintRichEdit(ActiveRich, Point(FirstPage, LastPage), 'YourTitle');

Regards again.. =)
0
 
raidosCommented:
Geez, i can't even remember what you asked for...*goddamnit* how to delete comments ?
0
 
kretzschmarCommented:
interesting code, raidos
0
 
urifAuthor Commented:
looks good, i'm going to try it now.
but where do you pass the code for the page number?
0
 
raidosCommented:
Urif, as i said in my last comment, the code provided is not really what you asked for...

sorry about that..

however if you read the code you'll see that on every new page it sets up the printer...and before it does the new page it could put the current page on the printers canvas i guess...and then you would only need a function to get the total pagecount..

kretzschmar, interesting code ? =)

Regards
//raidos
0
 
urifAuthor Commented:
ok, thanks for the code :)
0
 
urifAuthor Commented:
Raidos,

do you know what do i need to include in uses for TFormatRange?

i'm using delphi 5 enterprise... i can't seem to find it anywhere.

thx
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 8
  • 5
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now