http://stackoverflow.com/questions/7608550/implement-idispatchinvoke-to-be-called-by-a-webbrowser-control我最近在做一个小浏览器,要实现禁止下载图片和视频的功能。 按照stackoverflow (见上链接)的做法,用反射调用Idispatch,可以达到目的。 但问题是,浏览器的刷新,前进和后退方法:Refresh(),goBack(),goForward()都不起作用了。有高手知道为什么吗?请不吝赐教

解决方案 »

  1.   

    public enum WebBrowserDownloadControlFlags: uint {     DLIMAGES                          = 0x00000010,     VIDEOS                            = 0x00000020,     BGSOUNDS                          = 0x00000040,     NO_SCRIPTS                        = 0x00000080,     NO_JAVA                           = 0x00000100,     NO_RUNACTIVEXCTLS                 = 0x00000200,     NO_DLACTIVEXCTLS                  = 0x00000400,     DOWNLOADONLY                      = 0x00000800,     NO_FRAMEDOWNLOAD                  = 0x00001000,     RESYNCHRONIZE                     = 0x00002000,     PRAGMA_NO_CACHE                   = 0x00004000,     NO_BEHAVIORS                      = 0x00008000,     NO_METACHARSET                    = 0x00010000,     URL_ENCODING_DISABLE_UTF8         = 0x00020000,     URL_ENCODING_ENABLE_UTF8          = 0x00040000,     NOFRAMES                          = 0x00080000,     FORCEOFFLINE                      = 0x10000000,     NO_CLIENTPULL                     = 0x20000000,     SILENT                            = 0x40000000,     OFFLINEIFNOTCONNECTED             = 0x80000000,     OFFLINE                           = OFFLINEIFNOTCONNECTED, }根据这个标志位表设置你需要屏蔽的功能。
      

  2.   

      public class WebBrowser1 : WebBrowser
      {
          private const int DISPID_AMBIENT_DLCONTROL = -5512;
          private int _controlFlags;      protected override WebBrowserSiteBase CreateWebBrowserSiteBase()
          {
              return new WebBrowserSite1(this);
          }      [DispId(DISPID_AMBIENT_DLCONTROL), Browsable(false)]
          public int ControlFlags
          {
              get
              {
                  return _controlFlags;
              }
              set
              {
                  if (_controlFlags == value)
                      return;              _controlFlags = value;              IOleControl ctl = (IOleControl)ActiveXInstance;
                  ctl.OnAmbientPropertyChange(DISPID_AMBIENT_DLCONTROL);
              }
          }      // we want our site class, not the default one
          protected class WebBrowserSite1 : WebBrowserSite, IReflect
          {
              private Dictionary<int, PropertyInfo> _dispidCache;
              private WebBrowser1 _host;          public WebBrowserSite1(WebBrowser1 host)
                  : base(host)
              {
                  _host = host;
              }          object IReflect.InvokeMember(string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters)
              {
                  object ret = null;
                  // Check direct IDispatch call using a dispid (see http://msdn.microsoft.com/en-us/library/de3dhzwy.aspx)
                  const string dispidToken = "[DISPID=";
                  if (name.StartsWith(dispidToken))
                  {
                      int dispid = int.Parse(name.Substring(dispidToken.Length, name.Length - dispidToken.Length - 1));
                      if (_dispidCache == null)
                      {
                          // WebBrowser has many properties, so we build a dispid cache on it
                          _dispidCache = new Dictionary<int, PropertyInfo>();
                          foreach (PropertyInfo pi in _host.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
                          {
                              if ((!pi.CanRead) || (pi.GetIndexParameters().Length > 0))
                                  continue;                          object[] atts = pi.GetCustomAttributes(typeof(DispIdAttribute), true);
                              if ((atts != null) && (atts.Length > 0))
                              {
                                  DispIdAttribute da = (DispIdAttribute)atts[0];
                                  _dispidCache[da.Value] = pi;
                              }
                          }
                      }                  PropertyInfo property;
                      if (_dispidCache.TryGetValue(dispid, out property))
                      {
                          ret = property.GetValue(_host, null);
                      }
                  }
                  return ret;
              }          FieldInfo[] IReflect.GetFields(BindingFlags bindingAttr)
              {
                  return this.GetType().GetFields(bindingAttr);
              }          MethodInfo[] IReflect.GetMethods(BindingFlags bindingAttr)
              {
                  return this.GetType().GetMethods(bindingAttr);
              }          PropertyInfo[] IReflect.GetProperties(BindingFlags bindingAttr)
              {
                  return this.GetType().GetProperties(bindingAttr);
              }          FieldInfo IReflect.GetField(string name, BindingFlags bindingAttr)
              {
                  throw new NotImplementedException();
              }          MemberInfo[] IReflect.GetMember(string name, BindingFlags bindingAttr)
              {
                  throw new NotImplementedException();
              }          MemberInfo[] IReflect.GetMembers(BindingFlags bindingAttr)
              {
                  throw new NotImplementedException();
              }          MethodInfo IReflect.GetMethod(string name, BindingFlags bindingAttr)
              {
                  throw new NotImplementedException();
              }          MethodInfo IReflect.GetMethod(string name, BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers)
              {
                  throw new NotImplementedException();
              }          PropertyInfo IReflect.GetProperty(string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
              {
                  throw new NotImplementedException();
              }          PropertyInfo IReflect.GetProperty(string name, BindingFlags bindingAttr)
              {
                  throw new NotImplementedException();
              }          Type IReflect.UnderlyingSystemType
              {
                  get { throw new NotImplementedException(); }
              }      }    [Flags]
          public enum AbBrowserDownloadControlFlags : int
          {
              /// <summary>
              /// No flags are set.
              /// </summary>
              None = 0,
              /// <summary>
              /// The browser will operate in offline mode. Equivalent to DLCTL_FORCEOFFLINE.
              /// </summary>
              AlwaysOffline = unchecked((int)0x10000000),
              /// <summary>
              /// The browser will play background sounds. Equivalent to DLCTL_BGSOUNDS.
              /// </summary>
              BackgroundSounds = unchecked((int)0x00000040),
              /// <summary>
              /// Specifies that the browser will not run Active-X controls. Use this setting
              /// to disable Flash movies. Equivalent to DLCTL_NO_RUNACTIVEXCTLS.
              /// </summary>
              DontRunActiveX = unchecked((int)0x00000200),
              /// <summary>
              ///  Specifies that the browser should fetch the content from the server. If the server's
              ///  content is the same as the cache, the cache is used.Equivalent to DLCTL_RESYNCHRONIZE.
              /// </summary>
              IgnoreCache = unchecked((int)0x00002000),
              /// <summary>
              ///  The browser will force the request from the server, and ignore the proxy, even if the
              ///  proxy indicates the content is up to date.Equivalent to DLCTL_PRAGMA_NO_CACHE.
              /// </summary>
              IgnoreProxy = unchecked((int)0x00004000),
              /// <summary>
              ///  Specifies that the browser should download and display images. This is set by default.
              ///  Equivalent to DLCTL_DLIMAGES.
              /// </summary>
              Images = unchecked((int)0x00000010),
              /// <summary>
              ///  Disables downloading and installing of Active-X controls.Equivalent to DLCTL_NO_DLACTIVEXCTLS.
              /// </summary>
              NoActiveXDownload = unchecked((int)0x00000400),
              /// <summary>
              ///  Disables web behaviours.Equivalent to DLCTL_NO_BEHAVIORS.
              /// </summary>
              NoBehaviours = unchecked((int)0x00008000),
              /// <summary>
              ///  The browser suppresses any HTML charset specified.Equivalent to DLCTL_NO_METACHARSET.
              /// </summary>
              NoCharSets = unchecked((int)0x00010000),
              /// <summary>
              ///  Indicates the browser will ignore client pulls.Equivalent to DLCTL_NO_CLIENTPULL.
              /// </summary>
              NoClientPull = unchecked(0x20000000),
              /// <summary>
              ///  The browser will not download or display Java applets.Equivalent to DLCTL_NO_JAVA.
              /// </summary>
              NoJava = unchecked((int)0x00000100),
              /// <summary>
              ///  The browser will download framesets and parse them, but will not download the frames
              ///  contained inside those framesets.Equivalent to DLCTL_NO_FRAMEDOWNLOAD.
              /// </summary>
              NoFrameDownload = unchecked((int)0x00080000),
              /// <summary>
              ///  The browser will not execute any scripts.Equivalent to DLCTL_NO_SCRIPTS.
              /// </summary>
              NoScripts = unchecked((int)0x00000080),
              /// <summary>
              ///  If the browser cannot detect any internet connection, this causes it to default to
              ///  offline mode.Equivalent to DLCTL_OFFLINEIFNOTCONNECTED.
              /// </summary>
              OfflineIfNotConnected =unchecked((int)0x80000000),
              /// <summary>
              ///  Specifies that UTF8 should be used.Equivalent to DLCTL_URL_ENCODING_ENABLE_UTF8.
              /// </summary>
              UTF8 = unchecked((int)0x00040000),
              /// <summary>
              ///  The browser will download and display video media.Equivalent to DLCTL_VIDEOS.
              /// </summary>
              Videos = unchecked((int)0x00000020)
          }
    }