overrid events of Webbrowser ActiveX Control in .Net

      在.net中使用ie WebBrowser控件时,需要跟据自已的需求来定制组件的事件,风格等。其实在.net里控制ie组件的操作很简单,并且可以使用MSHTML提供的所有功能。您可以截获用户的所有操作事件,可以取得用户操作的对像以及对像的属性。下面是我的保留代码,以备后用。

     #region  WebBrowser所需的事件触发定义
    
///  
    
///  Generic HTML DOM Event method handler.
    
///  
     public   delegate   void  DHTMLEvent(IHTMLEventObj e);
    
///  
    
///  Generic Event handler for HTML DOM objects.
    
///  Handles a basic event object which receives an IHTMLEventObj which
    
///  applies to all document events raised.
    
///  
     public   class  DHTMLEventHandler
    {
        
public  DHTMLEvent Handler;
        HTMLDocument Document;
        
public  DHTMLEventHandler(HTMLDocument doc)
        {
            
this .Document = doc;
        }
        [DispId(
0 )]
        
public   void  Call()
        {
            Handler(Document.parentWindow.@event);
        }
    }
    
#endregion

         private   void  axWebBrowserADS_DocumentComplete( object  sender, AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent e)
        {
            HTMLDocument doc 
=   this .axWebBrowserADS.Document  as  HTMLDocument ;
            
// 绑定事件
            DHTMLEventHandler onclickHandler  =   new  DHTMLEventHandler( doc ); 
            onclickHandler.Handler 
+=   new  DHTMLEvent( this .myads_onclick);
            doc.onclick 
=  onclickHandler;
        
            DHTMLEventHandler contextMenuHandler 
=   new  DHTMLEventHandler( doc ); 
            contextMenuHandler.Handler 
+=   new  DHTMLEvent( this .myads_oncontextmenu);
            doc.oncontextmenu 
=  contextMenuHandler;

            DHTMLEventHandler onkeydownHandler 
=   new  DHTMLEventHandler( doc ); 
            onkeydownHandler.Handler 
+=   new  DHTMLEvent( this .myads_onkeydown);
            doc.onkeydown 
=  onkeydownHandler;
            
            doc.body.style.border
= " none " ;
            doc.body.style.overflow
= " hidden " ;
        }

        
private   void  myads_oncontextmenu(IHTMLEventObj pEvtObj)
        {
            pEvtObj.returnValue
= false ;
        }

        
private   void  myads_onclick(IHTMLEventObj pEvtObj)
        {
            
string  a = pEvtObj.srcElement.GetType().ToString();
            
if (a == " mshtml.HTMLAnchorElementClass " )
            {
                mshtml.HTMLAnchorElementClass link
= (mshtml.HTMLAnchorElementClass)pEvtObj.srcElement;
                
if (link.href.Substring( 0 , 5 ).ToUpper() == " JOIN: " )
                {
                    clsVariable.FM_SERVICESLIST.doJOINcmd(link.href.Substring(
5 ));
                    pEvtObj.returnValue
= false ;
                }
                
else   if (link.id.Substring( 0 , 1 ) == " # " )
                {
                    clsVariable.FM_SERVICESLIST.doJOINcmd(link.id);
                    pEvtObj.returnValue
= false ;
                }
                
else
                {
                    link.target
= " _blank " ;
                }
            }
            
else   if (a == " mshtml.HTMLImgClass " )
            {
                mshtml.HTMLImgClass img
= (mshtml.HTMLImgClass)pEvtObj.srcElement;
                
if (img.id != null   &&  img.id.Substring( 0 , 1 ) == " # " )
                {
                    clsVariable.FM_SERVICESLIST.doJOINcmd(img.id);
                    pEvtObj.returnValue
= false ;
                }
                
else
                {
                    mshtml.HTMLAnchorElementClass link
= (mshtml.HTMLAnchorElementClass)pEvtObj.srcElement;
                    
if (link != null )
                        link.target
= " _blank " ;
                }
            }
            
else   if (a == " mshtml.HTMLAreaElementClass " )
            {
                mshtml.HTMLAreaElementClass area
= (mshtml.HTMLAreaElementClass)pEvtObj.srcElement;
                
if (area.href.Substring( 0 , 5 ).ToUpper() == " JOIN: " )
                {
                    clsVariable.FM_SERVICESLIST.doJOINcmd(area.href.Substring(
5 ));
                    pEvtObj.returnValue
= false ;
                }
                
else   if (area.id != null   &&  area.id.Substring( 0 , 1 ) == " # " )
                {
                    clsVariable.FM_SERVICESLIST.doJOINcmd(area.id);
                    pEvtObj.returnValue
= false ;
                }
                
else
                {
                    area.target
= " _blank " ;
                }
            }
        }

        
private   void  myads_onkeydown(IHTMLEventObj pEvtObj)
        {
            
if (pEvtObj.keyCode == 8 )     // backspace
            {
                pEvtObj.keyCode
= 0 ;
                pEvtObj.returnValue
= false ;
            }
            
else   if (pEvtObj.keyCode == 116 )     // KEY F5
            {
                pEvtObj.keyCode
= 0 ;
                pEvtObj.returnValue
= false ;
            }
            
else   if (pEvtObj.ctrlKey  &&  pEvtObj.keyCode == 70 )     // Key ctrl+F
            {
                pEvtObj.keyCode
= 0 ;
                pEvtObj.returnValue
= false ;
            }
            
else   if (pEvtObj.ctrlKey  &&  pEvtObj.keyCode == 78 )     // Key ctrl+N
            {
                pEvtObj.keyCode
= 0 ;
                pEvtObj.returnValue
= false ;
            }
            
else   if (pEvtObj.ctrlKey  &&  pEvtObj.keyCode == 80 )     // Key ctrl+P
            {
                pEvtObj.keyCode
= 0 ;
                pEvtObj.returnValue
= false ;
            }
        }

你可能感兴趣的:(WebBrowser)