仿迅雷客户端的浏览器自定义协议的小程序

上家公司有这样的一个需求:

在网页中点一个链接,启动我们自己的一个客户端程序,并接受链接传递过来的参数,在客户端做相应的处理

1。自定义浏览器协议

     只要给注册表写进去一点信息就可以实现将自己定义的协议(如:mylink://)跟某客户端程序关联,google一下就可以了

2.只能让这个客户端运行一个实例

ExpandedBlockStart.gif
None.gif static void Main(string[] url)
ExpandedBlockStart.gif        {
InBlock.gif            Application.EnableVisualStyles();
InBlock.gif            Application.SetCompatibleTextRenderingDefault(false);
InBlock.gif
InBlock.gif            if (url != null && url.Length > 0)
ExpandedSubBlockStart.gif            {
InBlock.gif                if (CreateMutex())
ExpandedSubBlockStart.gif                {
InBlock.gif                    form = new MainForm(url[0]);
InBlock.gif                    Application.Run(form);
InBlock.gif                    ReleaseMutex(); 
ExpandedSubBlockEnd.gif                }

InBlock.gif                else
ExpandedSubBlockStart.gif                {
InBlock.gif                    int hd = FindWindow(null, "自定义浏览器协议");
InBlock.gif                    if (hd == 0)
ExpandedSubBlockStart.gif                    {
InBlock.gif                        throw new Exception("Could   not   find   Main   window!");
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    byte[] sarr = System.Text.Encoding.Default.GetBytes(url[0]);
InBlock.gif                    int len = sarr.Length;
InBlock.gif                    COPYDATASTRUCT cds;
InBlock.gif                    cds.dwData = (IntPtr)100;
InBlock.gif                    cds.lpData = url[0];
InBlock.gif                    cds.cbData = len + 1;
InBlock.gif                    SendMessage(hd, 0x004A, 0, ref cds);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            else
ExpandedSubBlockStart.gif            {
InBlock.gif                if (CreateMutex())
ExpandedSubBlockStart.gif                {
InBlock.gif                    form = new MainForm();
InBlock.gif                    Application.Run(form);
InBlock.gif                    ReleaseMutex();
ExpandedSubBlockEnd.gif                }

InBlock.gif                else
ExpandedSubBlockStart.gif                {
InBlock.gif                    MessageBox.Show("该程序只能运行一个实例!");
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }
ExpandedBlockStart.gif
        public static bool CreateMutex()
        {
            bool result = false;
            mutex = new Mutex(true, Assembly.GetEntryAssembly().FullName, out result);
            return result; 
        }

        public static void ReleaseMutex()
        {
            if (mutex != null)
            {
                mutex.Close();
            }
        }

3.消息传递   用windows的消息机制

ExpandedBlockStart.gif
ExpandedBlockStart.gif        /// <summary>
InBlock.gif        /// 该结构用来定义用户封装的数据
ExpandedBlockEnd.gif        /// </summary> 

None.gif        public struct COPYDATASTRUCT
ExpandedBlockStart.gif        {
InBlock.gif            public IntPtr dwData;
InBlock.gif            public int cbData;
InBlock.gif            [MarshalAs(UnmanagedType.LPStr)]
InBlock.gif            public string lpData;
ExpandedBlockEnd.gif        }

None.gif
None.gif       
ExpandedBlockStart.gif
ExpandedBlockStart.gif        /// <summary>
InBlock.gif        /// 消息处理
InBlock.gif        /// </summary>
ExpandedBlockEnd.gif        /// <param name="m">消息</param>

None.gif        protected override void DefWndProc(ref Message m)
ExpandedBlockStart.gif        {
InBlock.gif            switch (m.Msg)
ExpandedSubBlockStart.gif            {
InBlock.gif                case 0x004A:
InBlock.gif                    COPYDATASTRUCT mystr = new COPYDATASTRUCT();
InBlock.gif                    Type mytype = mystr.GetType();
InBlock.gif                    mystr = (COPYDATASTRUCT)m.GetLParam(mytype);
InBlock.gif                    WebBrowserNavigatingEventArgs e = new WebBrowserNavigatingEventArgs(new Uri(mystr.lpData.ToString()), "");
InBlock.gif                    OnNavigate(null, e);
InBlock.gif                    break;
InBlock.gif                default:
InBlock.gif                    base.DefWndProc(ref   m);
InBlock.gif                    break;
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

转载于:https://www.cnblogs.com/sunsoft/archive/2011/02/26/1965620.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值