RV_GetPrinterDC [VCL and LCL]

<< Click to display table of contents >>

RV_GetPrinterDC [VCL and LCL]

Returns handle compatible with the the current printer object.

Unit PtblRV.

Syntax

function RV_GetPrinterDC: HDC;

After using this handle, delete it with DeleteDC.

Alternatively, you can use Printer.Handle.

Example

The procedure below returns size of document area on page, converted to “standard” pixels. Document area is paper size minus margins.

For conversion, it uses screen resolution (RichViewPixelsPerInch is ignored).

It does not take headers and footers into account.

procedure GetPageSize(RVPrint: TRVPrint; ARVStyle: TRVStyle;

  out Width, Height: TRVPixelLength); 

var

  DC: HDC; 

  phoX, phoY, phW, phH, lpy, lpx, LM, TM, RM, BM: Integer; 

begin 

  DC := RV_GetPrinterDC;

 

  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 := RV_UnitsToPixels(RVPrint.Margins.Left,

    RVPrint.Units, lpx)- phoX;

  TM := RV_UnitsToPixels(RVPrint.Margins.Top,

    RVPrint.Units, lpy)- phoY; 

  RM := RV_UnitsToPixels(RVPrint.Margins.Right,

    RVPrint.Units, lpx) - (phW - (phoX + Width)); 

  BM := RV_UnitsToPixels(RVPrint.Margins.Bottom,

    RVPrint.Units, lpy) - (phH - (phoY + Height)); 

 

  if RVPrint.FixMarginsMode=rvfmmAutoCorrect then

  begin

    if LM<0 then

    begin

      inc(RM, LM);

      LM := 0

    end;

    if TM<0 then

    begin

      inc(BM, TM);

      TM := 0

    end;

    if RM<0 then

      RM := 0

    if BM<0 then

      BM := 0

  end;

 

  dec(Width, LM+RM); 

  dec(Height, TM+BM); 

 

  DeleteDC(DC); 

 

  Width  := MulDiv(Width,  ARVStyle.UnitsPixelsPerInch, lpx); 

  Height := MulDiv(Height, ARVStyle.UnitsPixelsPerInch, lpy); 

end;

Example of using (making document width in editor equal to page width, taking difference in resolutions into account):

var w, h: TRVPixelLength

 

// rvoClientTextWidth must be excluded from RichViewEdit1.Options

 

GetPageSize(RVPrint1, w,h); 

RichViewEdit1.MaxTextWidth := 

  w - RichViewEdit1.LeftMargin - RichViewEdit1.RightMargin;

RichViewEdit1.MinTextWidth := RichViewEdit1.MaxTextWidth;

RichViewEdit1.Format;