如何使用 PageSettings 对象和 PageSetupDialog 控件允许用户选择各种打印选项

//------------------------------------------------------------------------------
/// <copyright from='1997' to='2001' company='Microsoft Corporation'>
///    版权所有 (c) Microsoft Corporation。保留所有权利。
///
///    此源代码仅作为 Microsoft 开发工具和/或联机文档
///    的补充。有关 Microsoft 代码示例的详细信息,请
///    参阅这些其他资料。
///
/// </copyright>
//------------------------------------------------------------------------------
namespace Microsoft.Samples.WinForms.Cs.PrintingExample4
{
    using System;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Drawing;
    using System.Drawing.Printing;
    using System.IO;
    public class PrintForm : System.Windows.Forms.Form
    {
        /// <summary>
        ///    必需的设计器变量。
        /// </summary>
        private System.ComponentModel.Container components;
        protected internal System.Windows.Forms.Button printButton;
        protected internal System.Windows.Forms.Button pageSetupButton;
        private PageSettings storedPageSettings = null;
        public PrintForm()
        {
            //
            // Windows 窗体设计器支持所必需的
            //
            InitializeComponent();
            // 启动按钮的事件处理程序
            pageSetupButton.Click += new System.EventHandler(pageSetupButton_Click);
            printButton.Click += new System.EventHandler(printButton_Click);
        }

        //在用户按下页面设置按钮时激发的事件
        private void pageSetupButton_Click(object sender, EventArgs e)
        {
            try
            {
                PageSetupDialog psDlg = new PageSetupDialog();
                if (storedPageSettings == null)
                {
                    storedPageSettings = new PageSettings();
                }
                psDlg.PageSettings = storedPageSettings;
                psDlg.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show("发生错误 - " + ex.Message);
            }
        }

        //在用户按下打印按钮时激发的事件
        private void printButton_Click(object sender, EventArgs e)
        {
            try
            {
                StreamReader streamToPrint = new StreamReader("PrintMe.Txt");
                try
                {
                    TextFilePrintDocument pd = new TextFilePrintDocument(streamToPrint); //假定为默认打印机
                    if (storedPageSettings != null)
                    {
                        pd.DefaultPageSettings = storedPageSettings;
                    }
                    PrintDialog dlg = new PrintDialog();
                    dlg.Document = pd;
                    DialogResult result = dlg.ShowDialog();
                    if (result == DialogResult.OK)
                    {
                        pd.Print();
                    }
                }
                finally
                {
                    streamToPrint.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("打印文件时发生错误 - " + ex.Message);
            }
        }
        /// <summary>
        ///    清理正在使用的所有资源。
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }
        /// <summary>
        ///    设计器支持所必需的方法,不要使用
        ///    代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.printButton = new System.Windows.Forms.Button();
            this.pageSetupButton = new System.Windows.Forms.Button();
            this.Text = "打印示例 4";
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(504, 381);
            printButton.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            printButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            printButton.Size = new System.Drawing.Size(136, 40);
            printButton.TabIndex = 0;
            printButton.Location = new System.Drawing.Point(32, 112);
            printButton.Text = "打印文件";
            pageSetupButton.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            pageSetupButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            pageSetupButton.Size = new System.Drawing.Size(136, 40);
            pageSetupButton.TabIndex = 0;
            pageSetupButton.Location = new System.Drawing.Point(32, 160);
            pageSetupButton.Text = "页面设置";
            this.Controls.Add(this.printButton);
            this.Controls.Add(this.pageSetupButton);
        }
        /// <summary>
        /// 应用程序的主要入口点。
        /// </summary>
        [STAThread]
        public static void Main(string[] args)
        {
            Application.Run(new PrintForm());
        }
    }
    // <doc>
    // <desc>
    //     TextFilePrintDocument 将流输出到打印机
    //
    //     注意:为了避免在关闭文件时出问题,
    //     如果发生异常,此类仅使用一个流
    //     并将它留给调用方,以打开该文件
    //     进行打印
    //
    // </desc>
    // </doc>
    public class TextFilePrintDocument : PrintDocument
    {
        private Font printFont = null;
        private StreamReader streamToPrint = null;
        public TextFilePrintDocument(StreamReader streamToPrint) : base ()
        {
            this.streamToPrint = streamToPrint;
        }
        //重写 OnBeginPrint 以设置将要使用的字体
        protected override void OnBeginPrint(PrintEventArgs ev)
        {
            base.OnBeginPrint(ev);
            printFont = new Font("Arial", 10);
        }
        //重写 OnPrintPage 以为文档提供打印逻辑
        protected override void OnPrintPage(PrintPageEventArgs ev)
        {
            base.OnPrintPage(ev);
            float lpp = 0;
            float yPos = 0;
            int count = 0;
            float leftMargin = ev.MarginBounds.Left;
            float topMargin = ev.MarginBounds.Top;
            String line = null;
            //算出每页的行数
            //在事件上使用 MarginBounds 以达到此目的
            lpp = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics);
            //现在,在文件上重复此操作以输出每行
            //注意:假设单行比页宽窄
            //首先检查行数,以便看不到不打印的行
            while (count < lpp && ((line = streamToPrint.ReadLine()) != null))
            {
                yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
                ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat());
                count++;
            }
            //如果有多行,则另外打印一页
            if (line != null)
                ev.HasMorePages = true;
            else
                ev.HasMorePages = false;
        }
    }
}
 
 
链接到联机示例文档
链接到联机示例源代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值