- - * - WhiteUnicorn - * - -




* #WhiteUnicorn/ StartPage/ Documentation/DelphiFAQ >


Frequently Asked Questions

Getting the printing margin of the page

Question:

How can I get the printing margin of the page?

Answer:

Under Win16, call the Windows API function Escape(), passing the
constant GETPRINTINGOFFSET. Under Win32, simply call the Windows API
function GetDeviceCaps() passing the predefined constants
PHYSICALOFFSETX and PHYSICALOFFSETY. Since there is no guarantee a
given escape is supported, always call Escape with the QUERYESCSUPPORT
constant to make sure a given escape is implemented. The following
example returns the margin of a given printer under both WIN16 and
WIN32, accounting for the fact that not all printers will support the
escape code GETPRINTINGOFFSET (under WIN16). If this is the
case, the printing offset is approximated by getting the page size,
subtracting the physical resolution of the device, and then diving by
two.

Example:

uses Printers;

procedure TForm1.Button1Click(Sender: TObject);
var
  EscapeCode : integer;
  Margin : TPoint;
begin
  if PrintDialog1.Execute then begin
   {$IFDEF WIN32}
    Margin.x :=GetDeviceCaps(Printer.Handle, PHYSICALOFFSETX);
    Margin.y :=GetDeviceCaps(Printer.Handle, PHYSICALOFFSETY);
   {$ELSE}
    EscapeCode := GETPRINTINGOFFSET;
    if Escape(Printer.Handle,
              QUERYESCSUPPORT,
              sizeof(EscapeCode),
              @EscapeCode,
              nil) <> 0 then
      if Escape(Printer.Handle,
                GETPRINTINGOFFSET,
                0,
                nil,
                @Margin) < 1  then begin
       EscapeCode := GETPHYSPAGESIZE;
       if Escape(Printer.Handle,
                 QUERYESCSUPPORT,
                 sizeof(EscapeCode),
                 @EscapeCode,
                 nil) <> 0 then
        if Escape(Printer.Handle,
                  GETPHYSPAGESIZE,
                  0,
                  nil,
                  @Margin) > 0  then begin
          Margin.x := (Margin.x -
                       GetDeviceCaps(Printer.Handle, HorzRes)) div 2;
          Margin.y := (Margin.y -
                       GetDeviceCaps(Printer.Handle, VertRes)) div 2;
        end else begin
          Margin.x := 0;
          Margin.y := 0;
        end;
      end;
   {$ENDIF}
    Memo1.Lines.Add(IntToStr(Margin.x));
    Memo1.Lines.Add(IntToStr(Margin.y));
  end;
end;



* #WhiteUnicorn/ StartPage/ Documentation/DelphiFAQ >



- - * - Anastasija aka WhiteUnicorn - * - - LJLiveJournal
PFPhotoFile