2024年终活动

DevExpress控件使用交流,DevExpress中国社区Dev联系电话 联系电话:023-68661681

DevExpress Dashboard如何实现打印和输出

来源:本站原创   发布时间:2013-09-13   浏览:5772次

下面这段示例演示了如何通过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控件中文网 [https://www.devexpresscn.com/]
本文地址:https://www.devexpresscn.com/post/493.html
扫码咨询
电话咨询
023-68661681
返回
顶部