Results 1 to 6 of 6

Thread: Afdrukken van een Form

  1. #1
    Senior Member
    Join Date
    Sep 2004
    Location
    BELGIE(Vilvoorde)
    Posts
    685

    Question Afdrukken van een Form

    CodeTyphon 7.1 + Windows 10.

    Onderstaande code werkt goed, maar ik zou graag de afdruk in het centrum van de A4 pagina krijgen.
    De print is "nu" in landscape, in de linker bovenhoek..
    Kan iemand mij de code doorgeven a.u.b.

    Code:
    procedure TForm1.BtnPRINTFORMClick(Sender: TObject);
    var
      myBitMap : TBitMap;
      rct: TRect;
      HScaleFactor, VScaleFactor: Single;
    begin
      if not PrintDialog1.Execute then Exit;
      HScaleFactor:=Printer.XDPI/Screen.PixelsPerInch;
      VScaleFactor:=Printer.YDPI/Screen.PixelsPerInch;
      myBitMap := TBitMap.Create;
      try
        myBitMap.LoadFromDevice(Canvas.Handle);
        rct:=Rect(0, 0, trunc(myBitMap.Width*HScaleFactor), trunc(myBitMap.Height*VScaleFactor));
        Printer.Refresh;
        Printer.Orientation:=poLandscape;
        Printer.BeginDoc;
        Printer.Canvas.StretchDraw(rct, myBitMap);
      finally
          Printer.EndDoc;
          myBitMap.Free;
      end;
    end;

  2. #2
    Ik denk zoiets:

    Delphi Code:
    1. procedure TForm1.BtnPRINTFORMClick(Sender: TObject);
    2. var
    3.   myBitMap : TBitMap;
    4.   rct: TRect;
    5.   HScaleFactor, VScaleFactor: Single;
    6.   BWidth, BHeight: Integer;
    7.   PWidth, PHeight: Integer;
    8.   RTop, RLeft, RBottom, RRight: Integer;
    9. begin
    10.   if not PrintDialog1.Execute then Exit;
    11.   HScaleFactor := Printer.XDPI / Screen.PixelsPerInch;
    12.   VScaleFactor := Printer.YDPI / Screen.PixelsPerInch;
    13.   myBitMap := TBitMap.Create;
    14.   try
    15.     myBitMap.LoadFromDevice(Canvas.Handle);
    16.     // Bitmap width / height
    17.     BWidth  := trunc(myBitMap.Width*HScaleFactor);
    18.     BHeight := trunc(myBitMap.Height*VScaleFactor);
    19.     // Printer page width / height
    20.     PWidth  := Printer.PageWidth;
    21.     PHeight := Printer.PageHeight;
    22.     // Calculate center of bitmap on printer canvas
    23.     RTop    := (PHeight div 2) - (BHeight div 2);
    24.     RLeft   := (PWidth div 2) - (BWidth div 2);
    25.     RBottom := RTop + BHeight;
    26.     RRight  := RLeft + BWidth;
    27.     // Set rect
    28.     rct := Rect(Rleft, RTop, RRight, RBottom);
    29.  
    30.     Printer.Refresh;
    31.     Printer.Orientation:=poLandscape;
    32.     Printer.BeginDoc;
    33.     Printer.Canvas.StretchDraw(rct, myBitMap);
    34.   finally
    35.       Printer.EndDoc;
    36.       myBitMap.Free;
    37.   end;

    Ps, Ik zie dat je uit Vilvoorde komt - ik woon in Aalst mocht je eens behoefte / zin hebben aan wat hulp, of een gezellige babbel - ben je altijd welkom
    Last edited by Reidinga; 06-May-20 at 14:38.

  3. #3
    Senior Member
    Join Date
    Sep 2004
    Location
    BELGIE(Vilvoorde)
    Posts
    685

    Question

    Bedankt voor uw code.

    Maar, de afdruk is nu links onderaan en niet centraal.

  4. #4
    Senior Member
    Join Date
    Sep 2004
    Location
    BELGIE(Vilvoorde)
    Posts
    685

    Question

    Als ik "Printer.Orientation:=poPortrait" gebruik, dan staat de bitmap wel in het midden v.d. pagina.
    Indien de afmetingen v.d. bitmat breder zijn dan de A4, dan vallen die buiten de pagina.

    Waarschijnlijk zijn er bijkomende parameters nodig om met "poLandscape" !?

    Reeds dank.

  5. #5
    Als je de regels:
    Code:
        Printer.Refresh;
        Printer.Orientation:=poLandscape;
    juist na de try verplaatst, worden de waarden van PWidth en PHeight juist berekend en komt de bitmap in het midden van de pagina.

    Verder zou ik
    Code:
        myBitMap := TBitMap.Create;
    binnen de try zetten, maar dat is een detail.

    Nu de bitmap in het centrum wordt geprint, blijft het probleem van een te grote bitmap over.
    Hiervoor kun je uit de math unit (bij de uses math toevoegen) de min en max functie gebruiken:
    Code:
        RTop    := (PHeight div 2) - (BHeight div 2);
        RLeft   := (PWidth div 2) - (BWidth div 2);
        RBottom := RTop + BHeight;
        RRight  := RLeft + BWidth;
    wordt dan:
    Code:
        RTop    := max(0, ((PHeight div 2) - (BHeight div 2)));
        RLeft   := max(0, ((PWidth div 2) - (BWidth div 2)));
        RBottom := min(PHeight, (RTop + BHeight));
        RRight  := min(PWidth, (RLeft + BWidth));
    Succes!

  6. #6
    Senior Member
    Join Date
    Sep 2004
    Location
    BELGIE(Vilvoorde)
    Posts
    685

    Thumbs up Print image of the Form in Landscape

    Hartelijke dank voor jullie code.
    Ik zal er van kunnen genieten!

    Deze code kan nog heel nuttig zijn voor andere amateur-programmeurs zoals ik.


    Hier de code in mijn programma:
    Code:
    procedure TForm1.BtnPRINTFORMClick(Sender: TObject);
    // Put "Math" in uses
    var
      myBitMap : TBitMap;
      rct: TRect;
      HScaleFactor, VScaleFactor: Single;
      BWidth, BHeight: Integer;
      PWidth, PHeight: Integer;
      RTop, RLeft, RBottom, RRight: Integer;
    begin
      if not PrintDialog1.Execute then Exit;
      HScaleFactor := Printer.XDPI / Screen.PixelsPerInch;
      VScaleFactor := Printer.YDPI / Screen.PixelsPerInch;
      try
        myBitMap := TBitMap.Create;
        Printer.Refresh;
        // Maximum Width / Height  of image (Formsize) = 910x510
        // else picture will not fit in page A4-Portrait.
        Printer.Orientation:= poLandscape;
        myBitMap.LoadFromDevice(Canvas.Handle);
        // Bitmap width / height
        BWidth  := trunc(myBitMap.Width*HScaleFactor);
        BHeight := trunc(myBitMap.Height*VScaleFactor);
        // Printer page width / height
        PWidth  := Printer.PageWidth;
        PHeight := Printer.PageHeight;
        // Calculate center of bitmap on printer canvas
        RTop    := max(0, ((PHeight div 2) - (BHeight div 2)));
        RLeft   := max(0, ((PWidth div 2) - (BWidth div 2)));
        RBottom := min(PHeight, (RTop + BHeight));
        RRight  := min(PWidth, (RLeft + BWidth));
        // Set rect
        rct := Rect(Rleft, RTop, RRight, RBottom);
        Printer.BeginDoc;
        Printer.Canvas.StretchDraw(rct, myBitMap);
      finally
        Printer.EndDoc;
        myBitMap.Free;
      end;
    end;

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •