首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PosPrinter只用于.net打印一次并挂起

PosPrinter只用于.net打印一次并挂起
EN

Stack Overflow用户
提问于 2017-03-03 07:49:55
回答 1查看 794关注 0票数 0

我正在构建winforms .net应用程序,我在网络上有一个below打印机,使用下面的代码:在表单加载打印机初始化:

代码语言:javascript
复制
    explorer = new PosExplorer(this);
        DeviceInfo receiptPrinterDevice = explorer.GetDevice("PosPrinter", Properties.Settings.Default.KitchenPrinter); //May need to change this if you don't use a logicial name or use a different one.
        kitchenPrinter = (PosPrinter)explorer.CreateInstance(receiptPrinterDevice);
       ConnectToPrinter();


    private void ConnectToPrinter()
    {  
        kitchenPrinter.Open();
        kitchenPrinter.Claim(10000);
        kitchenPrinter.DeviceEnabled = true;
     }

对打印按钮的函数调用:

代码语言:javascript
复制
 private void PrintReceipt()
    {

         try
            {  kitchenPrinter.PrintNormal(PrinterStation.Receipt, "test");
              }
            finally
            {

            }
    }

当我想要切换到其他形式时,我调用断开函数。

代码语言:javascript
复制
        DisconnectFromPrinter(kitchenPrinter);
        Reporting frm = new Reporting(curuser);
        frm.Show();
        this.Hide();


  private void DisconnectFromPrinter(PosPrinter kitchenPrinter)
    {

        try
        {
           kitchenPrinter.Release();
            kitchenPrinter.Close();
        }
       catch { }
    }

它一次打印成功,下一次按下打印时抛出和异常。

方法ClaimDevice抛出一个异常。试图对设备执行非法或不受支持的操作,或者使用无效的参数值。

有什么建议吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-03 17:30:03

因为Release命令并不有效,而且可能声明命令每次加载表单时都会抛出一个错误,因为它是在前面声明的。

因此,我创建了一个名为"createPOS“的单独类

代码语言:javascript
复制
         class createPOS
{
 public  static PosExplorer explorer;
  public  static PosPrinter kitchenPrinter;
  public static void createPos()
  {
      explorer = new PosExplorer();
      DeviceInfo receiptPrinterDevice = explorer.GetDevice("PosPrinter", Properties.Settings.Default.KitchenPrinter); //May need to change this if you don't use a logicial name or use a different one.
      kitchenPrinter = (PosPrinter)explorer.CreateInstance(receiptPrinterDevice);
      kitchenPrinter.Open();
      kitchenPrinter.Claim(10000);
      kitchenPrinter.DeviceEnabled = true;
  }
      public static void Print(string text){
          if (kitchenPrinter.Claimed)
              PrintTextLine(kitchenPrinter, text);  // kitchenPrinter.PrintNormal(PrinterStation.Receipt, text ); //Print text, then a new line character.
       }
      private static void PrintTextLine(PosPrinter printer, string text)
      {
          if (text.Length < printer.RecLineChars)
              printer.PrintNormal(PrinterStation.Receipt, text + Environment.NewLine); //Print text, then a new line character.
          else if (text.Length > printer.RecLineChars)
              printer.PrintNormal(PrinterStation.Receipt, TruncateAt(text, printer.RecLineChars)); //Print exactly as many characters as the printer allows, truncating the rest, no new line character (printer will probably auto-feed for us)
          else if (text.Length == printer.RecLineChars)
              printer.PrintNormal(PrinterStation.Receipt, text + Environment.NewLine); //Print text, no new line character, printer will probably auto-feed for us.
      }
      private static string TruncateAt(string text, int maxWidth)
      {
          string retVal = text;
          if (text.Length > maxWidth)
              retVal = text.Substring(0, maxWidth);

          return retVal;
      }
  }

在登录表单上,只有在我初始化打印机之后才能访问它。

代码语言:javascript
复制
     createPOS.createPos();

在MainForm上,我称之为打印方法:

代码语言:javascript
复制
        createPOS.Print("This allows me to Print Several times");

这样的话,我可以打印几次,甚至导航到其他表单,然后再回来,效果很好。

谢谢你们。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42573417

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档