Web page Launch WinForm program

Source: Internet
Author: User
Tags parse string

The requirements for this article are:

A. Launch the WinForm program through A Web page;

B. Pass the parameters of the page to the WinForm program;

The C.winform program has started and is running when the WinForm program cannot be restarted from the Web page.

Only when the parameters of the incoming WinForm program change, the data displayed on the WinForm is updated accordingly.

  The specific implementation is as follows:

1. Page HTML code

<! DOCTYPE HTML Public"-//W3C//DTD XHTML 1.0 transitional//en" "HTTP://WWW.W3.ORG/TR/XHTML1/DTD/XHTML1-TRANSITIONAL.DTD">"http://www.w3.org/1999/xhtml"> "Content-type"Content="text/html; charset=gb2312"/> "oraans://parameters for incoming EXE files">Open 1</a> <br> <a href="oraans://parameters for incoming EXE files">Open 2</a> <br> <a href="oraans://parameters for incoming EXE files">Open 3</a> <br> <a href="oraans://parameters for incoming EXE files">Open 4</a> <br> </div> </body>

2, the page starts the program is through the registration form to start

Xxx.reg Operation Registry File code

Windows Registry Editor Version5.00[Hkey_classes_root\oraans]"URL Protocol"="E:\\debug\\xxx.exe" @="Oralanswerprotocol"[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\""

3, WinForm program processing the parameters passed in the page (based on C #)

1), Program.cs file code

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Windows.Forms;usingSystem.Text.RegularExpressions;usingMicrosoft.Win32;usingSystem.Threading;namespaceoraans{Static classProgram { Public StaticEventWaitHandle programstarted;//event wait handle        /// <summary>        ///The main entry point for the application. /// </summary>[STAThread]Static voidMain (string[] args) {            if(args. Length >0)//A parameter is passed in when the page starts, or it is started directly            {                stringSparametervalue = Regex.match (args[0],"^[0-9a-za-z]+://(. +) $"). groups[1].                Value; Filterinvalidcharacter (refsparametervalue); Registry.setvalue (@"hkey_current_user\software\oraansparameters","", Sparametervalue);//writes processed incoming parameters to the registry                                BOOLbisornotcreatenew; Programstarted=NewEventWaitHandle (false, Eventresetmode.autoreset,"oraansclient", outbisornotcreatenew); if(!bisornotcreatenew) {                    //WinForm Program is started when it is executedProgramstarted.set (); return;            }} application.enablevisualstyles (); Application.setcompatibletextrenderingdefault (false); Application.Run (NewOralanswermain ()); }        /// <summary>        ///illegal characters that handle the parameters returned by the page/// </summary>        /// <param name= "Sparametervalue" ></param>        Static voidFilterinvalidcharacter (ref stringsparametervalue) {            intNstrlength =sparametervalue.length; if(Nstrlength >0)            {                if('/'= = Sparametervalue[nstrlength-1])                {                    if(1==nstrlength) {Sparametervalue=""; }                    Else{Sparametervalue= Sparametervalue.substring (0, Nstrlength-1); }                }            }        }    }}

2) code for WinForm code file

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingMicrosoft.Win32;usingSystem.Threading;namespaceoraans{ Public Partial classOraansmain:form {/// <summary>        ///constructor Function/// </summary>         PublicOraansmain () {InitializeComponent (); Try            {                //gets the parameters passed by the page from the registry and resolves                ObjectOBJ = Registry.getvalue (@"hkey_current_user\software\oraansparameters","",string.                Empty); if(OBJ! =NULL)                {                    stringsrestring = OBJ as string; //TODO: Parse string arguments passed in from page                }                if(program.programstarted! =NULL) {threadpool.registerwaitforsingleobject (program.programstarted, onprogramstarted,  This, -1,false);//Registering thread Delegates                }            }            Catch(Exception e) {e.tostring (); }        }
Public Delegate voidMyinvoke ();//declaring a delegate//callbacks performed by the ThreadPool.RegisterWaitForSingleObject method voidOnprogramstarted (ObjectStateBOOLTimeout) { Try { //processing of asynchronous calls through delegates, avoiding different threads manipulating the UI threadMyinvoke mi =NewMyinvoke (uiinitial); This. BeginInvoke (MI,NewObject[] {/*the input Parameter object for the Uiinitial method call*/ }); } Catch(Exception e) {e.tostring (); } } /// <summary> ///UI Display Initialization/// </summary> voiduiinitial () {//processing of Todo:ui initialization }
Private voidOraansmain_load (Objectsender, EventArgs e) { } }}

......

Web page Launch WinForm program

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.