轉(zhuǎn)帖|使用教程|編輯:龔雪|2014-03-18 15:41:17.000|閱讀 6888 次
概述:本文介紹了將Developer Express .NET中的ChartControl直接進(jìn)行打印的實現(xiàn)方法。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
通過幾天的學(xué)習(xí),終于對打印有了初步的認(rèn)識,并實現(xiàn)了打印功能,下面將詳細(xì)介紹下如何使用DevExpress下的打印功能。
首先要實現(xiàn)打印功能必須添加一個引用:DevExpress.XtraPrint
實現(xiàn)打印需要用到的主要的類:
1. public class PrintingSystem : Component, ISupportInitialize,IPrintingSystem,IAccessible,IExporter,IPrintingSystemContext
該類負(fù)責(zé)提供生成報表的一些方法,還提供了對頁面進(jìn)行設(shè)置和打印報表的一些函數(shù),這個類的屬性包含了訪問其他管理報表的對象,如Document類、BrickGraphics類(在報表上進(jìn)行畫圖的類)、XtraPageSettings類(提供對打印頁面進(jìn)行設(shè)置的操作)
2. public class PrintableComponentLink : Link
該類的功能是對實現(xiàn)DevExpress.XtraPrinting.IPrintable接口的所有控件進(jìn)行打印,它提供了一些基本的生成和打印報表的功能。在生成報表以后可以通過PrintingSystem將報表打印出來。
通過以上這兩個類就能完全實現(xiàn)各種控件的打印了,只要一個控件實現(xiàn)了DevExpress.XtraPrinting.IPrintable這個接口就能夠?qū)υ摽?件進(jìn)行打印,如GridControl,CharControl等等,下面給出具體的實現(xiàn)方法:
public class PrintSettingController { PrintingSystem ps = null; string formName = null; PrintableComponentLink link = null; string _PrintHeader = null; /// <summary> /// 打印時的標(biāo)題 /// </summary> public string PrintHeader { set { _PrintHeader = value; } } string _PrintFooter = null; /// <summary> /// 打印時頁腳 /// </summary> public string PrintFooter { set { _PrintFooter = value; } } bool _landScape; /// <summary> /// 頁面橫縱向 /// </summary> public bool LandScape { set { _landScape = value; } } System.Drawing.Printing.PaperKind _paperKind; /// <summary> /// 紙型 /// </summary> public System.Drawing.Printing.PaperKind PaperKind { set { _paperKind = value; } } /// <summary> /// 打印控制器 /// </summary> /// <param name="control">要打印的部件</param> public PrintSettingController(IPrintable control) { if (control == null) return; Control c = (Control)control; formName = c.FindForm().GetType().FullName + "." + c.Name; ps = new DevExpress.XtraPrinting.PrintingSystem(); link = new DevExpress.XtraPrinting.PrintableComponentLink(ps); ps.Links.Add(link); link.Component = control; } /// <summary> /// 打印預(yù)覽 /// </summary> public void Preview() { try { if (DevExpress.XtraPrinting.PrintHelper.IsPrintingAvailable) { Cursor.Current = Cursors.AppStarting; if (_PrintHeader != null) { PageHeaderFooter phf = link.PageHeaderFooter as PageHeaderFooter; //設(shè)置頁眉 phf.Header.Content.Clear(); phf.Header.Content.AddRange(new string[] { "", _PrintHeader, "" }); phf.Header.Font = new System.Drawing.Font("宋體", 14, System.Drawing.FontStyle.Bold); phf.Header.LineAlignment = BrickAlignment.Center; //設(shè)置頁腳 phf.Footer.Content.Clear(); phf.Footer.Content.AddRange(new string[] { "", "", _PrintFooter }); phf.Footer.Font = new System.Drawing.Font("宋體", 9, System.Drawing.FontStyle.Regular); phf.Footer.LineAlignment = BrickAlignment.Center; } link.PaperKind = ps.PageSettings.PaperKind ; link.Margins = ps.PageSettings.Margins; link.Landscape = ps.PageSettings.Landscape; link.CreateDocument(); //漢化 DevExpress.XtraPrinting.Localization.PreviewLocalizer.Active = new Dxperience.LocalizationCHS.DxperienceXtraPrintingLocalizationCHS(); ps.PreviewFormEx.Show(); } else { Cursor.Current = Cursors.Default; MessageBox.Show("打印機不可用 ", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } finally { Cursor.Current = Cursors.Default; } } /// <summary> /// 打印 /// </summary> public void Print() { try { if (DevExpress.XtraPrinting.PrintHelper.IsPrintingAvailable) { if (_PrintHeader != null) { PageHeaderFooter phf = link.PageHeaderFooter as PageHeaderFooter; //設(shè)置頁眉 phf.Header.Content.Clear(); phf.Header.Content.AddRange(new string[] { "", _PrintHeader, "" }); phf.Header.Font = new System.Drawing.Font("宋體", 14, System.Drawing.FontStyle.Bold); phf.Header.LineAlignment = BrickAlignment.Center; //設(shè)置頁腳 phf.Footer.Content.Clear(); phf.Footer.Content.AddRange(new string[] { "", "", _PrintFooter }); phf.Footer.Font = new System.Drawing.Font("宋體", 9, System.Drawing.FontStyle.Regular); phf.Footer.LineAlignment = BrickAlignment.Center; } link.PaperKind = ps.PageSettings.PaperKind; link.Margins = ps.PageSettings.Margins; link.Landscape = ps.PageSettings.Landscape; link.CreateDocument(); link.CreateDocument(); //漢化 DevExpress.XtraPrinting.Localization.PreviewLocalizer.Active = new Dxperience.LocalizationCHS.DxperienceXtraPrintingLocalizationCHS(); ps.Print(); } else { Cursor.Current = Cursors.Default; MessageBox.Show("打印機不可用 ", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } finally { } } //獲取頁面設(shè)置信息 public void LoadPageSetting() { System.Drawing.Printing.Margins margins = new System.Drawing.Printing.Margins(60, 60, 60, 60); ps.PageSettings.Assign(margins, _paperKind, _landScape); } }
客戶端如何使用:實例化PrintSettingController,進(jìn)行參數(shù)設(shè)置,包括頁眉頁腳橫縱向紙型,當(dāng)然所有這些設(shè)置信息也可以在PrintSettingControll類里進(jìn)行設(shè)置了,我這里是需求的需要,所以加在了外面,PrintSystem.PageSettings屬性是XtraPageSettings類型,XtraPageSettings類封裝了所有的頁面設(shè)置信息,你可以在LoadPageSetting()函數(shù)里面進(jìn)行修改,來實現(xiàn)自己想要的功能,例如可以把設(shè)置信息保存成XML文件
PrintSettingController psc = new PrintSettingController(this.gridControl1); //頁眉 psc.PrintHeader = this.Text; //頁腳 psc.PrintFooter = this.dateTimePicker1.Text.ToString(); //橫縱向 psc.LandScape = this.rbtnHorizon.Checked; //紙型 psc.PaperKind = System.Drawing.Printing.PaperKind.A4; //加載頁面設(shè)置信息 psc.LoadPageSetting(); psc.Preview();
轉(zhuǎn)載自//blog.csdn.net/wdwgr/article/details/4041844
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都控件網(wǎng)