2 panjy12 panjy12 于 2014.06.12 11:18 提问

C#在b/s模式下调用斑马打印机,无反应,求助~~

从网上搜得代码如下,有的说可以用,有的说报错,我的就是没有反应,
斑马打印机ZT210,Fnthex32.dll文件33K的和24K的都试过。

代码如下:

类:
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;

namespace Barcode
{
/*条码打印命令说明
^XA //条码打印指令开始
^MD30 //设置色带颜色的深度, 取值范围从-30到30
^LH60,10 //设置条码纸的边距
^FO20,10 //设置条码左上角的位置
^ACN,18,10 //设置字体
^BY1.4,3,50 //设置条码样式。1.4是条码的缩放级别,3是条码中粗细柱的比例, 50是条码高度
^BC,,Y,N //打印code128的指令
^FD12345678^FS //设置要打印的内容, ^FD是要打印的条码内容^FS表示换行
^XZ //条码打印指令结束
*/
//上面的指令会打印12345678的CODE128的条码
public class BarcodePrint
{
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
private struct OVERLAPPED
{
int Internal;
int InternalHigh;
int Offset;
int OffSetHigh;
int hEvent;
}
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
private static extern int CreateFile(string lpFileName, uint dwDesiredAccess, int dwShareMode, int lpSecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, int hTemplateFile);
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
private static extern bool WriteFile(int hFile, byte[] lpBuffer, int nNumberOfBytesToWrite, out int lpNumberOfBytesWritten, out OVERLAPPED lpOverlapped);
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
private static extern bool CloseHandle(int hObject);
private int iHandle;
public bool Open()
{
iHandle = CreateFile("LPT1:", (uint)FileAccess.ReadWrite, 0, 0, (int)FileMode.Open, 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("LPT1端口未打开!");
}
}
public bool Close()
{
return CloseHandle(iHandle);
}
}
}

程序代码:
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Barcode;

public partial class Printer : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string sBarCodeCMD = ""; //条码打印命令
StringBuilder sb1 = new StringBuilder(10240);
int i1;
i1 = GETFONTHEX("测试","宋体","temp1",0,30,20,0,0,sb1);
sBarCodeCMD = sb1.ToString() + "^XA^MD30^LH20,20^FO20,20^XGtemp1,1,1^FS^XZ";
}

[System.Runtime.InteropServices.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
    );

/*条码打印命令说明
^XA //条码打印指令开始
^MD30 //设置色带颜色的深度, 取值范围从-30到30
^LH60,10 //设置条码纸的边距
^FO20,10 //设置条码左上角的位置
^ACN,18,10 //设置字体
^BY1.4,3,50 //设置条码样式。1.4是条码的缩放级别,3是条码中粗细柱的比例, 50是条码高度
^BC,,Y,N //打印code128的指令
^FD12345678^FS //设置要打印的内容, ^FD是要打印的条码内容^FS表示换行
^XZ //条码打印指令结束
*/
//上面的指令会打印12345678的CODE128的条码

}

后来在程序代码里加了一个按钮,很一行代码下面加一个输出,看会不会卡在哪一步,结果12345全都输出了!!!
protected void Button1_Click(object sender, EventArgs e)
{
string sBarCodeCMD = ""; //条码打印命令
Response.Write("1");
StringBuilder sb1 = new StringBuilder(10240);
Response.Write("2");
int i1;
Response.Write("3");
i1 = GETFONTHEX("测试", "宋体", "temp1", 0, 30, 20, 0, 0, sb1);
Response.Write("4");
sBarCodeCMD = sb1.ToString() + "^XA^MD30^LH20,20^FO20,20^XGtemp1,1,1^FS^XZ";
Response.Write("5");
}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!