zoukankan      html  css  js  c++  java
  • C# winform 打印事例

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Drawing.Printing;
    
    namespace Zsh.print
    {
        public partial class frmBadHzd : Form
        {
            public frmBadHzd()
            {
                InitializeComponent();
            }
    
            private void frmBadHzd_Paint(object sender, PaintEventArgs e)
            {
                Font font;
                string str;
                float xPos;       //x点坐标
                 float yPos;       //y点的坐标
                 float topMargin = 0;
                float leftMargin = 0;
    
                font = new Font("宋体", 12);
                str = "欢迎光临";
                xPos = leftMargin + 15;
                yPos = topMargin;
                e.Graphics.DrawString(str, font, Brushes.Black, xPos, yPos, new StringFormat());
    
                xPos = leftMargin;
                yPos = yPos + font.GetHeight(e.Graphics) + 5;
                font = new Font("黑体", 12);
                str = "---------------------------------";
                e.Graphics.DrawString(str, font, Brushes.Black, xPos, yPos, new StringFormat());
    
                xPos = leftMargin + 25;
                yPos = yPos + font.GetHeight(e.Graphics) + 3;
                font = new Font("黑体", 18);
                str = "客 户 回 执 单";
                e.Graphics.DrawString(str, font, Brushes.Black, xPos, yPos, new StringFormat());
    
                xPos = leftMargin;
                yPos = yPos + font.GetHeight(e.Graphics) + 3;
                font = new Font("黑体", 12);
                str = "---------------------------------";
                e.Graphics.DrawString(str, font, Brushes.Black, xPos, yPos, new StringFormat());
    
                xPos = leftMargin;
                yPos = yPos + font.GetHeight(e.Graphics) + 5;
                font = new Font("宋体", 11);
                str = "  凭单号";
                e.Graphics.DrawString(str, font, Brushes.Black, xPos, yPos, new StringFormat());
    
                xPos = leftMargin;
                yPos = yPos + font.GetHeight(e.Graphics) + 5;
                font = new Font("宋体", 11);
                str = "业务类型";
                e.Graphics.DrawString(str, font, Brushes.Black, xPos, yPos, new StringFormat());
    
                xPos = leftMargin;
                yPos = yPos + font.GetHeight(e.Graphics) + 5;
                font = new Font("宋体", 11);
                str = "    卡号";
                e.Graphics.DrawString(str, font, Brushes.Black, xPos, yPos, new StringFormat());
    
                xPos = leftMargin;
                yPos = yPos + font.GetHeight(e.Graphics) + 10;
                font = new Font("宋体", 11);
                str = "  原余额";
                e.Graphics.DrawString(str, font, Brushes.Black, xPos, yPos, new StringFormat());
    
                xPos = leftMargin;
                yPos = yPos + font.GetHeight(e.Graphics) + 5;
                font = new Font("宋体", 11);
                str = "  现余额";
                e.Graphics.DrawString(str, font, Brushes.Black, xPos, yPos, new StringFormat());
    
                xPos = leftMargin;
                yPos = yPos + font.GetHeight(e.Graphics) + 5;
                font = new Font("黑体", 12);
                str = "---------------------------------";
                e.Graphics.DrawString(str, font, Brushes.Black, xPos, yPos, new StringFormat());
    
                xPos = leftMargin + 50;
                yPos = yPos + font.GetHeight(e.Graphics) + 5;
                font = new Font("宋体", 14);
                str = "客户签名";
                e.Graphics.DrawString(str, font, Brushes.Black, xPos, yPos, new StringFormat());
    
                xPos = leftMargin + 130;
                yPos = yPos + font.GetHeight(e.Graphics) - 5;
                font = new Font("宋体", 12);
                str = "-----------";
                e.Graphics.DrawString(str, font, Brushes.Black, xPos, yPos, new StringFormat());
    
                xPos = leftMargin;
                yPos = yPos + font.GetHeight(e.Graphics);
                font = new Font("宋体", 10);
                str = "受理单位名称";
                e.Graphics.DrawString(str, font, Brushes.Black, xPos, yPos, new StringFormat());
    
                xPos = leftMargin;
                yPos = yPos + font.GetHeight(e.Graphics) + 5;
                font = new Font("宋体", 10);
                str = "  操作员名称";
                e.Graphics.DrawString(str, font, Brushes.Black, xPos, yPos, new StringFormat());
    
                xPos = leftMargin + 10;
                yPos = yPos + font.GetHeight(e.Graphics) + 5;
                font = new Font("宋体", 10);
                str = "   日期";
                e.Graphics.DrawString(str, font, Brushes.Black, xPos, yPos, new StringFormat());
    
                xPos = leftMargin;
                yPos = yPos + font.GetHeight(e.Graphics) + 5;
                font = new Font("黑体", 12);
                str = "---------------------------------";
                e.Graphics.DrawString(str, font, Brushes.Black, xPos, yPos, new StringFormat());
    
                xPos = leftMargin;
                yPos = yPos + font.GetHeight(e.Graphics) + 5;
                font = new Font("华文行楷", 12);
                str = "此凭单为购物凭证,请妥善保管!";
                e.Graphics.DrawString(str, font, Brushes.Black, xPos, yPos, new StringFormat());
            }
    
            private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
            {
                Font font;
                string str;
                float xPos;       //x点坐标
                 float yPos;       //y点的坐标
                 float topMargin = 0;
                float leftMargin = 0;
    
                font = new Font("宋体", 12);
                str = "欢迎光临";
                xPos = leftMargin + 15;
                yPos = topMargin;
                e.Graphics.DrawString(str, font, Brushes.Black, xPos, yPos, new StringFormat());
    
                xPos = leftMargin;
                yPos = yPos + font.GetHeight(e.Graphics) + 5;
                font = new Font("黑体", 12);
                str = "---------------------------------";
                e.Graphics.DrawString(str, font, Brushes.Black, xPos, yPos, new StringFormat());
    
                xPos = leftMargin + 25;
                yPos = yPos + font.GetHeight(e.Graphics) + 3;
                font = new Font("黑体", 18);
                str = "客 户 回 执 单";
                e.Graphics.DrawString(str, font, Brushes.Black, xPos, yPos, new StringFormat());
    
                xPos = leftMargin;
                yPos = yPos + font.GetHeight(e.Graphics) + 3;
                font = new Font("黑体", 12);
                str = "---------------------------------";
                e.Graphics.DrawString(str, font, Brushes.Black, xPos, yPos, new StringFormat());
    
                xPos = leftMargin;
                yPos = yPos + font.GetHeight(e.Graphics) + 5;
                font = new Font("宋体", 11);
                str = "  凭单号";
                e.Graphics.DrawString(str, font, Brushes.Black, xPos, yPos, new StringFormat());
    
                xPos = leftMargin;
                yPos = yPos + font.GetHeight(e.Graphics) + 5;
                font = new Font("宋体", 11);
                str = "业务类型";
                e.Graphics.DrawString(str, font, Brushes.Black, xPos, yPos, new StringFormat());
    
                xPos = leftMargin;
                yPos = yPos + font.GetHeight(e.Graphics) + 5;
                font = new Font("宋体", 11);
                str = "    卡号";
                e.Graphics.DrawString(str, font, Brushes.Black, xPos, yPos, new StringFormat());
    
                xPos = leftMargin;
                yPos = yPos + font.GetHeight(e.Graphics) + 10;
                font = new Font("宋体", 11);
                str = "  原余额";
                e.Graphics.DrawString(str, font, Brushes.Black, xPos, yPos, new StringFormat());
    
                xPos = leftMargin;
                yPos = yPos + font.GetHeight(e.Graphics) + 5;
                font = new Font("宋体", 11);
                str = "  现余额";
                e.Graphics.DrawString(str, font, Brushes.Black, xPos, yPos, new StringFormat());
    
                xPos = leftMargin;
                yPos = yPos + font.GetHeight(e.Graphics) + 5;
                font = new Font("黑体", 12);
                str = "---------------------------------";
                e.Graphics.DrawString(str, font, Brushes.Black, xPos, yPos, new StringFormat());
    
                xPos = leftMargin + 50;
                yPos = yPos + font.GetHeight(e.Graphics) + 5;
                font = new Font("宋体", 14);
                str = "客户签名";
                e.Graphics.DrawString(str, font, Brushes.Black, xPos, yPos, new StringFormat());
    
                xPos = leftMargin + 130;
                yPos = yPos + font.GetHeight(e.Graphics) - 5;
                font = new Font("宋体", 12);
                str = "-----------";
                e.Graphics.DrawString(str, font, Brushes.Black, xPos, yPos, new StringFormat());
    
                xPos = leftMargin;
                yPos = yPos + font.GetHeight(e.Graphics);
                font = new Font("宋体", 10);
                str = "受理单位名称";
                e.Graphics.DrawString(str, font, Brushes.Black, xPos, yPos, new StringFormat());
    
                xPos = leftMargin;
                yPos = yPos + font.GetHeight(e.Graphics) + 5;
                font = new Font("宋体", 10);
                str = "  操作员名称";
                e.Graphics.DrawString(str, font, Brushes.Black, xPos, yPos, new StringFormat());
    
                xPos = leftMargin + 10;
                yPos = yPos + font.GetHeight(e.Graphics) + 5;
                font = new Font("宋体", 10);
                str = "   日期";
                e.Graphics.DrawString(str, font, Brushes.Black, xPos, yPos, new StringFormat());
    
                xPos = leftMargin;
                yPos = yPos + font.GetHeight(e.Graphics) + 5;
                font = new Font("黑体", 12);
                str = "---------------------------------";
                e.Graphics.DrawString(str, font, Brushes.Black, xPos, yPos, new StringFormat());
    
                xPos = leftMargin;
                yPos = yPos + font.GetHeight(e.Graphics) + 5;
                font = new Font("华文行楷", 12);
                str = "此凭单为购物凭证,请妥善保管!";
                e.Graphics.DrawString(str, font, Brushes.Black, xPos, yPos, new StringFormat());
            }
    
            /// <summary>
            /// 打印票据信息
            /// </summary>
            /// <param name="?">打印机名称</param>
            /// <param name="sho_printNum">打印数量</param>
            public void print(string str_Printer, short sho_printNum)
            {
                try
                {
                    PrintDocument pd = new PrintDocument();
                    pd.PrinterSettings.PrinterName = str_Printer;
                    pd.PrinterSettings.Copies = sho_printNum;
                    pd.PrintController = new StandardPrintController();
                    pd.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
                    if (pd.PrinterSettings.IsValid)
                    {
                        pd.Print();
                    }
                    else
                    {
                        MessageBox.Show("打印机连接错误", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                }
            }
        }
    }

    源码源于 ==》 http://bbs.csdn.net/topics/310215237

  • 相关阅读:
    C语言探索之旅 | 第二部分第二课:进击的指针,C语言的王牌!
    C语言探索之旅 | 第二部分第一课:模块化编程
    C语言探索之旅 | 第一部分练习题
    C语言探索之旅 | 第一部分第十一课:函数
    数据结构和算法 | 第一部分第五课:算法复杂度实践
    数据结构和算法 | 第一部分第四课:算法复杂度(下)
    数据结构和算法 | 第一部分第三课:算法复杂度(上)
    数据结构和算法 | 第一部分第二课:小鸭子们去旅行
    数据结构和算法 | 第一部分第一课:什么是数据结构和算法
    C语言探索之旅 | 第一部分第十课:第一个C语言小游戏
  • 原文地址:https://www.cnblogs.com/felix-h/p/4651129.html
Copyright © 2011-2022 走看看