C# 打印窗体上的所有Lable和TextBox(可多个窗体)__转载

C# 打印窗体上的所有Lable和TextBox(可多个窗体)

新建一个类,全部代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Windows.Forms;

namespace System_Class
{
   
    /// <summary>
    /// 打印窗体上所有lalel和textbox的Text 正常
    /// </summary>
    public class PrintLblTb
    {
        private System.Drawing.Printing.PrintDocument docToPrint = new System.Drawing.Printing.PrintDocument();
        protected PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog();
        private Form form = null;
        private Form[] forms = null;

        public PrintLblTb()
        {
            this.docToPrint.PrintPage += new PrintPageEventHandler(docToPrint_PrintPage);
        }

        /// <summary>
        /// 开始打印
        /// </summary>
        /// <param name="f">当前窗体</param>
        /// <param name="DocTitle">标题</param>
        public void StartPrint(Form f, string DocTitle)
        {
            form = f;
            System.Windows.Forms.PrintDialog PrintDialog1 = new PrintDialog();
            PrintDialog1.AllowSomePages = true;
            PrintDialog1.ShowHelp = true;
            PrintDialog1.Document = docToPrint;

            DialogResult result = PrintDialog1.ShowDialog();//调用PrintDialog的ShowDialog函数显示打印对话框
            if (result == DialogResult.OK)
            {
                //docToPrint.Print();//开始打印
                docToPrint.DocumentName = DocTitle;
                docToPrint.PrinterSettings = PrintDialog1.PrinterSettings;
                docToPrint.DefaultPageSettings = PrintDialog1.PrinterSettings.DefaultPageSettings;
                //docToPrint.DefaultPageSettings.Landscape = true;
                docToPrint.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);
                printPreviewDialog.Document = docToPrint;
                printPreviewDialog.ShowDialog();
            }
        }

        /// <summary>
        /// 开始打印多个窗体
        /// </summary>
        /// <param name="f">窗体数组</param>
        /// <param name="DocTitle">标题</param>
        public void StartPrint(Form[] f, string DocTitle)
        {
            forms = f;
            System.Windows.Forms.PrintDialog PrintDialog1 = new PrintDialog();
            PrintDialog1.AllowSomePages = true;
            PrintDialog1.ShowHelp = true;
            PrintDialog1.Document = docToPrint;

            DialogResult result = PrintDialog1.ShowDialog();//调用PrintDialog的ShowDialog函数显示打印对话框
            if (result == DialogResult.OK)
            {
                //docToPrint.Print();//开始打印
                docToPrint.DocumentName = DocTitle;
                docToPrint.PrinterSettings = PrintDialog1.PrinterSettings;
                docToPrint.DefaultPageSettings = PrintDialog1.PrinterSettings.DefaultPageSettings;
                //docToPrint.DefaultPageSettings.Landscape = true;
                docToPrint.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);
                printPreviewDialog.Document = docToPrint;
                printPreviewDialog.ShowDialog();
            }
        }

        private void docToPrint_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)//设置打印机开始打印的事件处理函数
        {
            //绘制label2中的内容
            //e.Graphics.DrawString(label2.Text, new Font("宋体", 20, FontStyle.Regular), Brushes.Black, 270, 430);
            //e.Graphics.DrawString(text, printFont, System.Drawing.Brushes.Black, e.MarginBounds.X, e.MarginBounds.Y);
            //e.Graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, System.Drawing.GraphicsUnit.Pixel);
            //Font f=new Font("宋体",12, FontStyle.Regular);

            int formTopHeight = 0;

            //多个窗体
            if (forms != null)
            {
                foreach( Form f in forms)
                {
                    FindLableOrTextBox(f, f.Controls, e, formTopHeight);
                    formTopHeight += f.Height;
                    //string fg="-----------------------------------------------------------------------------------------";
                    //e.Graphics.DrawString(fg, new Font("宋体", 12, FontStyle.Regular), Brushes.Black, f.Left, f.Top + formTopHeight);
                }
            }
            else if (form != null)
            {
                FindLableOrTextBox(form, form.Controls, e,formTopHeight);
            }
        }

        /// <summary>
        /// 为每一个Label和TextBox绘图
        /// </summary>
        /// <param name="O">父对像</param>
        /// <param name="CS">控件集</param>
        /// <param name="e">绘制事件</param>
        private void FindLableOrTextBox(Object O,Control.ControlCollection CS, System.Drawing.Printing.PrintPageEventArgs e,int TopH)
        {
            foreach (Control cot in CS)
            {
                if (typeof(Button) != cot.GetType())//typeof(Label) == cot.GetType() || typeof(TextBox) == cot.GetType()
                {
                    float ptop = 0;
                    if (O.GetType() == typeof(Panel))
                    {
                        Panel p = O as Panel;
                        ptop = p.Top;
                    }

                    if(!cot.Text.Equals("*"))
                        e.Graphics.DrawString(cot.Text, cot.Font, Brushes.Black, cot.Left, cot.Top + ptop + TopH);
                }
                if (cot.Controls.Count > 0) FindLableOrTextBox(cot,cot.Controls,e,TopH);
            }
        }
    }
}

调用如下:

多窗体:

            TDJXC.Fees.Print1 p1 = new Print1(_umode,_mode,1,lblAll.Text);
            TDJXC.Fees.Print1 p2 = new Print1(_umode, _mode, 2, lblAll.Text);
            Form[] fs = { this, p1, p2 };

            System_Class.PrintLblTb scp = new System_Class.PrintLblTb();
            scp.StartPrint(fs,"打印收据");

单窗体

            System_Class.PrintLblTb scp = new System_Class.PrintLblTb();
            scp.StartPrint(this,"打印收据");

转载于http://www.wotui.net/wotui/news/show.asp?id=2579&pid=125

原文地址:https://www.cnblogs.com/fyhui/p/2281677.html