從web頁面啟動winform程序的實現方法


  本文實現的需求是:

    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) { } } }

......


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM