post 机打印小票 c#篇

来源:互联网 发布:软件英文 编辑:程序博客网 时间:2024/06/05 17:54

using System;
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 org.in2bits.MyXls;
using System.Runtime.InteropServices;
using System.IO;
using Microsoft.Win32.SafeHandles;
namespace WebTouch
{
   public partial class products_cart : System.Web.UI.Page
   {
     
       const uint GENERIC_READ =0x80000000;
       const uint GENERIC_WRITE = 0x40000000;      
       const int OPEN_EXISTING = 3;
       private FileStream fs=null;
       [DllImport("kernel32.dll")]
       public static externIntPtr CreateFileA(string lpFileName, uint dwDesiredAccess, intdwShareMode, int lpSecurityAttributes, int dwCreationDisposition, intdwFlagsAndAttributes, int hTemplateFile);
   
       protected void Page_Load(object sender, EventArgs e)
       {
           if (!this.IsPostBack)
           {
               if (!this.Request.IsAuthenticated)
               {
                   string str = Request.Url.ToString();
                   string url = "myinfo_login.aspx?RetUrl=" + str + "";
                   //string url = "myinfo_login.aspx";
                   Jscript.JavaScriptLocationHref(url);
               }
               else
               {
                   Bind();
               }
               string name = "";
               if (Request.IsAuthenticated)
               {
                   name = User.Identity.Name;
               }
               else
               {
                   name = "未注册用户";
               }
               string Reurl = Request.RawUrl.ToString();
               TJ t = new TJ();
               if (t.addTG(name, Reurl) == true)
               {
                   //Jscript.JavaScriptLocationHref(url + "&TJ=1");
               }

           }
       }
       private void Bind()
       {
           ShopCart mycart = CartCtroller.GetCart();
           bandToData(AspNetPager2, mycart, this.rpdata);  
       }
     
       private void bandToData(Wuqi.Webdiyer.AspNetPager anp,ArrayList ar,  Repeater ppt)
       {
           anp.RecordCount = ar.Count;
           PagedDataSource pds = new PagedDataSource();
           pds.DataSource = ar;
           pds.AllowPaging = true;
           pds.CurrentPageIndex = anp.CurrentPageIndex - 1;
           pds.PageSize = anp.PageSize;
           ppt.DataSource = pds;
           ppt.DataBind();
       }
       protected void AspNetPager1_PageChanged(object sender, EventArgs e)
       {
           Bind();
       }
       protected void Submit1_ServerClick(object sender, EventArgs e)
       {
         
           del();
           Jscript.JavaScriptLocationHref("products_cart.aspx");
       }
       private void del()
       {
           ShopCart mycart = null;
           foreach (Control c in this.rpdata.Controls)
           {
               HtmlInputCheckBox cbx = (HtmlInputCheckBox)c.FindControl("Item");
               //CheckBox cbx = (CheckBox)c.FindControl("cbxId");
               if (this.CheckAll.Checked == true || this.checkAllall.Checked == true)
               {
                   CartCtroller.CleanCart();
                   mycart = CartCtroller.GetCart();
                   mycart.Clear();
                   return;
               }
               else
               {
                   if (cbx != null)
                   {
                       if (cbx.Checked == true)
                       {
                           int T_id = int.Parse(cbx.Value);
                           if (T_id >= 0)
                           {
                               mycart = CartCtroller.GetCart();
                               //mycart.RemoveRange(
                               mycart.RemoveAt(T_id);
                               //Bind();
                           }
                           else
                           {
                               return;
                           }
                       }
                   }
               }
           }
           CartCtroller.StoreCart(mycart);
       }
           
       protected void imageButton1_Click(object sender, ImageClickEventArgs e)
       {
           PrintESC(0);
           ShopCart mycart = CartCtroller.GetCart();
           IntPtr iHandle;
          iHandle = CreateFileA("LPT1", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
          if (iHandle.ToInt32() == -1)
          {
              Jscript.AjaxRunJs(this.Page, "alert('没有连接打印机或者打印机端口不是LPT1!')");
              return;
          }
          else
          {              
              fs = new FileStream(iHandle, FileAccess.ReadWrite);
              StreamReader sr = new StreamReader(fs);   //读数据;  
              StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Default);  //些数据;  
              if (mycart.Count == 0)
              {
                  Jscript.AjaxRunJs(this.Page, "alert('请先购买产品!')");
                  return;
              }
           
              for (int i = 0; i < mycart.Count; i++)
              {
                  modelArrayProduct temp = (modelArrayProduct)mycart[i];
                  string name = "产品名称:" + temp.T_name;
                  string miaoshu = "产品描述:" + temp.T_miaoshu;
                  string shuliang = "购买数量:" + temp.T_shuliang.ToString() + "支";
                 
                 
                  //fs.WriteByte(0x0A);
                  sw.WriteLine(name);
                  sw.WriteLine(miaoshu);
                  sw.WriteLine(shuliang);
                  sw.WriteLine("-------------------------------");
                  PrintESC(1);
                 
                 
              }
              sw.Close();
              fs.Close();
           
          }
       
          //PrintESC(2);
          //PrintESC(2);
          //PrintESC(2);
          //PrintESC(2);
          //PrintESC(2);
       }
       //打印前退纸 0
       //打印
       //打印后进纸1
         /// <summary>
       /// ESC/P 指令
       /// </summary>
       /// <param name="iSelect">0:退纸命令 1:进纸命令 2:换行命令</param>
       public void PrintESC(int iSelect)
       {
           string send;
           IntPtr iHandle;
         
           iHandle = CreateFileA("LPT1", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
           if (iHandle.ToInt32() == -1)
           {
               Jscript.AjaxRunJs(this.Page, "alert('没有连接打印机或者打印机端口不是LPT1!')");
               return;              
           }
           else
           {
                fs = new FileStream(iHandle, FileAccess.ReadWrite);
           }
         
         
           byte[] buf = new byte[80];
           switch (iSelect)
           {
               case 0:
                   send = "" + (char)(27) + (char)(64) + (char)(27) + 'j' + (char)(255);    //退纸1 255 为半张纸长
                   send = send + (char)(27) + 'j' + (char)(125);    //退纸2
                   break;
               case 1:
                   send = "" + (char)(27) + (char)(64) + (char)(27) + 'J' + (char)(255);    //进纸
                   break;
               //case 1:
               //    send = "" +(char)(64) + (char)(64) + (char)(64) + (char)(64) + 'J' +(char)(255);   //进纸                  
               //    break;
               case 2:
                   send = "" + (char)(27) + (char)(64) + (char)(12);   //换行
                   break;
               default:
                   send = "" + (char)(27) + (char)(64) + (char)(12);   //换行
                   break;
           }
           for (int i = 0; i < send.Length; i++)
           {
               buf[i] = (byte)send[i];
           }
           fs.Write(buf, 0, buf.Length);
           fs.Close();
       }

   }
}