在写这个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 onclick="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)
{
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; 在安装目录下的第一个文件夹中。
下面直接贴实现代码:(需要注意在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);
//ItemmstMod clsItemmst = new ItemmstMod() { PN = PN, Descr = Descr, Enable = int.Parse(Enable), ProdName = ProdName, Spec = Spec, SupplierPn = SupplierPn, Unit = Unit, CreateDate = CreateDate };
//GlobalMod.RunTime.Elapsed += new System.Timers.ElapsedEventHandler((s, el) => PrintJob(s, el, clsItemmst));
//PrintJob(null,null,clsItemmst);
//if (!GlobalMod._blSwicth)
//{
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);
}
//GlobalMod.RunTime.Start();
//button1.Text = "暂停打料号";
//}
//else
//{
// GlobalMod._blSwicth = false;
// GlobalMod.RunTime.Enabled = false;
// button1.Text = "开始打料号";
//}
}
/// <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;
}