Exemple #1
0
 public bool Write(byte[] mybyte)
 {
     if (this.iHandle != -1)
     {
         LptControl.OVERLAPPED oVERLAPPED = default(LptControl.OVERLAPPED);
         int num = 0;
         return(LptControl.WriteFile(this.iHandle, mybyte, mybyte.Length, ref num, ref oVERLAPPED));
     }
     throw new Exception("[Pos 3002] 不能连接到打印机!");
 }
Exemple #2
0
 public bool Write(string Mystring)
 {
     if (this.iHandle != -1)
     {
         LptControl.OVERLAPPED oVERLAPPED = default(LptControl.OVERLAPPED);
         int    num   = 0;
         byte[] bytes = Encoding.GetEncoding("GB2312").GetBytes(Mystring);
         return(LptControl.WriteFile(this.iHandle, bytes, bytes.Length, ref num, ref oVERLAPPED));
     }
     throw new Exception("[Pos 3001] 不能连接到打印机!");
 }
Exemple #3
0
 public bool Close()
 {
     return(LptControl.CloseHandle(this.iHandle));
 }
Exemple #4
0
 public bool Open()
 {
     this.iHandle = LptControl.CreateFile(this.LptStr, 1073741824u, 0, 0, 3, 0, 0);
     return(this.iHandle != -1);
 }
Exemple #5
0
        private string PrintNewList(List <PrintLineModel> lines)
        {
            if (lines == null || lines.Count == 0)
            {
                throw new Exception("[Pos 1000] 无打印数据!");
            }
            string appConfig = Utils.GetAppConfig("LptStr");

            this.lc = new LptControl(appConfig);
            if (!this.lc.Open())
            {
                throw new Exception("[Pos 1001] 设备驱动失败!");
            }
            try
            {
                LogTools.Info("开始打印");
                #region 循环打印
                for (int i = 0; i < lines.Count; i++)
                {
                    PrintLineModel m = lines[i];

                    ////LPT打印,不用手动隔行,因为它不会重叠


                    switch (m.type)
                    {
                    case 1:
                    //图片和条形码都视作图片打印
                    case 2:
                        this.lc.Write(new byte[]
                        {
                            27,
                            64
                        });
                        this.lc.Write(new byte[]
                        {
                            27,
                            33,
                            48
                        });
                        this.lc.Write(new byte[]
                        {
                            27,
                            97,
                            1
                        });
                        //打印图片或条形码
                        try
                        {
                            this.Print2Bitmap(m.content, "tmpBmp.bmp", m.splitLine);
                        }
                        catch (Exception ex)
                        {
                            string sErr = string.Format("[Pos 1003] 打印图片{0},下载地址:{2}。\r\n异常:{1}"
                                                        , "tmpBmp.bmp", ex.ToString(), m.content);
                            //throw new Exception();
                            LogTools.Debug(sErr);
                        }

                        LptControl arg_164_0 = this.lc;
                        byte[]     array     = new byte[3];
                        array[0] = 27;
                        array[1] = 33;
                        arg_164_0.Write(array);
                        LptControl arg_186_0 = this.lc;
                        byte[]     array2    = new byte[3];
                        array2[0] = 27;
                        array2[1] = 97;
                        arg_186_0.Write(array2);
                        //this.lc.Write(Environment.NewLine);
                        break;

                    case 3:
                        StringBuilder sb = new StringBuilder();
                        switch (m.fontSize)
                        {
                        case 1:
                            sb.Append(_1w1h);
                            break;

                        case 2:
                            sb.Append(_1w2h);
                            break;

                        case 3:
                            sb.Append(_2w2h);
                            break;

                        default:
                            break;
                        }
                        sb.Append(m.content + Environment.NewLine);
                        this.lc.Write(sb.ToString());
                        //LogTools.Info(string.Format("打印第{0}行,内容:{1}", i.ToString(), sb.ToString()));
                        for (int j = 0; j < m.splitLine - 2; i++)
                        {
                            this.lc.Write(" " + Environment.NewLine);
                        }
                        break;

                    default:
                        break;
                    }
                }
                #endregion
                #region 打印尾巴
                this.lc.Write(" " + Environment.NewLine);
                this.lc.Write(" " + Environment.NewLine);
                this.lc.Write(" " + Environment.NewLine);
                this.lc.Write(" " + Environment.NewLine);
                this.lc.Write(" " + Environment.NewLine);
                this.lc.Write(" " + Environment.NewLine);
                this.lc.Write(new byte[]
                {
                    27,
                    100,
                    3
                });
                this.lc.Write(new byte[]
                {
                    27,
                    109
                });
                LogTools.Info("打印完毕,切纸");
                #endregion
                return("");
            }
            catch (Exception ex4)
            {
                throw new Exception("[Pos 1005] 打印失败," + ex4.Message);
            }
            finally
            {
                this.lc.Close();
            }
        }