【问题标题】:How to print TPanel contents?如何打印 TPanel 内容?
【发布时间】:2009-05-30 11:38:34
【问题描述】:

我有 TPanel。在这个面板上有一个 TImage 后代,很少有其他带有控件的面板等。实际上,图片包含一些图表,而在运行时创建了带有标签的其他面板,以便为用户提供额外的信息。
最近有人告诉我,如果可以打印此面板,并以它的形式出现在纸上,那就太好了。有什么线索,怎么办?

【问题讨论】:

    标签: delphi printing vcl delphi-5 tpanel


    【解决方案1】:

    我发现一个旧的usenet帖子提供了一个解决方案,将面板的内容复制到可以打印的位图:

    procedure TFormPrintWindows.ButtonPrintPanelClick(Sender: TObject);
      var
        Bitmap       :  TBitmap;
        FromLeft     :  INTEGER;
        FromTop      :  INTEGER;
        PrintedWidth :  INTEGER;
        PrintedHeight:  INTEGER;
    begin
      Printer.BeginDoc;
      TRY
        Bitmap := TBitmap.Create;
        TRY
          Bitmap.Width  := Panel1.Width;
          Bitmap.Height := Panel1.Height;
          Bitmap.PixelFormat := pf24bit;  // avoid palettes
    
          // Copy the Panel area from the Form into a separate Bitmap
          Bitmap.Canvas.CopyRect(Rect(0,0, Bitmap.Width,Bitmap.Height),
                                 FormPrintWindows.Canvas,
                                 Rect(Panel1.Left, Panel1.Top,
                                      Panel1.Left + Panel1.Width-1,
                                      Panel1.Top  + Panel1.Height-1) );
    
          // Assumes 10% left, right and top margin
          // Assumes bitmap aspect ratio > ~0.75 for portrait mode
          PrintedWidth  := MulDiv(Printer.PageWidth, 80,100);  // 80%
          PrintedHeight := MulDiv(PrintedWidth, Bitmap.Height, Bitmap.Width);
          FromLeft      := MulDiv(Printer.PageWidth, 10,100);  // 10%
          FromTop       := MulDiv(Printer.PageHeight,10,100);  // 10%
    
          PrintBitmap(Printer.Canvas,
            Rect(FromLeft, FromTop,
                 FromLeft + PrintedWidth,
                 FromTop  + PrintedHeight),
            Bitmap);
        FINALLY
          Bitmap.Free
        END;
      FINALLY
        Printer.EndDoc
      END
    
    end;
    

    并添加

    //Source of Code: 
    //http://www.swissdelphicenter.ch/torry/showcode.php?id=744
    //Which refers to a posting to borland.public.delphi.winapi by Rodney E Geraghty, 8/8/97.
    
    procedure PrintBitmap(Canvas: TCanvas; DestRect: TRect; Bitmap: TBitmap);
    var
      BitmapHeader: pBitmapInfo;
      BitmapImage: Pointer;
      HeaderSize: DWORD;
      ImageSize: DWORD;
    begin
      GetDIBSizes(Bitmap.Handle, HeaderSize, ImageSize);
      GetMem(BitmapHeader, HeaderSize);
      GetMem(BitmapImage, ImageSize);
      try
        GetDIB(Bitmap.Handle, Bitmap.Palette, BitmapHeader^, BitmapImage^);
        StretchDIBits(Canvas.Handle,
          DestRect.Left, DestRect.Top,    // Destination Origin
          DestRect.Right - DestRect.Left, // Destination Width
          DestRect.Bottom - DestRect.Top, // Destination Height
          0, 0,                           // Source Origin
          Bitmap.Width, Bitmap.Height,    // Source Width & Height
          BitmapImage,
          TBitmapInfo(BitmapHeader^),
          DIB_RGB_COLORS,
          SRCCOPY)
      finally
        FreeMem(BitmapHeader);
        FreeMem(BitmapImage)
      end
    end {PrintBitmap};
    

    【讨论】:

    • Birger,本着 SO 的精神,您应该编辑您的答案并添加 Robert Love 的答案中的功能(出于礼貌) - SO 的重点是存储正确的答案。
    • 感谢您向我指出。我更改了答案并添加了该功能。谢谢罗伯特!
    【解决方案2】:

    Birger 的代码示例中的 PrintBitmap 缺失,当您添加缺失的方法时它可以工作 嗯。

    //Source of Code: 
    //http://www.swissdelphicenter.ch/torry/showcode.php?id=744
    //Which refers to a posting to borland.public.delphi.winapi by Rodney E Geraghty, 8/8/97.
    
    procedure PrintBitmap(Canvas: TCanvas; DestRect: TRect; Bitmap: TBitmap);
    var
      BitmapHeader: pBitmapInfo;
      BitmapImage: Pointer;
      HeaderSize: DWORD;
      ImageSize: DWORD;
    begin
      GetDIBSizes(Bitmap.Handle, HeaderSize, ImageSize);
      GetMem(BitmapHeader, HeaderSize);
      GetMem(BitmapImage, ImageSize);
      try
        GetDIB(Bitmap.Handle, Bitmap.Palette, BitmapHeader^, BitmapImage^);
        StretchDIBits(Canvas.Handle,
          DestRect.Left, DestRect.Top,    // Destination Origin
          DestRect.Right - DestRect.Left, // Destination Width
          DestRect.Bottom - DestRect.Top, // Destination Height
          0, 0,                           // Source Origin
          Bitmap.Width, Bitmap.Height,    // Source Width & Height
          BitmapImage,
          TBitmapInfo(BitmapHeader^),
          DIB_RGB_COLORS,
          SRCCOPY)
      finally
        FreeMem(BitmapHeader);
        FreeMem(BitmapImage)
      end
    end {PrintBitmap};
    

    【讨论】:

    • 感谢您的附录!抱歉,我不能同时接受这两个答案,所以至少这对你来说是 +1!
    猜你喜欢
    • 2021-08-15
    • 1970-01-01
    • 2019-08-29
    • 2021-07-04
    • 2014-06-04
    • 2020-09-08
    • 2020-04-13
    • 2020-08-26
    • 1970-01-01
    相关资源
    最近更新 更多