C# 列印表單上的所有Lable和TextBox(可多個表單)__轉載

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.