zoukankan      html  css  js  c++  java
  • 仿迅雷客户端的浏览器自定义协议的小程序

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

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

    1。自定义浏览器协议

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

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


     static void Main(string[] url)
            
    {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(
    false);

                
    if (url != null && url.Length > 0)
                
    {
                    
    if (CreateMutex())
                    
    {
                        form 
    = new MainForm(url[0]);
                        Application.Run(form);
                        ReleaseMutex(); 
                    }

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

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

                }

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

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

                }

            }

            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的消息机制


            /// <summary>
            
    /// 该结构用来定义用户封装的数据
            
    /// </summary> 

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


           

            /// <summary>
            
    /// 消息处理
            
    /// </summary>
            
    /// <param name="m">消息</param>

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

            }
  • 相关阅读:
    bios设置启动方式问题
    springmvc 之 easyUI开发商城管理系统
    maven 整合 ssm 异常分析
    maven 之nexus仓库管理_私服配置
    maven 之 web.xml 头设置错误问题
    maven之jre默认配置
    springMvc异常之 For input string: "show"
    mysql参照完整性 策略设置之 on update 和 on delete
    ssm之mapper异常 Result Maps collection already contains value for com.ssj.mapper.UserMapper 和 Type interface com.ssj.mapper.UserMapper is already known to the MapperRegistry.
    HDU-2054 A==B?
  • 原文地址:https://www.cnblogs.com/sunsoft/p/1965620.html
Copyright © 2011-2022 走看看