How to get the PrintAreaRect from a RVPrint

General TRichView support forum. Please post your questions here
Post Reply
DaleC
Posts: 2
Joined: Thu Aug 30, 2012 8:08 am

How to get the PrintAreaRect from a RVPrint

Post by DaleC »

Hi there,

I use a TRVPrint on a form, and I would like to get the PrintAreaRect (to get the print area height and the print area width).
I don't want to use a preview or somthing visual.

Is there a RVPrint.GetPrintArea or somthing equivalent ?

Thanks in advance

DaleC
Sergey Tkachenko
Site Admin
Posts: 17557
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Do you mean page size excluding margins, in printer pixels?

Code: Select all

procedure GetPageSize(RVPrint: TRVPrint; 
                            var Width, Height: Integer); 
var DC: HDC; 
    phoX, phoY, phW, phH, lpy, lpx, LM, TM, RM, BM: Integer; 
begin 
  DC := RV_GetPrinterDC; // from PtblRV unit 

  Width  := GetDeviceCaps(DC, HORZRES); 
  Height := GetDeviceCaps(DC, VERTRES); 

  lpy := GetDeviceCaps(DC, LOGPIXELSY); 
  lpx := GetDeviceCaps(DC, LOGPIXELSX); 

  phoX := GetDeviceCaps(DC, PHYSICALOFFSETX); 
  phoY := GetDeviceCaps(DC, PHYSICALOFFSETY); 
  phW  := GetDeviceCaps(DC, PHYSICALWIDTH); 
  phH  := GetDeviceCaps(DC, PHYSICALHEIGHT); 

  LM := MulDiv(RVPrint.LeftMarginMM,   5*lpx, 127)- phoX; 
  TM := MulDiv(RVPrint.TopMarginMM,    5*lpy, 127)- phoY; 
  RM := MulDiv(RVPrint.RightMarginMM,  5*lpx, 127)- (phW-(phoX+Width)); 
  BM := MulDiv(RVPrint.BottomMarginMM, 5*lpy, 127)- (phH-(phoY+Height)); 

  if LM<0 then LM := 0; 
  if TM<0 then TM := 0; 
  if RM<0 then RM := 0; 
  if BM<0 then BM := 0; 

  dec(Width, LM+RM); 
  dec(Height, TM+BM); 

  DeleteDC(DC); 
end; 
DaleC
Posts: 2
Joined: Thu Aug 30, 2012 8:08 am

Post by DaleC »

In fact, I want to get the PrintAreaRect.

When you use the RVPrint.OnPrePaint event, one parameter is PrintAreaRect.. I don't know how it is calculated, but values are differents than the "GetPageSize" example procedure.

I need to find (calculate) this PrintAreaRect to use somewhere else, but I don't know how to get it. (I don't want to use the event).

Any idea ?

thanx
DaleC
Sergey Tkachenko
Site Admin
Posts: 17557
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

I cannot reproduce the case of different values.

My tests.

I used the demo in Demos\Delphi\Editors\Editor 1\

I added the menu item:

Code: Select all

procedure TForm1.ss1Click(Sender: TObject);
var Width, Height: Integer;
begin
  GetPageSize(RVPrint1, Width, Height);
  Caption := 'Calculated: '+IntToStr(Width)+','+IntToStr(Height);
end;
Also, I processed OnPagePostpaint event:

Code: Select all

procedure TForm1.RVPrint1PagePostpaint(Sender: TRVPrint; PageNo: Integer;
  Canvas: TCanvas; Preview: Boolean; PageRect, PrintAreaRect: TRect);
begin
  Caption := 'From event: '+IntToStr(PrintAreaRect.Right-PrintAreaRect.Left)+','+
    IntToStr(PrintAreaRect.Bottom-PrintAreaRect.Top);
end;
In the both cases, the same values are shown on the form caption: 4017,6072
Sergey Tkachenko
Site Admin
Posts: 17557
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Code: Select all

function GetPrintAreaRect(RVPrint: TRVPrint): TRect;
var DC: HDC;
    phoX, phoY, phW, phH, lpy, lpx, LM, TM, RM, BM, Width, Height: Integer;
begin
  DC := RV_GetPrinterDC; // from PtblRV unit

  Width  := GetDeviceCaps(DC, HORZRES);
  Height := GetDeviceCaps(DC, VERTRES);

  lpy := GetDeviceCaps(DC, LOGPIXELSY);
  lpx := GetDeviceCaps(DC, LOGPIXELSX);

  phoX := GetDeviceCaps(DC, PHYSICALOFFSETX);
  phoY := GetDeviceCaps(DC, PHYSICALOFFSETY);
  phW  := GetDeviceCaps(DC, PHYSICALWIDTH);
  phH  := GetDeviceCaps(DC, PHYSICALHEIGHT);

  LM := MulDiv(RVPrint.LeftMarginMM,   5*lpx, 127)- phoX;
  TM := MulDiv(RVPrint.TopMarginMM,    5*lpy, 127)- phoY;
  RM := MulDiv(RVPrint.RightMarginMM,  5*lpx, 127)- (phW-(phoX+Width));
  BM := MulDiv(RVPrint.BottomMarginMM, 5*lpy, 127)- (phH-(phoY+Height));

  if LM<0 then LM := 0;
  if TM<0 then TM := 0;
  if RM<0 then RM := 0;
  if BM<0 then BM := 0;

  dec(Width, LM+RM);
  dec(Height, TM+BM);

  Result := Bounds(LM, TM, Width, Height);

  DeleteDC(DC);
end;
Post Reply