C#打印条码到Zebra打印机

建立一个winform程序,添加两个textbox,一个button。 其中一个textbox设为允许多行 textbox1, 一个为txtport,

 

新增一个类文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace WinForm_Print
{
    public class PrinterControl
    {
        [StructLayout(LayoutKind.Sequential)]
        private struct OVERLAPPED
        {
            int Internal;
            int InternalHigh;
            int Offset;
            int OffSetHigh;
            int hEvent;
        }

        [DllImport("kernel32.dll ")]
        private static extern int CreateFile(
        string lpFileName,
        uint dwDesiredAccess,
        int dwShareMode,
        int lpSecurityAttributes,
        int dwCreationDisposition,
        int dwFlagsAndAttributes,
        int hTemplateFile
        );


        [DllImport("kernel32.dll ")]
        private static extern bool WriteFile(
        int hFile,
        byte[] lpBuffer,
        int nNumberOfBytesToWrite,
        out   int lpNumberOfBytesWritten,
        out   OVERLAPPED lpOverlapped
        );


        [DllImport("kernel32.dll ")]
        private static extern bool CloseHandle(
        int hObject
        );

        private int iHandle;

        public bool Open(string strPort)
        {
            iHandle = CreateFile(strPort, 0x40000000, 0, 0, 3, 0, 0);
            if (iHandle != -1)
            {
                return true;
            }
            else
            {
                return false;
            }

        }


        public bool Write(String Mystring)
        {
            if (iHandle != -1)
            {
                int i;
                OVERLAPPED x;
                byte[] mybyte = System.Text.Encoding.Default.GetBytes(Mystring);
                return WriteFile(iHandle, mybyte, mybyte.Length, out   i, out   x);
            }
            else
            {
                throw new Exception("端口未打开! ");
            }
        }

        public bool Close()
        {
            return CloseHandle(iHandle);
        }
    }
}

 

write函数也可为, 其中的 iHandle 要改为 IntPtr 类型

        public bool Write(String Mystring)
        {
            if (iHandle.ToInt32() != -1)
            {
                int i;
                OVERLAPPED x;

                FileStream fs = new FileStream(iHandle, FileAccess.ReadWrite);
                StreamWriter sw = new StreamWriter(fs, Encoding.Default);
                sw.WriteLine(Mystring);
                sw.Close();
                fs.Close();
                return true;
            }
            else
            {
                throw new Exception("端口未打开! ");
            }
        }


 


button后台代码为

 

        private void button1_Click(object sender, EventArgs e)
        {
            //printDialog1.Document = printDocument1;
            //DialogResult result = printDialog1.ShowDialog();
            //if (result == DialogResult.OK)
            //printDocument1.Print();

            PrinterControl lpt = new PrinterControl();
            string mycommanglines = textBox1.Text;//System.IO.File.ReadAllText("zplII.txt");//zplII.txt里写了条码机的命令
            lpt.Open(txtPort.Text);
            lpt.Write(mycommanglines).ToString();
            lpt.Close();
        }

 

 

打印中文方法

1.调用函数GETFONTHEX()该函数的功能是将传入的中文字符串转换为十六进制编码并返回。

2.函数使用的C#原型

[DllImport("fnthex32.dll")]

public static extern  int GETFONTHEX(string barcodeText,string fontName,string fileName,int orient,int height,int width,int isBold,int isItalic,StringBuilder returnBarcodeCMD); 


3.实例

调用下面条码指令打印会打印“测试”二个汉字)

string sBarCodeCMD = string.Empty;    //条码打印命令

StringBuilder sb1 = new StringBuilder(10240);    //用给定的容量创建一个空的StringBuilder

int  li = GETFONTHEX(“测试”,“宋体”,“temp1”,0,30,20,0,0,sb1); //转换为HEX编码(16进制)

sBarCodeCMD = sb1.ToString() + "^XA^MD30^LH20,20^FO20,20^XGtemp1,1,1^FS^XZ";



http://wenku.baidu.com/view/83387ceb172ded630a1cb605.html

http://hi.baidu.com/sunflower/item/3544cd9e56bf28dc1e427169

http://wenku.baidu.com/view/9e4a8d65caaedd3383c4d3d6.html 含中文打印
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值