基本信息
源码名称:C#串口、并口、驱动打印功能
源码大小:3.87M
文件格式:.rar
开发语言:C#
更新时间:2019-07-18
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

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

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

封装了串口、并口、驱动打印机的打印,使用esc命令,行内所有打印机通用,网口打印机也是一样的,把esc指令发过去即可。个人可以根据自己的使用习惯来进行封装。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
using LaisonTech.CommonBLL;
using System.Management;
using System.Threading;
using LaisonTech.MediaLib;
using System.Drawing.Printing;

namespace 小票打印Demo
{
    public partial class FormPrint_USB : Form
    { 
        private ReceiptPrinter_USB m_printer = new ReceiptPrinter_USB();

        public FormPrint_USB()
        {
            InitializeComponent();
        }

        private void FormReceipt_Load(object sender, EventArgs e)
        {
            btnRefreshPrinter_Click(null, null);
            
            cboAlign.Items.Clear();
            cboAlign.Items.Add("左对齐");
            cboAlign.Items.Add("居中");
            cboAlign.Items.Add("右对齐");
            cboAlign.SelectedIndex = 0;
        }


        private void btnRefreshPrinter_Click(object sender, EventArgs e)
        {
            cboPrinterList.Items.Clear();
            List<String> printlist = PrinterHelper.GetPrinterList();
            cboPrinterList.Items.AddRange(printlist.ToArray());

            cboPrinterList.SelectedIndex = 0;
        }

        private void btnOpenPrinter_Click(object sender, EventArgs e)
        {
            Console.WriteLine(cboPrinterList.Text);
            Boolean bl = m_printer.Open(cboPrinterList.Text);
            lblInit.Text = bl ? "已连接" : "未连接";
        }

        /// <summary>
        /// 打印
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPrint_Click(object sender, EventArgs e)
        {
            //FormPrint_USB fm = new FormPrint_USB();
            string str = textBoxData.Text;
            m_printer.PrintText(str);
        }

        /// 设置左边距
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSetLeft_Click(object sender, EventArgs e)
        {
            string contentLeft = numericUpDown1.Value.ToString();
            int left = Convert.ToInt32(contentLeft);

            m_printer.SetLeft(left);            
        }

        /// <summary>
        /// 设置字体大小
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSetFontSize_Click(object sender, EventArgs e)
        {
            int size = Convert.ToInt32(numericUpDown2.Value);
            m_printer.SetFontSize(size);
        }

        /// <summary>
        /// 走纸
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnFeed_Click(object sender, EventArgs e)
        {
            int length = Convert.ToInt32(numericUpDown3.Value.ToString());
            m_printer.Feed(length);
        }

        /// <summary>
        /// 切纸
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCut_Click(object sender, EventArgs e)
        {
            m_printer.Cut();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            m_printer.Close(); 
            lblInit.Text =  "未连接";
        }

        private Bitmap m_img = null;
        /// <summary>
        /// 载入并显示图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLoadImage_Click(object sender, EventArgs e)
        {
            String filename = String.Empty;
            Boolean bl = FileProcessor.SelectFile("选择图片文件", Application.StartupPath,
                "Image Files(*.bmp;*.jpg;*.gif;*.png;*.ico)|*.bmp;*.jpg;*.gif;*.png;*.ico",
                out filename);
            if (!bl)
            {
                return;
            }

            Bitmap img = ImageProcessor.LoadBitImage(filename);
            if (img == null)
            {
                return;
            }
            picbox.Image = img; 
            m_img = img;
        }

        /// <summary>
        /// 打印图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPrintImage_Click(object sender, EventArgs e)
        {
            if (m_img == null)
            {
                return;
            }

            m_printer.PrintBitmap(m_img);
        }

        /// <summary>
        /// 设置加粗
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBold_Click(object sender, EventArgs e)
        {
            m_printer.SetBold(true);
        }

        /// <summary>
        /// 取消加粗
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancelBold_Click(object sender, EventArgs e)
        {
            m_printer.SetBold(false);
        }

        private void btnSetAlign_Click(object sender, EventArgs e)
        {
            m_printer.SetAlignMode((eTextAlignMode)cboAlign.SelectedIndex);
        }

        private void btn_startprinter_Click(object sender, EventArgs e)
        {
            m_printer.StartPrint();
        }

        private void btn_endprinter_Click(object sender, EventArgs e)
        {
            m_printer.EndPrint();
        }

        /// <summary>
        /// 打印Unicode
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPrint_Unicode_Click(object sender, EventArgs e)
        {
            string str = textBoxData.Text;
            m_printer.PrintUnicode(str);
        }

        /// <summary>
        /// 打印自测页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Print_Test_Click(object sender, EventArgs e)
        {
            m_printer.PrintTestPage();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            m_printer.PrintBarcode("123456");
        }
    }
}