C# winform打印總結 z


http://blog.csdn.net/jing_xin/article/details/41444063

針對BEIYANG收據打印機 BTP-R580測試通過。

操作說明:http://www.docin.com/p-395110672.html

1、一般的打印

static Font printFont; //打印使用的字體
        public static void PrintEventPage(object sender, PrintPageEventArgs e)
        {

            float yPos = 0;
            int count = 0;
            float leftMargin = e.MarginBounds.Left;
            float topMargin = e.MarginBounds.Top;
            //string line = null;
            //string subs = " ";
            string context = null;


            //打印字體
            printFont = new Font("宋體", 14F, FontStyle.Bold | FontStyle.Underline);
            //打印內容

            context = "收銀小票";
            //打印的位置
            yPos = topMargin + (count * printFont.GetHeight(e.Graphics));
            e.Graphics.DrawString(context, printFont, Brushes.Black,
            50, 10, new StringFormat());
            //換行
            count++;


            e.HasMorePages = false;
        }

 

調用處:

            System.Windows.Forms.PrintDialog PrintDialog1 = new PrintDialog();
            System.Drawing.Printing.PrintDocument docToPrint = new System.Drawing.Printing.PrintDocument();
            docToPrint.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(PrintEventPage);

            PrintDialog1.AllowSomePages = true;
            PrintDialog1.ShowHelp = true;


            PrintDialog1.Document = docToPrint;//把PrintDialog的Document屬性設為上面配置好的PrintDocument的實例
            PrintController printController = new StandardPrintController();
            docToPrint.PrintController = printController;


            DialogResult result = PrintDialog1.ShowDialog();//調用PrintDialog的ShowDialog函數顯示打印對話框

            //If the result is OK then print the document.
            if (result == DialogResult.OK)
            {
                docToPrint.Print();//開始打印
            }

效果:直接調出設置打印機的窗口。

2、USB打印

思路:
1、先找到USB 打印機。SetupDiGetClassDevs、SetupDiEnumDeviceInfo、SetupDiGetDeviceRegistryProperty,找到“USB 支持設備”;
2、然后就是老路,獲得該USB打印機的路徑,SetupDiEnumDeviceInterfaces、SetupDiGetInterfaceDeviceDetail,獲得路徑。(SetupDiGetClassDevs不用了,因為1中已經獲得句柄集)
3、再根據路徑CreateFile,后面的內容網上一搜一大堆。

步驟:
1、CreateFile
2、SetupComm
3、SetCommTimeouts
4、GetCommState
5、SetCommState
6、PurgeComm
7、WriteFile


不知道是不是繞遠路了,呵呵。

參考資料:http://www.cnblogs.com/SunYu/archive/2010/04/29/1723977.html
參考代碼:http://www.codeproject.com/Articles/14500/Detecting-Hardware-Insertion-and-or-Removal

 

USB下直接用程序訪問不太方便,於是利用下面這個連接里的方法進行LPT並口轉換:

http://www.yt-pos.com/ask/question.php?id=263

然后就可以連接上打印機了。

參考其他網站:

http://zhidao.baidu.com/link?url=3hhQOUVhcJdaV9tNgxNnD_SqEUgA-cesSciNvhV42a4G6Pu8u5r17m1J3HmHTud_6ZAtyQquX9gHzVoIYgaLqa

http://bbs.csdn.net/topics/390085304?page=1#post-398581498

http://www.cnblogs.com/zbo/archive/2009/07/11/1521454.html

 

打印輸出並切紙操作:

第一點: 

const uint GENERIC_READ =0x80000000;
        const uint GENERIC_WRITE = 0x40000000;     
        const int OPEN_EXISTING = 3;
        private FileStream fs=null;
        IntPtr iHandle;
        [DllImport("kernel32.dll")]
        public static extern IntPtr CreateFileA(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   int i
        );

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

