CSDN博客

img abaowu

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

发表于2004/9/15 13:58:00  2152人阅读

分类: .NET SOUCE

//------------------------------------------------------------------------------
/// <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;
        }

    }

}

 

 

链接到联机示例文档

链接到联机示例源代码

阅读全文
0 0

相关文章推荐

img
取 消
img