ehlib支持预览打印功能,可以省去重新制作报表的麻烦,经过一天的努力,基本上解决了这个问题。把解决方法写出来,同行的朋友可以参考,同时为自己做个学习笔记。
首先,需要放置PrintDBGridEh和PreviewBox两个控件。通过PreviewBox设置纸张的大小和方向。方向通过PreviewBox1.Printer.Orientation属性设置,poLandscape为横向,poPortrait 为纵向。纸张大小尚未研究明白,以后再补充。
然后,需要设置将内容显示在一页内,将PrintDBGridEh的options的pghFitGridToPageWidth属性设置为true就可以了。
具体预览代码如下:
self.yb_DBGridEh2.Visible:=False;
Self.yb_PreviewBox1.Printer.Orientation:=poLandscape;//纸张方向
Self.yb_PreviewBox1.Printer.PrinterSetupOwner := self.yb_DBGridEh2;
self.yb_PrintDBGridEh1.DBGridEh:=self.yb_DBGridEh2;
self.yb_PrintDBGridEh1.PrintTo(Self.yb_PreviewBox1.Printer);
self.yb_PreviewBox1.Visible:=True;
Self.yb_PreviewBox1.Printer.Orientation:=poLandscape;//纸张方向
Self.yb_PreviewBox1.Printer.PrinterSetupOwner := self.yb_DBGridEh2;
self.yb_PrintDBGridEh1.DBGridEh:=self.yb_DBGridEh2;
self.yb_PrintDBGridEh1.PrintTo(Self.yb_PreviewBox1.Printer);
self.yb_PreviewBox1.Visible:=True;
具体打印代码如下:
Self.yb_PreviewBox1.Printer.Orientation:=poLandscape;
Self.yb_PreviewBox1.Printer.PrinterSetupOwner := self.yb_DBGridEh2;
self.yb_PrintDBGridEh1.DBGridEh:=self.yb_DBGridEh2;
self.yb_PrintDBGridEh1.PrintTo(Self.yb_PreviewBox1.Printer);
self.yb_PreviewBox1.PrintDialog;
Self.yb_PreviewBox1.Printer.PrinterSetupOwner := self.yb_DBGridEh2;
self.yb_PrintDBGridEh1.DBGridEh:=self.yb_DBGridEh2;
self.yb_PrintDBGridEh1.PrintTo(Self.yb_PreviewBox1.Printer);
self.yb_PreviewBox1.PrintDialog;