js与C# winform交互——BarTender打印

在写这个winform程序之前,我还有点小疑惑,winform怎么跟web交互?可能是因为我太久没接触winform了,废话不多说直接进入主题。

js与C#交互很简单,在网上搜一下一大堆,不过为了方便还是在这里说一下:

//web端

<html>
<head>
    <meta http-equiv="Content-Language" content="zh-cn">
    <script language="javascript" type="text/javascript">

<!-- 提供给C#程序调用的方法 -->
function GetWinForm(message)
{
    alert(message);
}
</script>
</head>
<body>
    <!-- 调用C#方法 -->
    <button οnclick="window.external.MyMessageBox('javascript访问C#代码')">
        javascript访问C#代码
    </button>
</body>
</html>

// C# winform端 ,页面使用webBrowser控件 (此处代码来自:https://www.jb51.net/article/66433.htm,略有改动)

// 只需加入[System.Runtime.InteropServices.ComVisible(true)]和  this.webBrowser1.ObjectForScripting = this; 即可被web端中js访问到。

[System.Runtime.InteropServices.ComVisible( true )]
   public partial class Form1 : Form
   {
     
 
     public Form1()
     {
       InitializeComponent();
       this .webBrowser1.ObjectForScripting = this ;
     }
 
     public void MyMessageBox ( string message)
     {
       MessageBox.Show(message);
     }
     private void button1_Click( object sender, EventArgs e)
     {
//获取web端页面元素对象(可使用id或name)
  HtmlElement searchWords = webBrowser1.Document.All["name"];
          //获取web端页面元素对象对应属性值
          var value = searchWords.GetAttribute("value");
     
//调用html页面的JS函数来传参数和运行winForm下的函数
       webBrowser1.Document.InvokeScript( "GetWinForm", "ss" );
    }
   }


接下来说一下使用BarTender这个软件的遇到的问题:

WinForm集成BarTender报错:
  检索 COM 类工厂中 CLSID 为 {8786AEA4-17EC-11D1-8AD8-006097D76312} 的组件失败,
原因是出现以下错误: 80070005 拒绝访问。(异常来自 HRESULT:0x80070005 (E_ACCESSDENIED))。


解决方案如下:

     IIS应用程序池中-->高级设置-->标识-->选择NetworkService用户
     同时在运行中输入dcomcnfg,组件服务->计算机->我的电脑->DCOM配置下的名字前缀为BarTender的服务—>属性->安全中的设置默认值添加BarTender用户及NetworkService用户并勾选全部权限 (添加->高级->立即查找(能直接查找到所有用户及组))
可参考:https://blog.csdn.net/windowsliusheng/article/details/52057358

(仅限x64位BarTender,x32位自动创建好了iis及自动配值了权限,应该是在安装时与是否创建了用户有关)

winform引用BarTender的dll,using Seagull.BarTender.Print; 在安装目录下SDK文件中的第一个文件夹中。


下面直接贴实现代码:(需要注意在web传对象时,需要json格式化,传集合时需要先json格式对象在push,然后传输是拼接上"["+obj+"]",但是运行时会报错,JSON未定义,因为winform是IE8的内核,不支持这个JSON对象,需要在web端直接引用json2.js文件)

 public class GlobalMod
    {
        public static bool _blSwicth = false;
        public static Engine btEngine;
        public static LabelFormatDocument btFormat;
        public static System.Timers.Timer RunTime = new System.Timers.Timer(500);
        public static string _Path = ConfigurationManager.AppSettings["Path"].ToString();

    }

方法体:

        public void ClickPrint(string obj)
        {
            JavaScriptSerializer js = new JavaScriptSerializer();
            List<ItemmstMod> itemmsts = js.Deserialize<List<ItemmstMod>>(obj);   
            if (GlobalMod.btFormat != null && GlobalMod.btFormat.Status == LabelFormat.LabelStatus.Loaded)
            {
                GlobalMod.btFormat.Close(SaveOptions.DoNotSaveChanges);//不保存模板更改
            }

           //由于在调用开起打印时会有一段延时,所以此段代码块移至页面加载事件中完成
            //GlobalMod.btEngine = new Engine(true);
            //GlobalMod.btEngine.Start();

            GlobalMod.btFormat = GlobalMod.btEngine.Documents.Open(GlobalMod._Path);

            GlobalMod._blSwicth = true;
            foreach (var item in itemmsts)
            {
                PrintJob(null, null, item);
            }  

        }


        /// <summary>
        /// 打印Job
        /// </summary>
        private void PrintJob(object sender, System.Timers.ElapsedEventArgs e, ItemmstMod clsItemmst)
        {
            Messages messages = Print(clsItemmst);
            if (!messages.HasError)
            {
                //修改到源数据
                //GetAPIDataSourceBLL.UpdateLabelPrinted(clsLabelJob.JobID);
            }

        }

        public Messages Print(ItemmstMod clsItemmst)
        {
            Messages messages = null;

            PrintDocument prtdoc = new PrintDocument();
            if (GlobalMod.btEngine == null)
            {
                GlobalMod.btEngine = new Engine(true);
                GlobalMod.btEngine.Start();
            }

            if (GlobalMod.btFormat == null)
            {
                GlobalMod.btFormat = GlobalMod.btEngine.Documents.Open(GlobalMod._Path);
            }

            GlobalMod.btFormat.PrintSetup.PrinterName = prtdoc.PrinterSettings.PrinterName;
            GlobalMod.btFormat.PrintSetup.IdenticalCopiesOfLabel = 1;
            GlobalMod.btFormat.SubStrings["PN"].Value = clsItemmst.PN;
            GlobalMod.btFormat.SubStrings["SupplierPn"].Value = clsItemmst.SupplierPn;
            GlobalMod.btFormat.SubStrings["Spec"].Value = clsItemmst.Spec;
            GlobalMod.btFormat.SubStrings["ProdName"].Value = clsItemmst.ProdName;

            Result nResult = GlobalMod.btFormat.Print("标签打印软件", 10000, out messages);
            GlobalMod.btFormat.PrintSetup.Cache.FlushInterval = CacheFlushInterval.PerSession;


            return messages;

        }

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值