打印小票

来源:互联网 发布:阿拉伯数字转大写算法 编辑:程序博客网 时间:2024/04/30 06:25

1,在Form中加入一个印刷的控件PrintDocument,取名为MyPrintDocument

2,新建一个一般类(不是窗体类),名叫MyPrinter,内容如下:

 public class MyPrinter

    {

        private PrintDocument ThePrintDocument;

        private Font titleFont;

        private Font theFont;

        private Color FontColor;

        private float CurrentY;

        static int PageNumber;

        private int PageWidth;

        private int PageHeight;

        private int LeftMargin;

        private int TopMargin;

        private int RightMargin;

        private int BottomMargin;

        private ArrayList textList = new ArrayList();

        private int currentIndex = 0;

        public MyPrinter(PrintDocument _thePrintDocument, Font _theFont, Font _titleFont,Color _FontColor, ArrayList _textList)

        {

            ThePrintDocument = _thePrintDocument;

            theFont = _theFont;

            titleFont = _titleFont;

            FontColor = _FontColor;

            PageNumber = 0;

            textList = _textList;

            if (!ThePrintDocument.DefaultPageSettings.Landscape)

            {

                PageWidth = ThePrintDocument.DefaultPageSettings.PaperSize.Width;

                PageHeight = ThePrintDocument.DefaultPageSettings.PaperSize.Height;

            }

            else

            {

                PageHeight = ThePrintDocument.DefaultPageSettings.PaperSize.Width;

                PageWidth = ThePrintDocument.DefaultPageSettings.PaperSize.Height;

            }

            LeftMargin = ThePrintDocument.DefaultPageSettings.Margins.Left;

            TopMargin = ThePrintDocument.DefaultPageSettings.Margins.Top;

            RightMargin = ThePrintDocument.DefaultPageSettings.Margins.Right;

            BottomMargin = ThePrintDocument.DefaultPageSettings.Margins.Bottom;

        }

        public bool DrawDocument(Graphics g)

        {

            try

            {

                DrawHeader(g);

                bool bContinue = DrawItems(g);

                g.Dispose();

                return bContinue;

            }

            catch (Exception ex)

            {

                MessageBox.Show("失败" + ex.Message.ToString(), " - Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                g.Dispose();

                return false;

            }

        }

        public void DrawHeader(Graphics g)

        {

            CurrentY = (float)TopMargin;

            PageNumber++;

            string PageString = "第" + PageNumber+"页";

            StringFormat PageStringFormat = new StringFormat();

            PageStringFormat.Trimming = StringTrimming.Word;

            PageStringFormat.FormatFlags = StringFormatFlags.NoWrap | StringFormatFlags.LineLimit | StringFormatFlags.NoClip;

            PageStringFormat.Alignment = StringAlignment.Far;

            RectangleF PageStringRectangle = new RectangleF((float)LeftMargin, CurrentY, (float)PageWidth - (float)RightMargin - (float)LeftMargin, g.MeasureString(PageString, theFont).Height);

            g.DrawString(PageString, theFont, new SolidBrush(Color.Black), PageStringRectangle, PageStringFormat);

            CurrentY += g.MeasureString(PageString, theFont).Height;

            StringFormat TitleFormat = new StringFormat();

            TitleFormat.Trimming = StringTrimming.Word;

            TitleFormat.FormatFlags = StringFormatFlags.NoWrap | StringFormatFlags.LineLimit | StringFormatFlags.NoClip;

            TitleFormat.Alignment = StringAlignment.Center;

            RectangleF TitleRectangle = new RectangleF((float)LeftMargin, CurrentY, (float)PageWidth - (float)RightMargin - (float)LeftMargin, g.MeasureString("收费单", titleFont).Height);

            g.DrawString("收费单",titleFont,new SolidBrush(FontColor),TitleRectangle,TitleFormat);

            CurrentY +=g.MeasureString("收费单",titleFont).Height;

        }

        public bool DrawItems(Graphics g)

        {

            StringFormat TextFormat = new StringFormat();

            TextFormat.Trimming = StringTrimming.Word;

            TextFormat.FormatFlags = StringFormatFlags.NoWrap | StringFormatFlags.LineLimit | StringFormatFlags.NoClip;

            TextFormat.Alignment = StringAlignment.Near;

            for (int i = currentIndex; i < textList.Count; i++)

            {

                string var = textList[i].ToString();

                RectangleF TextRectangle = new RectangleF((float)LeftMargin, CurrentY, (float)PageWidth - (float)RightMargin - (float)LeftMargin, g.MeasureString("收费单", titleFont).Height);

                g.DrawString(var, theFont, new SolidBrush(FontColor), TextRectangle, TextFormat);

                CurrentY = CurrentY + g.MeasureString(var, theFont).Height;

                if ((int)CurrentY > (PageHeight - TopMargin - BottomMargin))

                {

                    currentIndex = i+1;

                    return true;

                }

            }

            return false;

        }

    }

3,回到原来的Form中,给MyPrintDocument添加PrintPage事件

private void MyPrintDocument_PrintPage(object sender, PrintPageEventArgs e)

        {

            bool hasNextPage = mp.DrawDocument(e.Graphics);

            if (hasNextPage)

            {

                e.HasMorePages = true;

            }

        }

4,撰写打印机设置对话框函数

 private bool SetupThePrinting()

        {

            PrintDialog MyPrintDialog = new PrintDialog();

            MyPrintDialog.AllowCurrentPage = false;

            MyPrintDialog.AllowPrintToFile = false;

            MyPrintDialog.AllowSelection = false;

            MyPrintDialog.AllowSomePages = false;

            MyPrintDialog.PrintToFile = false;

            MyPrintDialog.ShowHelp = false;

            MyPrintDialog.ShowNetwork = false;

            if (MyPrintDialog.ShowDialog() != DialogResult.OK)

                return false;

            MyPrintDocument.DocumentName = "收费单 ";

            MyPrintDocument.PrinterSettings = MyPrintDialog.PrinterSettings;

            MyPrintDocument.DefaultPageSettings = MyPrintDialog.PrinterSettings.DefaultPageSettings;

            MyPrintDocument.DefaultPageSettings.Margins = new Margins(100, 40, 100, 40);

            ArrayList textList = new ArrayList();

            mp = new MyPrinter(MyPrintDocument, new Font("Tahoma", 8, FontStyle.Regular, GraphicsUnit.Point), new Font(FontFamily.GenericSerif, 18, FontStyle.Bold, GraphicsUnit.Point), Color.Black, textList);

            return true;

        }

5,最后加上打印按钮,打印预览按钮的单击事件

    1),打印按钮的单击事件

         private void btnPrint_Click(object sender, EventArgs e)

        {            

            if (SetupThePrinting())

                MyPrintDocument.Print();

        }

    2)打印预览按钮的单击事件(根据需要)

         private void btnPrintPreview_Click(object sender, EventArgs e)

        {

            if (SetupThePrinting())

            {

                PrintPreviewDialog MyPrintPreviewDialog = new PrintPreviewDialog();

                MyPrintPreviewDialog.Document = MyPrintDocument;

                MyPrintPreviewDialog.ShowDialog();

            }

        }

