URL Protocol应用

来源:互联网 发布:武汉java 编辑:程序博客网 时间:2024/06/05 07:59

最近见一资源管理系统使用webwin32的窗口进行通信感觉挺有意思,偶尔在购物网站看到了QQ消息的弹窗效果

 

类似这样点击后弹出QQ消息窗口

然后google  有答案,是使用了win的一个协议http://msdn.microsoft.com/en-us/library/aa767914(VS.85).aspx

这里比较详细

自己动手写了个demoMFC不是很灵光先用C#winfrom了)

 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using Microsoft.Win32; //引用Microsoft.Win32命名空间详细说明MSDN

 

/*

 * URL协议注册详细说明请见

 * http://msdn.microsoft.com/en-us/library/aa767914(VS.85).aspx

 *QQ664316183

*/

namespace URLProtocol

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        private void button2_Click(object sender, EventArgs e)

        {

            openFileDialog1.ShowDialog();

            textBox2.Text = openFileDialog1.FileName;

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

            if (textBox1.Text != "" && textBox2.Text != "")

            {

                try

                {

                    RegistryKey rgsk = Registry.ClassesRoot;//打开HKEY_CLASS_ROOT

                    if (rgsk.OpenSubKey(textBox1.Text) == null) //验证此键值是否存在

                    {

                        RegistryKey rgskroot = rgsk.CreateSubKey(textBox1.Text);

                        rgskroot.SetValue("", textBox1.Text + " Protocol");

                        rgskroot.SetValue("URL Protocol", textBox2.Text);

                        rgskroot.CreateSubKey("DefaultIcon").SetValue("", textBox2.Text + ",1");

                        rgskroot.CreateSubKey("shell").CreateSubKey("open").CreateSubKey("command").SetValue("", "/"" + textBox2.Text + "/" /"%1/"");

                        rgskroot.Close();

                        MessageBox.Show("协议创建成功!");

                    }

                    else MessageBox.Show("该协议已经存在!");

                }

                catch(Exception)

                {

                    MessageBox.Show("注册表读取异常!");

                }

            }

            else MessageBox.Show("协议名与处理程序不能为空!");

        }

 

        private void button3_Click(object sender, EventArgs e)

        {

            if (textBox3.Text != "")

            {

                try

                {

                    RegistryKey rgsk = Registry.ClassesRoot;

                    if (rgsk.OpenSubKey(textBox3.Text) != null) //验证此键值是否存在

                    {

                        rgsk.DeleteSubKeyTree(textBox3.Text);

                        MessageBox.Show("协议卸载成功!");

                    }

                    else MessageBox.Show("协议不存在!");

                    rgsk.Close();

                }

                catch(Exception)

                {

                    MessageBox.Show("注册表读取异常!");

                }

            }

            else MessageBox.Show("卸载的协议名不能为空!");

        }

    }

}

注册协议

 

测试代码

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>

<head>

</head>

<body>

    <div>

        <a href="helloworld://hello world">

            <img border="0" src='http://is.qq.com/webpresence/images/status/01_online.gif' alt="点击这里给我发消息">

        </a>

    </div>

</body>

</html>

 

卸载协议

 

原创粉丝点击