从web页面启动winform程序的实现方法
2023-09-11 14:16:44 时间
本文实现的需求是:
A.通过web页面启动winform程序;
B.将页面的参数传递给winform程序;
C.winform程序已经启动并正在运行时,从web页面不能重新启动winform程序,只是当传入winform程序的参数更改时,winform上显示的数据作出相应的更新。
具体实现如下:
1、页面html代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
</head>
<body>
<div>
<a href="OraAns://传入exe的参数1">
打开1
</a>
<br>
<a href="OraAns://传入exe的参数2">
打开2
</a>
<br>
<a href="OraAns://传入exe的参数3">
打开3
</a>
<br>
<a href="OraAns://传入exe的参数4">
打开4
</a>
<br>
</div>
</body>
</html>
2、页面启动的程序是通过注册表来启动的
a、通过注册表脚本文件(xxx.reg)操作注册表
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\OraAns]
"URL Protocol"="E:\\Debug\\xxx.exe"
@="OraAnsProtocol"
[HKEY_CLASSES_ROOT\OraAns\DefaultIcon]
@="E:\\Debug\\xxx.exe,1"
[HKEY_CLASSES_ROOT\OraAns\shell]
[HKEY_CLASSES_ROOT\OraAns\shell\open]
[HKEY_CLASSES_ROOT\OraAns\shell\open\command]
@="\"E:\\Debug\\xxx.exe\" \"%1\""
b、通过批处理文件(xxx.bat)操作注册表
reg add HKEY_CLASSES_ROOT\OraAns /ve /t reg_sz /d OraAnsProtocol
reg add HKEY_CLASSES_ROOT\OraAns /v "URL Protocol" /t reg_sz /d “%~dp0OraAns.exe”
reg add HKEY_CLASSES_ROOT\OraAns\DefaultIcon /ve /t reg_sz /d “%~dp0OraAns.exe”,1
reg add HKEY_CLASSES_ROOT\OraAns\shell\open\command /ve /t reg_sz /d "\"%~dp0OraAns.exe\" \"%%1\""
3、winform程序处理页面传入的参数(基于C#)
1)、Program.cs文件代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using Microsoft.Win32;
using System.Threading;
namespace OraAns
{
static class Program
{
public static EventWaitHandle ProgramStarted; //事件等待句柄
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
if (args.Length > 0) //从页面启动时有参数传入,否则直接启动
{
string sParameterValue = Regex.Match(args[0], "^[0-9a-zA-Z]+://(.+)$").Groups[1].Value;
FilterInvalidCharacter(ref sParameterValue);
Registry.SetValue(@"HKEY_CURRENT_USER\Software\OraAnsParameters", "", sParameterValue); //将经过处理的传入参数写入注册表
bool bIsOrNotCreateNew;
ProgramStarted = new EventWaitHandle(false, EventResetMode.AutoReset, "OraAnsClient", out bIsOrNotCreateNew);
if (!bIsOrNotCreateNew)
{
//winform程序已经启动时执行
ProgramStarted.Set();
return;
}
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new OralAnswerMain());
}
/// <summary>
/// 处理页面传回的参数的非法字符
/// </summary>
/// <param name="sParameterValue"></param>
static void FilterInvalidCharacter(ref string sParameterValue)
{
int nStrLength = sParameterValue.Length;
if (nStrLength > 0)
{
if ('/' == sParameterValue[nStrLength - 1])
{
if (1 == nStrLength)
{
sParameterValue = "";
}
else
{
sParameterValue = sParameterValue.Substring(0, nStrLength - 1);
}
}
}
}
}
}
2)、winform代码文件的代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;using Microsoft.Win32;
using System.Threading;
namespace OraAns
{
public partial class OraAnsMain : Form
{
/// <summary>
/// 构造函数
/// </summary>
public OraAnsMain()
{
InitializeComponent();
try
{
//从注册表中获取页面传递过来的参数并解析
object Obj = Registry.GetValue(@"HKEY_CURRENT_USER\Software\OraAnsParameters", "", string.Empty);
if (Obj != null)
{
string sReString = Obj as string;
//TODO:解析从页面传入的字符串参数
}
if (Program.ProgramStarted != null)
{
ThreadPool.RegisterWaitForSingleObject(Program.ProgramStarted, OnProgramStarted, this, -1, false); //注册线程委托
}
}
catch (Exception e)
{
e.ToString();
}
}
public delegate void MyInvoke(); //声明委托
//ThreadPool.RegisterWaitForSingleObject方法执行的回调
void OnProgramStarted(object state, bool timeout)
{
try
{
//通过委托进行异步调用的处理,避免不同线程操作UI线程
MyInvoke mi = new MyInvoke(UIinitial);
this.BeginInvoke(mi, new Object[] { /*UIinitial方法调用的输入参数对象*/ });
}
catch (Exception e)
{
e.ToString();
}
}
/// <summary>
/// UI显示初始化
/// </summary>
void UIinitial()
{
//TODO:UI初始化的处理
}
private void OraAnsMain_Load(object sender, EventArgs e)
{
}
}
}
相关文章
- winform程序公布后,client下载报错“您的 Web 浏览器设置不同意执行未签名的应用程序”
- Expression构建DataTable to Entity 映射委托 sqlserver 数据库里面金额类型为什么不建议用float,实例告诉你为什么不能。 sql server 多行数据合并成一列 C# 字符串大写转小写,小写转大写,数字保留,其他除外 从0开始用U盘制作启动盘装Windows10系统(联想R720笔记本)并永久激活方法 纯CSS打造淘宝导航菜单栏 C# Winform
- 一个winform带你玩转rabbitMQ
- winform中tabControl的标题隐藏
- C#【必备技能篇】Winform中,如何去掉Button按钮的边框线(或更改边框颜色)
- DevExpress添加Winform窗体到DockPanel
- winform程序最大化时,页面显示不全的问题
- C# Winform实现捕获窗体最小化、最大化、关闭按钮事件的方法,可通过重写WndProc来实现
- Winform让扫描枪听话,防止在有焦点的地方就扫码输入的尴尬
- Winform appconfig修改后的更新问题
- Winform框架之字典数据管理
- Winform 多组合老板键Alt_Ctrl_Shift
- Winform应用程序实现通用遮罩层
- WINFORM如何禁止调整窗体大小
- C# winForm 多条件模糊查询实现(主要讲思路,请忽略与数据库(sqlite)相关的代码)
- c# winform捕获全局异常,并记录日志
- C# winform实现右下角弹出窗口结果的方法
- C# winform窗口打开特效及窗口位置居中
- windows程序消息机制(Winform界面更新有关)
- C# Winform 两个窗体通讯 一个窗体调用另一个窗体的方法