DevExpress控件使用交流,DevExpress中国社区 售前咨询
当前位置: 首页 > 开发资源 » 示例代码 » DevExpress Dashboard如何实现打印和输出

DevExpress Dashboard如何实现打印和输出

作者:mayz   来源:本站原创   浏览:Loading...次   发布时间:2013-09-13   评论:0条

下面这段示例演示了如何通过Print Preview打印和输出仪表盘

使用Show Preview 和 Show Paged Preview两种模式可调用打印预览。Show Preview模式下的纸张尺寸刚好适应仪表盘,但第二个仪表盘要划分到适当数量的标准A4纸中。

首先创建一个DashboardExporter类实现DevExpress.XtraPrinting.IPrintable界面。DashboardExporter实例被映射到一个DashboardViewer,当创建实例之后然后传递到Print Preview,从而使仪表盘能打印到合适的纸上。

参考下面的代码:

using System;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.XtraDashboard;
using DevExpress.XtraPrinting;
using DevExpress.XtraReports.Native;
using DevExpress.XtraReports.UI;

namespace DashboardExport {
    class DashboardExporter : IPrintable {
        PrintingSystem ps = new PrintingSystem();
        PrintableComponentLink componentLink = new PrintableComponentLink();
        DashboardViewer viewer;
        bool paged;
        public DashboardExporter(DashboardViewer viewer) {
            this.viewer = viewer;
            ps.Links.AddRange(new object[] { componentLink });
            ps.AfterChange += ps_AfterChange;
            componentLink.Component = this;
        }
        internal void ShowPrintPreview(bool fitPage) {
            if(fitPage) {
                componentLink.PaperKind = System.Drawing.Printing.PaperKind.Custom;
                componentLink.CustomPaperSize = 
                    new Size(Convert.ToInt32(Math.Ceiling(viewer.Width / 0.96f)) + 45, 
                        Convert.ToInt32(Math.Ceiling(viewer.Height / 0.96f)) + 45);
                ps.PreviewFormEx.Size = new Size(viewer.Width + 100, viewer.Height + 100);
            } else {
                componentLink.PaperKind = System.Drawing.Printing.PaperKind.A4;
                ps.PreviewFormEx.Size = new Size(0x2f0, 0x274);
            }
            componentLink.Margins = new System.Drawing.Printing.Margins(20, 20, 20, 20);
            componentLink.MinMargins = new System.Drawing.Printing.Margins(0, 0, 0, 0);
            ps.PreviewFormEx.StartPosition = FormStartPosition.CenterScreen;
            this.paged = !fitPage;
            componentLink.CreateDocument();
            componentLink.ShowPreview();
        }
        void ps_AfterChange(object sender, ChangeEventArgs e) {
            switch(e.EventName) {
                case SR.PageSettingsChanged:
                case SR.AfterMarginsChange:
                    componentLink.CreateDocument();
                    break;
            }
        }
        int ToDocument(int value) {
            return Convert.ToInt32(GraphicsUnitConverter.PixelToDoc(value));
        }
#region IPrintable
        void IPrintable.AcceptChanges() {
        }
        bool IPrintable.CreatesIntersectedBricks {
            get { return false; }
        }
        bool IPrintable.HasPropertyEditor() {
            return false;
        }
        UserControl IPrintable.PropertyEditorControl {
            get { return null; }
        }
        void IPrintable.RejectChanges() {            
        }
        void IPrintable.ShowHelp() {
        }
        bool IPrintable.SupportsHelp() {
            return false;
        }
        void IBasePrintable.CreateArea(string areaName, IBrickGraphics graph) {
            if(areaName != "Detail")
                return;
            int width;
            int height;
            if(paged) {
                width = viewer.Width;
                height = viewer.Height;
            } else {
                width = Convert.ToInt32(ps.PageSettings.UsablePageSizeInPixels.Width);
                height = Convert.ToInt32(ps.PageSettings.UsablePageSizeInPixels.Height);
            }
            Image bitmap = 
                XRControlPaint.GetControlImage(
                    viewer, 
                    WinControlDrawMethod_Utils.UseWMPrintRecursive, 
                    WinControlImageType_Utils.Bitmap);
            graph.DrawBrick(new ImageBrick() {
                    Image = bitmap, 
                    SizeMode = ImageSizeMode.CenterImage
                }, new Rectangle(0, 0, width, height));
        }
        void IBasePrintable.Finalize(IPrintingSystem ps, ILink link) {
        }
        void IBasePrintable.Initialize(IPrintingSystem ps, ILink link) {
        }
#endregion
    }
}

本站文章除注明转载外,均为本站原创或翻译
欢迎任何形式的转载,但请务必注明出处,尊重他人劳动成果
转载请注明:文章转载自:DevExpress控件中文网 [http://www.devexpresscn.com/]
本文地址:http://www.devexpresscn.com/Resources/CodeExamples-393.html

评论列表

暂无评论

请谈谈你的看法 请使用IE或者Firefox浏览器,暂不支持Chrome!

昵称 不填则默认为游客评论

DevExpress DXperience DXv2 v12

DevExpress DXperience 下载 download

DevExpress购买、价格、授权

慧都控件网为DevExpress界面控件的中国地区唯一正式授权经销商,正版控件销售公司,授权代理商,经销商及合作伙伴。

电话:400-700-1020
        023-66090381

邮箱:sales@evget.com

>>如何选择正规控件购买渠道