delphi - 如何打印TPanel内容?

标签 delphi printing vcl delphi-5 tpanel

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

最佳答案

我发现一个旧的 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};

关于delphi - 如何打印TPanel内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/929589/

相关文章:

delphi - 计算直到圣诞节的天数

delphi - 如何防止大量的 OutputDebugString() 调用导致 Delphi 6 IDE 中的应用程序降级?

delphi - 如何将接口(interface)添加到尚未包含 TInterfacedObject 的类层次结构中?

java - 在 Windows 中找不到使用 java 的打印服务

PostgreSQL VCL 控件

c++ - 如何为动态创建的按钮编写函数 Click()?

delphi - 如何为 Delphi Synapse HTTP Server 添加 SOAP 接口(interface)?

cocoa - 打印Webview ==更改scaleUnitSquareToSize后剪切的内容

delphi - 如何自定义绘制 TEdit 控件文本?

c - 在二维数组中以之字形顺序打印数字