一、创建Winform应用程序
新建一个Windows Forms项目,在默认的Form1里添加一个Label控件(这里是为了显示传递过来的参数用的,你也可以不这么做)。
在窗体的Load事件里写上如下代码:
this.label1.Text = strArgs;//是Form1的声明为:Public的String类型变量
为了让我们的程序支持命令行参数(winform程序默认没有命令行参数),我们再到Programs.cs里对Main()函数进行改造。
改造后的代码如下:
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1 frm = new Form1();
if (args.Length >0)
{
frm.strArgs = args[0];
Application.Run(frm);
}
else
{
Application.Run(new Form1());
}
}
将项目编译。
二、通过注册表建立协议
将以下内容保存为.bat文件,双击执行以添加helloworld协议到注册表。
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\HelloWorld]
@="HelloWorld Protocol"
"URL Protocol"=""
[HKEY_CLASSES_ROOT\HelloWorld\DefaultIcon]
@="D:\\My Documents\\Visual Studio 2005\\Projects\\HelloWorld\\release\\HelloWorld.exe,1"
[HKEY_CLASSES_ROOT\HelloWorld\shell]
@=""
[HKEY_CLASSES_ROOT\HelloWorld\shell\open]
@=""
[HKEY_CLASSES_ROOT\HelloWorld\shell\open\command]
@="\"D:\\My Documents\\Visual Studio 2005\\Projects\\HelloWorld\\release\\HelloWorld.exe\" \"%1\""
保存完成之后,在地址栏中输入:helloworld://hello world/,可以发现,上面创建的Winform程序被调起,并接收到协议中传过来的"hello world"字符串。