使用C# WinForm实现打印小票的功能

1 打印效果

在这里插入图片描述

2 生成二维码和条形码的dll

条码和二维码生成的DLL

3 二维码生成类QRcodeCreator

using ThoughtWorks.QRCode.Codec;

namespace LotteryPro
{
    public class QRcodeCreator
    {
        #region 根据链接生成二维码
        /// <summary>
        /// 根据链接获取二维码
        /// </summary>
        /// <param name="url">链接</param>
        /// <returns>返回二维码图片</returns>
        public static Image GetQRCodeBmp(string url)
        {
            QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
            qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
            qrCodeEncoder.QRCodeScale = 4;
            qrCodeEncoder.QRCodeVersion =0;
            qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
            return qrCodeEncoder.Encode(url);
        }
        #endregion
    }
}

4 打印彩票方法编写

/// <summary>
/// 打印彩票
/// </summary>
public void PrintLottery(System.Drawing.Printing.PrintPageEventArgs e, string serialNum, List<string> numList)
{
    //生成条形码
    Fath.BarcodeX barCode = new Fath.BarcodeX();//创建条码生成对象
    barCode.Text = serialNum;//条码数据
    barCode.Symbology = Fath.bcType.Code128;//设置条码格式
    barCode.ShowText = true;//同时显示文本           
    e.Graphics.DrawImage(barCode.Image(240, 50), new Point(20, 5));//画条形码

    //生成彩票信息
    float left = 2; //打印区域的左边界
    float top = 70;//打印区域的上边界
    Font titlefont = new Font("仿宋", 10);//标题字体
    Font font = new Font("仿宋", 8);//内容字体            
    e.Graphics.DrawString("天津百万奖彩票中心", titlefont, Brushes.Blue, left + 20, top, new StringFormat());//打印标题
    //画一条分界线
    Pen pen = new Pen(Color.Green, 1);
    e.Graphics.DrawLine(pen, new Point((int)left - 2, (int)top + 20), new Point((int)left + (int)180, (int)top + 20));

    //循环打印选号
    for (int i = 0; i < numList.Count; i++)
    {
        e.Graphics.DrawString(numList[i], font, Brushes.Blue, left,
            top + titlefont.GetHeight(e.Graphics) + font.GetHeight(e.Graphics) * i + 12, new StringFormat());
    }

    //画一条分界线           
    float topPoint = titlefont.GetHeight(e.Graphics) + font.GetHeight(e.Graphics) * (numList.Count) + 22;

    e.Graphics.DrawLine(pen, new Point((int)left - 2, (int)top + (int)topPoint),
        new Point((int)left + (int)180, (int)top + (int)topPoint));

    //打印时间
    string time = "购买时间:" + DateTime.Now.ToString("yyy-MM-dd  HH:mm:ss");
    e.Graphics.DrawString(time, font, Brushes.Blue, left, top + titlefont.GetHeight(e.Graphics)
        + font.GetHeight(e.Graphics) * (numList.Count + 1) + 12, new StringFormat());

    //二维码图片left和top坐标
    int qrcodetop = (int)(top + titlefont.GetHeight(e.Graphics) + font.GetHeight(e.Graphics) * (numList.Count + 3) + 12);
    int qrcodeleft = (int)left + 32;

    //生成二维码图片
    // Image bmp = QRcodeCreator.GetQRCodeBmp("http://www.xiketang.com/duijiang/query?id=" + serialNum);//直接添加流水号,将来从网站直接查询
    Image bmp = QRcodeCreator.GetQRCodeBmp("http://www.xiketang.com");
    //打印
    e.Graphics.DrawImage(bmp, new Point(qrcodeleft, qrcodetop));//不同的URL图片大小不同,可以根据需要调整left坐标
    e.Graphics.DrawString("扫描二维码可直接查询兑奖结果", font, Brushes.Blue, left, qrcodetop + bmp.Height + 10, new StringFormat());
}     

5 在Winform中使用打印功能

public partial class FrmMain : Form
{
    private PrintDocument printDoc = new PrintDocument();//创建打印对象,可以调用方法设定选中的打印机,目前只有一台,所以不需设置

    #region 初始化
    public FrmMain()
    {
        InitializeComponent();
        //关联打印对象的事件
        this.printDoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.LotteryPrintPage);
    }
    #endregion

    //使用小票打印机打印
    private void btnPrint_Click(object sender, EventArgs e)
    {
        this.printDoc.Print();
    }
    //具体打印实现过程
    private void LotteryPrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        string serialNum = DateTime.Now.ToString("yyyyMMddHHmmssms");//流水号(生成条码使用)
        this.objSelector.PrintLottery(e, serialNum, objSelector.GetPrintedNums());//调用前面编写的打印方法,为了省略代码,objSelector.GetPrintedNums()删除了,其返回类型为List<string>
    }
}

©️2020 CSDN 皮肤主题: 黑客帝国 设计师:上身试试 返回首页