这个是技术服务提供的代码,但是我们需要c#的,之前也没有做跟硬件接口类的所以不太熟悉,请教大家应该怎么写?根据我的理解,可能需要传一个string入打印机,应该怎么作?Hi all,
 
When printing the label by your applications, please replace those red colour data by your parameters. The size and the location of the TEXTs may need to modify to create a better view.
 
ofstream fout;                        //declare file-stream handle
fout.open("lpt1:", ios::out);      //open out to lpt1:
fout<<"^XA";                         // start ZPL command
fout<<"^FO11,19^A0N,17,16^FDYXX^FS";                                    // define the locate of the printout and the size of the font is 17 x 16
fout<<"^FO45,19^A0N,17,16^FDXXXXX^FS";
fout<<"^FO95,19^A0N,17,16^FDAA^FS";
fout<<"^FO121,19^A0N,17,16^FDB^FS";
fout<<"^FO11,40^A0N,13,12^FDMM/DD/YY^FS";                          // define the locate of the printout and the size of the font is 13 x 12
fout<<"^FO81,40^A0N,13,12^FDC^FS";
fout<<"^FO103,40^A0N,13,12^FDD^FS";
fout<<"^FO11,59^A0N,13,12^FDPF^FS";
fout<<"^FO45,60^A0N,13,12^FDKF^FS";
fout<<"^FO73,60^A0N,13,12^FDDD^FS";
fout<<"^FO95,61^A0N,13,12^FDI^FS";
fout<<"^BY1,3,15^FO11,80^BCN,,Y,N^FD0705610065^FS";         // print barcode 128 with human readable
fout<<"^PQ1";                      //print one label
fout<<"^XZ";                        //sending end of zpl Print one
fout.close();                         //close file Please contact me if any unclear.

