基本信息
源码名称:A4打印(winform源码)
源码大小:0.08M
文件格式:.zip
开发语言:C#
更新时间:2021-08-05
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 5 元 
   源码介绍

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace A4打印
{
    public partial class Form1 : Form
    {
        DB db = new DB();
        string successMsg = "";
        string errMsg = "";
        string sql = "";
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            List<String> list = LocalPrinter.GetLocalPrinters(); //获得系统中的打印机列表
            foreach (String s in list)
            {
                comboBox1.Items.Add(s); //将打印机名称添加到下拉框中
            }
            comboBox1.SelectedIndex = 0;

            this.Width = 1250;
            this.Height = 750;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            string fontName = "隶书";

            Bitmap bmp = new Bitmap(1200, 700);
            Graphics g = Graphics.FromImage((Image)bmp);
            g.Clear(Color.White);

            int h = 50;
            int w = 1100;

            int s = 150;

            string[] dres = {
                "      宇宙人酒楼预订菜单",
                " ",
                " 预订餐台:牡丹,桂花,玫瑰,月季",
                " 预订日期:二〇二〇年九月三十日 午餐",
                " ",
                "凉菜",
                " 凉拌土鸡|壹贰份",
                " 凉拌牛肉|壹份",
                " 凉拌耳片|壹份",
                "热菜" ,
                " 宫保鸡丁|壹份",
                " 鱼香肉丝|壹份",
                " 木瓜官燕|壹份",
                " 红烧带鱼|壹份",
                " 清蒸甲鱼|壹份",
                " 水煮牛肉|壹份",
                " 蒜蓉大虾|壹份",
                " 干锅排骨|壹份",
                " 爆炒猪肝|壹份",
                " 鸿运当头|壹份",
                " 蚂蚁上树|壹份",
                "汤品",
                " 青菜圆子汤|壹份",
                " 鱼头豆腐汤|拾贰例",
                "小吃",
                " 南瓜饼|壹份",
                " 绿豆糕|壹份",
                "酒水",
                " 五粮液|壹瓶",
                " 王老吉|拾贰瓶",
                " ",
                "  合计金额:伍佰伍拾元整"
            };
            for (int j = 0; j < 80; j  )
            {
                for (int i = 0; i <= dres.Length - 1; i  )
                {
                    w -= 30;

                    string str = dres[i];
                    string[] arr = str.Split('|');
                    //上部
                    if (arr[0].Length > j)
                    {
                        str = arr[0][j].ToString();
                    }
                    else
                    {
                        str = "  ";
                    };

                    if (dres[i].Length > j)
                    {
                        g.DrawString(str, new Font(fontName, 18, FontStyle.Bold), Brushes.Black, w, h);
                    }
                    else
                    {
                        g.DrawString("  ", new Font(fontName, 18, FontStyle.Bold), Brushes.Black, w, h);
                    };
                    //下部
                    if (dres[i].Contains('|'))
                    {
                        if (arr[1].Length > j)
                        {
                            str = arr[1][j].ToString();
                        }
                        else
                        {
                            str = "  ";
                        };

                        if (dres[i].Length > j)
                        {
                            g.DrawString(str, new Font(fontName, 18, FontStyle.Bold), Brushes.Black, w, 750 - s);
                        }
                        else
                        {
                            g.DrawString("  ", new Font(fontName, 18, FontStyle.Bold), Brushes.Black, w, 750 - s);
                        };
                    };

                };

                s -= 24;
                h  = 24;
                w = 1100;
            };

            pictureBox1.Image = bmp;

        }
        private void button2_Click(object sender, EventArgs e)
        {
            PrintDocument pd = new PrintDocument();
            pd.PrintPage  = new PrintPageEventHandler(printDocument_Print);
            pd.DefaultPageSettings.PrinterSettings.PrinterName = comboBox1.Text;       //打印机名称
            pd.DefaultPageSettings.Landscape = true;  //设置横向打印,不设置默认是纵向的
            pd.PrintController = new System.Drawing.Printing.StandardPrintController();
            pd.Print();
        }
        int newRow_s = 0;
        int newRow_e = 36;
        DataTable dt = null;
        private void printDocument_Print(object sender, PrintPageEventArgs e)
        {
            string fontName = "隶书";

            PrintDocument tmp = (PrintDocument)sender;
            Graphics g = e.Graphics;
            g.Clear(Color.White);

            string[] dres = {
                "      宇宙人酒楼预订菜单",
                " ",
                " 预订餐台:牡丹,桂花,玫瑰,月季",
                " 预订日期:二〇二〇年九月三十日|午餐",
                " ",
                "凉菜",
                " 凉拌土鸡|壹贰份",
                " 凉拌牛肉|壹份",
                " 凉拌耳片|壹份",
                "热菜" ,
                " 宫保鸡丁|壹份",
                " 鱼香肉丝|壹份",
                " 木瓜官燕|壹份",
                " 红烧带鱼|壹份",
                " 清蒸甲鱼|壹份",
                " 水煮牛肉|壹份",
                " 蒜蓉大虾|壹份",
                " 干锅排骨|壹份",
                " 爆炒猪肝|壹份",
                " 鸿运当头|壹份",
                " 蚂蚁上树|壹份",
                " 干锅排骨|壹份",
                " 爆炒猪肝|壹份",
                " 鸿运当头|壹份",
                " 蚂蚁上树|壹份",
                " 干锅排骨|壹份",
                " 爆炒猪肝|壹份",
                " 鸿运当头|壹份",
                " 蚂蚁上树|壹份",
                " 干锅排骨|壹份",
                " 爆炒猪肝|壹份",
                " 鸿运当头|壹份",
                " 蚂蚁上树|壹份",
                "汤品",
                " 青菜圆子汤|壹份",
                " 鱼头豆腐汤|拾贰例",
                "小吃",
                " 南瓜饼|壹份",
                " 绿豆糕|壹份",
                "酒水",
                " 五粮液|壹瓶",
                " 王老吉|拾贰瓶",
                " ",
                "  合计金额:伍佰伍拾元整"
            };

            int len = dres.Length;
            int sLen = newRow_s;
            int eLen = newRow_e;
            if (len < 36)
            {
                sLen = 0;
                eLen = len;
            };
            int h = 50;
            int w = 1100;
            int s = 150;
            int zh = 700;
            for (int j = 0; j < 30; j  )
            {
                for (int i = sLen; i <= eLen - 1; i  )
                {
                    w -= 30;

                    string str = dres[i];
                    string[] arr = str.Split('|');
                    //上部
                    if (arr[0].Length > j)
                    {
                        str = arr[0][j].ToString();
                    }
                    else
                    {
                        str = "  ";
                    };
                    if (dres[i].Length > j)
                    {
                        g.DrawString(str, new Font(fontName, 18, FontStyle.Bold), Brushes.Black, w, h);
                    }
                    else
                    {
                        g.DrawString("  ", new Font(fontName, 18, FontStyle.Bold), Brushes.Black, w, h);
                    };                    
                    //下部
                    if (dres[i].Contains('|'))
                    {
                        if (arr[1].Length > j)
                        {
                            str = arr[1][j].ToString();
                        }
                        else
                        {
                            str = "  ";
                        };
                        if (dres[i].Length > j)
                        {
                            g.DrawString(str, new Font(fontName, 18, FontStyle.Bold), Brushes.Black, w, zh - s);
                        }
                        else
                        {
                            g.DrawString("  ", new Font(fontName, 18, FontStyle.Bold), Brushes.Black, w, zh - s);
                        };
                    };
                };
                s -= 24;
                h  = 24;
                w = 1100;
            };
            if (newRow_e< len)
            {
                e.HasMorePages = true;
                newRow_s  = 36;
                if (newRow_e   36 <= len)
                {
                    newRow_e  = 36;
                }
                else
                {
                    newRow_e = len;
                };
            }
            else
            {
                e.HasMorePages = false;
                newRow_s = 0;
                newRow_e = 36;
            };
        }

        private void button3_Click(object sender, EventArgs e)
        {
            PrintDocument pd = new PrintDocument();
            pd.PrintPage  = new PrintPageEventHandler(printDocument_Print1);
            pd.DefaultPageSettings.PrinterSettings.PrinterName = comboBox1.Text;       //打印机名称
            pd.DefaultPageSettings.Landscape = true;  //设置横向打印,不设置默认是纵向的
            pd.PrintController = new System.Drawing.Printing.StandardPrintController();
            pd.Print();
        }
        private void printDocument_Print1(object sender, PrintPageEventArgs e)
        {
            string fontName = "隶书";

            PrintDocument tmp = (PrintDocument)sender;
            Graphics g = e.Graphics;
            g.Clear(Color.White);
            Pen myPen = new Pen(Color.FromArgb(255, Color.Black), 1.0F);

            //原点坐标
            int o_w = 0;
            int o_h = 0;

            //g.DrawLine(myPen, new Point(0, 0), new Point(10, 0));
            //g.DrawLine(myPen, new Point(10, 0), new Point(10, 10));
            //g.DrawLine(myPen, new Point(0, 0), new Point(0, 10));
            //g.DrawLine(myPen, new Point(0, 10), new Point(10, 10));

            //标题
            string title = "宇宙人酒楼"   "宴席单";
            //预订桌台
            string ths = "预订桌台:" "201(国色天香),202(难忘今宵),203(梅兰竹菊),204(满天星),205(锦水湖畔),206(小鸟依人),207(王者至尊)";
            int len = GetByteLength(ths);
            int blen = ths.Length;
            if (len > 86) {
                int xlen = blen * 86 / len;
                ths = ths.Substring(0, xlen)   "…";
            };
            //预定人姓名
            string ydName = "预订人姓名:"   "张三";
            string ydZs = "预订桌数:"   "2桌";
            string ydDate = "预订日期:"   "2020年12月01日";
            string ydBc = "预订餐次:"   "晚餐";

            g.DrawString(title, new Font(fontName, 24, FontStyle.Bold), Brushes.Black, 340, o_h);
            g.DrawString(ths, new Font(fontName, 14, FontStyle.Bold), Brushes.Black, o_w, 50);
            g.DrawString(ydName, new Font(fontName, 14, FontStyle.Bold), Brushes.Black, o_w, 80);
            g.DrawString(ydZs, new Font(fontName, 14, FontStyle.Bold), Brushes.Black, 240, 80);
            g.DrawString(ydDate, new Font(fontName, 14, FontStyle.Bold), Brushes.Black, 420, 80);
            g.DrawString(ydBc, new Font(fontName, 14, FontStyle.Bold), Brushes.Black, 720, 80);
            //边框
            g.DrawLine(myPen, new Point(o_w, 110), new Point(900, 110));
            g.DrawLine(myPen, new Point(900, 110), new Point(900, 650));
            g.DrawLine(myPen, new Point(o_w, 110), new Point(o_w, 650));
            g.DrawLine(myPen, new Point(o_w, 650), new Point(900, 650));
            g.DrawLine(myPen, new Point(o_w, 560), new Point(900, 560));

            //g.DrawLine(myPen, new Point(890, 640), new Point(900, 640));
            //g.DrawLine(myPen, new Point(900, 640), new Point(900, 650));
            //g.DrawLine(myPen, new Point(890, 650), new Point(900, 650));
            //g.DrawLine(myPen, new Point(890, 640), new Point(890, 650));
        }

        /// <summary>
        /// 获取字符所占半角宽度个数
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public int GetByteLength(string str)
        {
            int j = 0;
            for (int i = 0; i < str.Length; i  )
            {
                if ((int)str[i] > 127)
                {
                    j  ; j  ;
                }
                else
                {
                    j  ;
                }
            }
            return j;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            int s1 = GetByteLength("(长)");
            int s2 = GetByteLength("(长)");
            MessageBox.Show(s1.ToString());
            MessageBox.Show(s2.ToString());
        }

        private void button5_Click(object sender, EventArgs e)
        {
            string fontName = "隶书";

            Bitmap bmp = new Bitmap(1200, 700);
            Graphics g = Graphics.FromImage((Image)bmp);
            g.Clear(Color.White);

            Pen myPen = new Pen(Color.FromArgb(255, Color.Black), 1.0F);

            //原点坐标
            int o_w = 0;
            int o_h = 0;

            //标题
            string title = "玉金香酒楼"   "宴席单";
            //预订桌台
            string ths = "预订桌台:"   "201(国色天香),202(难忘今宵),203(梅兰竹菊),204(满天星),205(锦水湖畔),206(小鸟依人),207(王者至尊)";
            int len = GetByteLength(ths);
            int blen = ths.Length;
            if (len > 86)
            {
                int xlen = blen * 86 / len;
                ths = ths.Substring(0, xlen)   "…";
            };
            //预定人姓名
            string ydName = "预订人姓名:"   "张三";
            //预订桌数
            string ydZs = "预订桌数:"   "2桌";
            //预订日期
            string ydDate = "预订日期:"   "2020年12月01日";
            //预订餐次
            string ydBc = "预订餐次:"   "晚餐";
            //预订菜品
            DataTable cp_dt = null;
            errMsg = "";
            dt = null;
            sql = ""  
                "SELECT "  
                    "rtrim(c.cpmc) cpmc, "  
                    "y.sl, "  
                    "rtrim(c.unit) unit, "  
                    "y.price, "  
                    "y.je, "  
                    "isnull(y.remark,'') cpsm, "  
                    "isnull(x.mc,'未知') cplx,c.no cpbm "  
                "FROM "  
                    "yy_dzcd y "  
                    "left outer join cpdb c on y.cpbh=c.no "  
                    "left outer join cpxsl x on x.no=c.cpxsl "  
                "WHERE "  
                    "y.no="   "217"   " "  
                "ORDER BY x.mc ASC; "  
                "";
            if (db.getDataTable(sql, out dt, out errMsg))
            {
                cp_dt = dt;
            }
            else
            {
                MessageBox.Show("错误,获取预订菜品失败!");
                return;
            };
            //获取台面备注
            string o_tmbz = "";
            successMsg = "";
            errMsg = "";
            dt = null;
            sql = ""  
                "SELECT "  
                    "rtrim(remark) result "  
                "FROM "  
                    "yydc "  
                "WHERE "  
                    "no="   "217"   " "  
                "";
            if (db.getTheData(sql, out successMsg, out errMsg))
            {
                o_tmbz = successMsg;
            }
            else
            {
                MessageBox.Show("错误,获取台面备注失败!");
                return;
            };

            int h = 0;
            int h_h = 0;
            int w_w = 0;
            //打印头部
            g.DrawString(title, new Font(fontName, 24, FontStyle.Bold), Brushes.Black, 340, o_h   h);
            h  = 50;
            g.DrawString(ths, new Font(fontName, 14, FontStyle.Regular), Brushes.Black, o_w, h);
            h  = 30;
            g.DrawString(ydName, new Font(fontName, 14, FontStyle.Regular), Brushes.Black, o_w, h);
            g.DrawString(ydZs, new Font(fontName, 14, FontStyle.Regular), Brushes.Black, 240, h);
            g.DrawString(ydDate, new Font(fontName, 14, FontStyle.Regular), Brushes.Black, 420, h);
            g.DrawString(ydBc, new Font(fontName, 14, FontStyle.Regular), Brushes.Black, 720, h);
            //打印边框
            g.DrawLine(myPen, new Point(o_w, 110), new Point(900, 110));
            g.DrawLine(myPen, new Point(900, 110), new Point(900, 650));
            g.DrawLine(myPen, new Point(o_w, 110), new Point(o_w, 650));
            g.DrawLine(myPen, new Point(o_w, 650), new Point(900, 650));
            g.DrawLine(myPen, new Point(o_w, 560), new Point(900, 560));

            g.DrawLine(myPen, new Point(290, 110), new Point(290, 560));
            g.DrawLine(myPen, new Point(580, 110), new Point(580, 560));

            //打印菜品
            h  = 45;
            h_h = h;
            if (cp_dt.Rows.Count > 0)
            {
                string old_cplx = "";
                foreach (DataRow dr in cp_dt.Rows)
                {
                    string o_cplx = Convert.ToString(dr["cplx"]);
                    string o_cpmc = Convert.ToString(dr["cpmc"]);
                    string o_sl = Convert.ToString(dr["sl"]);
                    string o_unit = Convert.ToString(dr["unit"]);

                    if (h > 510)
                    {
                        h = h_h;
                        w_w  = 290;
                    };

                    if (old_cplx != o_cplx)
                    {
                        old_cplx = o_cplx;
                        g.DrawString(old_cplx, new Font(fontName, 15, FontStyle.Bold), Brushes.Black, o_w   w_w   15, h);
                        h  = 20;
                        g.DrawString("  "   o_cpmc, new Font(fontName, 13, FontStyle.Regular), Brushes.Black, o_w   w_w   15, h);
                        g.DrawString(o_sl   o_unit, new Font(fontName, 13, FontStyle.Regular), Brushes.Black, o_w   w_w   230, h);
                        h  = 20;
                    }
                    else
                    {
                        g.DrawString("  "   o_cpmc, new Font(fontName, 13, FontStyle.Regular), Brushes.Black, o_w   w_w   15, h);
                        g.DrawString(o_sl   o_unit, new Font(fontName, 13, FontStyle.Regular), Brushes.Black, o_w   w_w   230, h);
                        h  = 20;
                    };

                };
            };
            //打印备注
            g.DrawString("备注:", new Font(fontName, 15, FontStyle.Bold), Brushes.Black, o_w   15, 575);
            g.DrawString("  "   o_tmbz, new Font(fontName, 13, FontStyle.Regular), Brushes.Black, o_w   15, 600);

            pictureBox1.Image = bmp;
        }
    }
}