6,现在你或许要问,那我的打印数据如何加入呢?其实就在刚才写的打印机设置对话框函数里面,如下:

ArrayList textList = new ArrayList();

            //textList.Add("收费单");

            //textList.Add("2009-04-01");

            //textList.Add("开始时间:12:00 ");

            //textList.Add("结束时间:13:00 ");

            //textList.Add("时间单位:4 ");

            //textList.Add("单位时间价格:20.00元 ");

            //textList.Add("消费价格:80.00元 ");

            //textList.Add("------------------------------------------------------------- ");

            //textList.Add("2009-04-01 ");

            //textList.Add("开始时间:12:20 ");

            //textList.Add("开始时间:12:20 ");

            //textList.Add("时间单位:3 ");

            //textList.Add("单位时间价格:20.00元 ");

            //textList.Add("消费金额:60.00元  ");

            //textList.Add("------------------------------------------------------------- ");

            //textList.Add("合计:140.00元 ");

            //textList.Add("2");

            mp = new MyPrinter(MyPrintDocument, new Font("Tahoma", 8, FontStyle.Regular, GraphicsUnit.Point), new Font(FontFamily.GenericSerif, 18, FontStyle.Bold, GraphicsUnit.Point), Color.Black, textList);

            return true;

也就是,你只要自己根据你现有的数据将信息一行一行Add到textList中,就可以了

楼上的,1万行你复制一万行?for循环留着干嘛的啊?个人觉得不一定现成的东西都是对自己最有利的。

付参考效果图。

0 0
原创粉丝点击