C#票据打印类

using System;

using System.Collections.Generic;

using System.Text;

using System.IO;

using System.Windows.Forms;

using Microsoft.Win32.SafeHandles;
using System.Runtime.InteropServices;

using System.ComponentModel;



namespace Comm

{

   
publicclass TicketPrinter

   
{

       
publicconstshort FILE_ATTRIBUTE_NORMAL =0x80;

       
publicconstshort INVALID_HANDLE_VALUE =-1;

       
publicconstuint GENERIC_READ =0x80000000;

       
publicconstuint GENERIC_WRITE =0x40000000;

       
publicconstuint CREATE_NEW =1;

       
publicconstuint CREATE_ALWAYS =2;

       
publicconstuint OPEN_EXISTING =3;



        [DllImport(
"inpout32.dll", EntryPoint ="Out32")]

       
privatestaticexternvoid Output(int adress, int value);



        [DllImport(
"inpout32.dll", EntryPoint ="Inp32")]

       
privatestaticexternint Input(int adress);  





       
private SafeFileHandle TicketPrinterHandle =null;

        [DllImport(
"kernel32.dll", SetLastError =true)]

       
staticextern SafeFileHandle CreateFile(

           
string lpFileName, 

           
uint dwDesiredAccess,

           
uint dwShareMode, 

            IntPtr lpSecurityAttributes, 

           
uint dwCreationDisposition,

           
uint dwFlagsAndAttributes, 

            IntPtr hTemplateFile);



        [DllImport(
"kernel32.dll", SetLastError =true)]

       
privatestaticexternbool WriteFile(

         SafeFileHandle hFile,           
// handle to file

         char[] lpBuffer,                       // data buffer

         int nNumberOfBytesToWrite,   // number of bytes to write

         outint lpNumberOfBytesWritten,  // number of bytes written

         IntPtr lpOverlapped          // overlapped buffer

         );



        [DllImport(
"kernel32.dll", SetLastError =true)]

       
privatestaticexternbool CloseHandle(

         SafeFileHandle hObject  
// handle to object

         );





       
privatestring _PPort ="";

       
publicstring Status ="正在检测...";



       
public TicketPrinter()

       
{



        }




       
publicvoid Create( string LPTName )

       
{

            _PPort
="";

            TicketPrinterHandle
= CreateFile(

                 LPTName, GENERIC_WRITE,
0, IntPtr.Zero,

                  OPEN_EXISTING,
0, IntPtr.Zero);

           
if (TicketPrinterHandle.IsInvalid)

           
{

                MessageBox.Show(
"票据打印机口不能打开!");

               
return;

            }


            _PPort
= LPTName;

        }




       
publicvoid Close()

       
{

           
if (TicketPrinterHandle ==null)

               
return;

           
if (TicketPrinterHandle.IsInvalid)

               
return;

            CloseHandle(TicketPrinterHandle);

        }




       
publicvoid Print(string str)

       
{

            Print(str,
"left");

        }




       
publicvoid Print(string str, string align)

       
{

           
if (TicketPrinterHandle ==null)

               
return;

           
if (TicketPrinterHandle.IsInvalid)

               
return;

           
int PadCount =30;

            PadCount
-= StrLen( str );

           
if (align =="center")

                str
= str.PadLeft(PadCount /2+ str.Length, '');

           
elseif (align =="right")

                str
= str.PadLeft(PadCount, '');



           
//////////////////////////////////////////////////////////

            //  7     6      5      4       3       2       1       0

           
// Busy  nAck Paper-Out Select nError   x       x       x

            //////////////////////////////////////////////////////////

            //check Parallel port Status registers 

            int RetryCount =10;

            Retry:

           
int sta =0;

           
if (_PPort =="LPT1")

                sta
= Input(0x379);

           
elseif (_PPort =="LPT2")

                sta
= Input(0x279);



           
if( sta ==0x7F )

           
{

                Status
="未连接";

               
return;

            }


           
if ((sta &0x10) ==0)

           
{

                Status
="未上电";

               
return;

            }


           
if ((sta &0x80) ==0)      //Busy

            {

                Status
="打印机忙";

                RetryCount
--;

               
if (RetryCount <=0)

                   
return;

               
goto Retry;

            }


           
if ((sta &0x20) !=0)      //Paper-Out

            {

                Status
="打印机缺纸";

               
return;

            }

posted @ 2012-06-27 23:37  小软狐  阅读(4299)  评论(2编辑  收藏  举报