您的位置:首页 > 资讯 > 正文
delphi - 如何打印 TPanel 内容?
来源:个人图书馆-hncdman 发布时间2023-06-23 17:40:10    


(资料图)

标签

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

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

procedureTFormPrintWindows.ButtonPrintPanelClick(Sender:TObject);varBitmap:TBitmap;FromLeft:INTEGER;FromTop:INTEGER;PrintedWidth:INTEGER;PrintedHeight:INTEGER;beginPrinter.BeginDoc;TRYBitmap:=TBitmap.Create;TRYBitmap.Width:=Panel1.Width;Bitmap.Height:=Panel1.Height;Bitmap.PixelFormat:=pf24bit;//avoidpalettes//CopythePanelareafromtheFormintoaseparateBitmapBitmap.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));//Assumes10%left,rightandtopmargin//Assumesbitmapaspectratio>~0.75forportraitmodePrintedWidth:=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);FINALLYBitmap.FreeEND;FINALLYPrinter.EndDocENDend;

并添加

//SourceofCode:///torry/showcode.php?id=744//Whichreferstoapostingtoborland.public.delphi.winapibyRodneyEGeraghty,8/8/97.procedurePrintBitmap(Canvas:TCanvas;DestRect:TRect;Bitmap:TBitmap);varBitmapHeader:pBitmapInfo;BitmapImage:Pointer;HeaderSize:DWORD;ImageSize:DWORD;beginGetDIBSizes(Bitmap.Handle,HeaderSize,ImageSize);GetMem(BitmapHeader,HeaderSize);GetMem(BitmapImage,ImageSize);tryGetDIB(Bitmap.Handle,Bitmap.Palette,BitmapHeader^,BitmapImage^);StretchDIBits(Canvas.Handle,DestRect.Left,DestRect.Top,//DestinationOriginDestRect.Right-DestRect.Left,//DestinationWidthDestRect.Bottom-DestRect.Top,//DestinationHeight0,0,//SourceOriginBitmap.Width,Bitmap.Height,//SourceWidth&HeightBitmapImage,TBitmapInfo(BitmapHeader^),DIB_RGB_COLORS,SRCCOPY)finallyFreeMem(BitmapHeader);FreeMem(BitmapImage)endend{PrintBitmap};

关键词:

推荐内容