解决方案 »

  1.   

    定义和这个一样的stream,按照这样的格式写进去阿
      

  2.   

    照片这会肯定没有办法上了,我暂时这样写的,但是不详调用PD出来,一会在调整以下
            private void button1_Click(object sender, EventArgs e)
            {
                if (((textBox1.Text == "") || (textBox2.Text == "") || (textBox3.Text == "") || (comboBox1.Text == "")
                    || (comboBox2.Text == "") || (comboBox3.Text == "") || (comboBox4.Text == "") || (comboBox5.Text == "")) == true)
                {
                    MessageBox.Show("请填写每一项!!");
                    return;
                }            GetNo();
                //zpl command line starts here
                string s = "^XA^FO11,19^A0N,17,16^FDYXX^FS^FO45,19^A0N,17,16^FDXXXXX^FS^FO95,19^A0N,17,16^FDAA^FS^FO121,19^A0N,17,16^FDB^FS^FO11,40^A0N,13,12^FDMM/DD/YY^FS^FO81,40^A0N,13,12^FDC^FS^FO103,40^A0N,13,12^FDD^FS^FO11,59^A0N,13,12^FDPF^FS^FO45,60^A0N,13,12^FDKF^FS^FO73,60^A0N,13,12^FDDD^FS^FO95,61^A0N,13,12^FDI^FS^BY1,3,15^FO11,80^BCN,,Y,N^FD0705610065^FS^PQ1^XZ ";            PrintDialog pd = new PrintDialog();
                pd.PrinterSettings = new PrinterSettings();            if (DialogResult.OK == pd.ShowDialog(this))
                {
                    // Send a printer-specific to the printer.
                    RawPrinterHelper.SendStringToPrinter(pd.PrinterSettings.PrinterName, s);            }
            }
    其中string s的那一串就是ZPL命令
      

  3.   

    ---------------------------------------------
    定义和这个一样的stream,按照这样的格式写进去阿
    ---------------------------------------------就是想请教这个,IO那块不熟悉,帮忙写个example看看先!!!!
      

  4.   

    最讨厌Zebra的ZPL语言,写得相当麻烦。应该有提供DLL,楼主可以去看看。
      

  5.   

    以下提供了一个LPT类,直接调用即可:public class LPTControl
    {
    [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,
    ref int lpNumberOfBytesWritten,
    ref OVERLAPPED lpOverlapped
    );
    [DllImport("kernel32.dll")]
    private static extern bool CloseHandle(
    int hObject
    );
    private int iHandle;
    public bool Open()
    {
    iHandle=CreateFile("lpt1",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
    ref i,ref x);
    }
    else
    {
    Throw new Exception("端口未打开!");
    }
    }
    public bool Close()
    {
    return CloseHandle(iHandle);
    }
    }
      

  6.   

    是这个么?
    ---------------------------------------------------------------/// <summary>
        ///在C#中调用windows API
        /// </summary>
        public class RawPrinterHelper    {        // Structure and API declarions:
            [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
            public class DOCINFOA
            {
                [MarshalAs(UnmanagedType.LPStr)]
                public string pDocName;            [MarshalAs(UnmanagedType.LPStr)]
                public string pOutputFile;            [MarshalAs(UnmanagedType.LPStr)]
                public string pDataType;        }        [DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
            public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, long pd);         [DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
            public static extern bool ClosePrinter(IntPtr hPrinter);         [DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
            public static extern bool StartDocPrinter(IntPtr hPrinter, Int32 level, [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);         [DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
            public static extern bool EndDocPrinter(IntPtr hPrinter);         [DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
            public static extern bool StartPagePrinter(IntPtr hPrinter);         [DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
            public static extern bool EndPagePrinter(IntPtr hPrinter);         [DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
            public static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, Int32 dwCount, out Int32 dwWritten);
      

  7.   

    上面这个是在斑马机的website上面找到的一个c #例子,但是我不太熟悉windows API和DLL的调用,能不能给个解释?
      

  8.   

    还是要应用到Win API,如果是纯的C#是实现不了的,功能并不强大!!
      

  9.   

    是的,上面的就是使用DLL列印条码。就不必记那些烦人的ZPL
      

  10.   

    岂不杀完全不需要斑马机厂的支持了?
    现在我们就是需要运用ZPL,当然前提是在他们提供帮助的情况下.我们告诉他们我想要达到的效果然后他们提供ZPL命令,我们要做的就是把这个命令传给打印机.怎么做?
      

  11.   

    呵呵,其实你可以借助第三方软件codesoft或labelview的dll文件实现打印.  我前段时间就是这样做的.这样的好处就是不管是什么类型的条码打印机,只要直接做好标签样板文件就可有通过自己编写的程序打印.
      

  12.   


    private void Form1_Load(object sender, System.EventArgs e)
    {
    LabelApp=new LabelApplicationClass();
    doc=(LabelView.LabelDocument)LabelApp.ActiveDocument();
    doc.Open("c:\\worklog\\label.lbl",true);
    }private void PrintLabel(string result)
    {
    try
    {

    LabelView.LabelField snLabel=(LabelView.LabelField)((LabelView.LabelFields)doc.LabelFields).Item("SN");
    snLabel.Value=result;
    doc.LabelSetup();
    doc.PrintLabel(2,null,null,null,null,null,null);


    }
    catch(Exception err)
    {
    MessageBox.Show(err.ToString(),"提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
    }
    }
    private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
    LabelApp.Quit();
    LabelApp = null;
    System.GC.Collect();

    }
      

  13.   

    哈哈,什么观念啊-_-如果他们提供ZPL的话,那么直接用我上面提供的LPT类来发送ZPL就可以了。LPTControl.Write("^XA^LH15,5^PRC^LL180");
      

  14.   

    多谢了,正研究中
    兄弟有没有联系方式可以交流以下 我的[email protected]
      

  15.   

    .......对啊,很奇怪么?呵呵
    那边技术支持又来一个新方案,这个怎么实现?> 你好,
    >    如果直接打开端口的方法不行的话,可以尝试用文本打印驱动,在打印机中添加 "Generic / Text
    > Only"然后直接把文本打印到 Generic / Text Only 打印机中。
    > 添加打印机,选择 制造商为"Generic" 打印机为 "Generic / Text Only "的打印机,该打印机为WINDOWS 自带的。
      

  16.   

    噢,这个方案看来是备用方案。
    始终觉得调用DLL是最好的方法。
      

  17.   

    恩,如果可以的话调用API自然是最好
    我把你给我的那个LPTControl类copy进去,优点小问题你给解决一下:
                public bool Write(String Mystring)
                {
                    private int i;
                    OVERLAPPED x;                if(iHandle !=-1)
                    {                    byte[] mybyte=System.Text.Encoding.Default.GetBytes(Mystring);
                        return WriteFile(iHandle,mybyte,mybyte.Length,ref i,ref x);
                    }
                    else
                    {
                        throw new Exception("端口未打开!");
                    }
                }这个里面的ref i 和ref x编译通过不了,说为赋值.
    我不太明白wirte函数申明中这两个参数的意思,能说明下么?
                ref int lpNumberOfBytesWritten,
                ref OVERLAPPED lpOverlapped
      

  18.   

    1.两个参数:
    1)lpNumberOfBytesWritten这个参数返回实际的写入字节数.
    2)lpOverlapped这个参数指定了当前操作的选项:如是否写完再运行程序...2.另外这边需要改成:
    [DllImport("kernel32.dll")]
            private static extern bool WriteFile(
                int hFile,
                byte[] lpBuffer,
                int nNumberOfBytesToWrite,
                out int lpNumberOfBytesWritten,
                out OVERLAPPED lpOverlapped
                );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("端口未打开!");
                }
            }
      

  19.   

    多谢kssys兄弟,为什么吧ref改成out?
    今天斑马机的技术支持过来了,根据我们要打印的标签要求设置好了ZPL
    现在剩下的任务就是把lpt1口打开
    利用你上面给我的调用api的方法是其中一条路,另外一个可以设置一个文本打印机然后将zpl命令作为一个文本文件打印输出,第二种方法我还不是很理解,能解释一下么?怎么样实现呢?kssys兄最好有msn或者qq或者email,我好实时请教您问题,再次感谢