第二點:

 protected void imageButton1_Click(object sender, ImageClickEventArgs e)
        {
          
            ShopCart mycart = CartCtroller.GetCart();         
           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);
             
               //for (int i = 0; i < 5; i++)
               //{
               //    fs.WriteByte(0x0A); //走紙(忘記了可能是這個)
               //}
               //StreamReader sr = new StreamReader(fs);   //讀數據;  
               //StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Default);  //些數據;  
            
               if (mycart.Count == 0)
               {
                   Jscript.AjaxRunJs(this.Page, "alert('請先購買產品!')");
                   return;
               }   

               byte[] buf = new byte[500];
               char ESC = (char)(27);
               char GS = (char)(29);                                                                                                                 
               char LF = (char)(10);
           

     //讀出數據
               StringBuilder SEND = new StringBuilder();
               //SEND.Append(LF.ToString());    
               char c1 = (char)60;
               char c2 = (char)0;
               SEND.Append(GS.ToString() + "L" + c1.ToString() + c2.ToString());
               SEND.Append(ESC + "D" + Convert.ToChar(25) + Convert.ToChar(30) + Convert.ToChar(0));
               string dt = DateTime.Now.ToString();
               string mingcheng = "產品名稱:";
               string miaoshu = "產品描述:";
               string shuliang = "購買數量:";
               string zhi = "支";
               SEND.Append(LF.ToString());   
               SEND.Append("歡 迎 光 臨 - 上海金福");
               SEND.Append("\r\n");
               SEND.Append("-------------------------------");
               SEND.Append(LF.ToString());
               SEND.Append("購買時間:"+dt);
               SEND.Append("\r\n");
               SEND.Append("-------------------------------");
               SEND.Append(LF.ToString());
               SEND.Append(LF.ToString());
               for (int i = 0; i < mycart.Count; i++)
               {
                   modelArrayProduct temp = (modelArrayProduct)mycart[i];                
                   SEND.Append(mingcheng + temp.T_name);
                   SEND.Append("\r\n");
                   SEND.Append(miaoshu +   temp.T_miaoshu);
                   SEND.Append("\r\n");  
                   SEND.Append(shuliang + temp.T_shuliang.ToString() + zhi);
                   SEND.Append("\r\n");  
                   SEND.Append("-------------------------------");
                   SEND.Append(LF.ToString());                   
               }            

               //走紙
               SEND.Append(LF.ToString());
               SEND.Append(LF.ToString());             
               SEND.Append(LF.ToString());
               SEND.Append(LF.ToString());
               SEND.Append(GS + "V" + Convert.ToChar(1));
             
                //字符類型轉換
               Encoding utf8 = Encoding.GetEncoding(65001);
               Encoding gb2312 = Encoding.GetEncoding("gb2312");//Encoding.Default ,936
               byte[] tempa = utf8.GetBytes(SEND.ToString());

               buf = Encoding.Convert(utf8, gb2312, tempa);
                                       
              fs.Write(buf, 0, buf.Length);
                          fs.Close();                       
           }
                 CloseHandle(iHandle);     

        }

 

 

C#執行批處理命令

using System.Diagnostics ;

using System.IO;

 

private void btnRun_Click(object sender, EventArgs e)
        {
            txtResult.Text = "";
            processCommand("Ping.exe", this.txtAddress.Text);
            processCommand("Ping.exe", this.txtAddress.Text);
        }

 

        public void processCommand(string commandName, string argument)
        {
            ProcessStartInfo start = new ProcessStartInfo(commandName);//設置運行的命令行文件問ping.exe文件,這個文件系統會自己找到
            //如果是其它exe文件,則有可能需要指定詳細路徑,如運行winRar.exe
            start.WorkingDirectory = "d:\\360Downloads\\";
            start.Arguments = argument;//設置命令參數
            start.CreateNoWindow = true;//不顯示dos命令行窗口
            start.RedirectStandardOutput = true;//
            start.RedirectStandardInput = true;//
            start.UseShellExecute = false;//是否指定操作系統外殼進程啟動程序
            txtResult.AppendText(start.WorkingDirectory  + "\n");
            Process p = Process.Start(start);
            StreamReader reader = p.StandardOutput;//截取輸出流
            string line = reader.ReadLine();//每次讀取一行
            while (!reader.EndOfStream)
            {
                txtResult.AppendText(line + "\n");
                line = reader.ReadLine();
            }
            p.WaitForExit();//等待程序執行完退出進程
            p.Close();//關閉進程
            reader.Close();//關閉流
        }

以上均測試成功。

 

補充BEIYANG BTP-R580相關參數:

 

主要參數  
打印方式 熱敏打印 分辨率 203×203dpi
最高打印速度 230mm/s 打印寬度(mm) 80
基本參數  
打印方式 熱敏打印 分辨率 203×203dpi
最高打印速度 230mm/s    
高級參數  
打印寬度(mm) 80 打印方向 雙向打印
紙張種類 連續紙, 標記紙, 標簽紙 紙張厚度 0.06mm-0.1mm
字體 ASCII(12×24), 壓縮 ASCII(9×17), 國標宋體I, Ⅱ級(24×24), 國際字符 供紙方式 自動
接口類型 並行接口(IEEE 1284), RS-232, RS-485/422串行接口, USB接口, Ethernet接口, WLAN接口可選    
電源參數  
電源電壓(V) 220±10% 電源頻率(Hz) 50-60
外觀參數  
顏色 黑色 重量(Kg) 1.3
高度(mm) 147 寬度(mm) 147
長度(mm) 205    

測試打印機二:

ESPON TM TM58 票據打印機

 

愛普生 T58 基本參數
產品類型: 微型熱敏打印機
打印方式: 熱敏式
打印寬度: 32/16
緩沖區: 2KB
接口類型: RS-232,Bi-directionalparallel
愛普生 T58 打印性能
打印速度: 最大100mm/sec
打印分辨率: 360dpi
字符集: 95Alphanumeric,37 International,128×1Graphic
字體: 12×24(ANK)/24×24(Chinese)
愛普生 T58 介質規格
介質寬度: 57.5±0.5mm
介質厚度: 0.06-0.07mm
愛普生 T58 其它參數
平均無故障時間: 160000小時
產品尺寸: 116×204×137mm
產品重量: 1.3kg
系統平台: Windows2000/XP
電源電壓: DC24V(±7%),1.1A
其它特性: 字符尺寸:1.25×3.0mm/2.5×3.0mm
每英寸字符數:16.9cpi/10.2cpi
D.K.D.功能:2drives
外殼顏色:ECW
安全標准:CCC
電源:Exclusive AC adapter(included)
配件:AC adapter,I/F cable,User’s manual
安全標准:UL/CSA/T+V(E N60950)

另參考:

 http://blog.csdn.net/guoyong4321/article/details/7079333


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM