4

C#でBHO(Browser Helper Object)を作成するのに忙しく、入力要素のすべてのonclickイベントにイベントハンドラーをアタッチする必要があります。Visual Studioが提供する組み込みのWebブラウザーを使用していません。代わりに、クライアントPCにインストールされているInternetExplorerの新しいインスタンスを起動しています。異なるバージョンのIEを使用すると、問題が発生します。

IE7とIE8では、次のように実行できます。

public void attachEventHandler(HTMLDocument doc)
{
  IHTMLElementCollection els = doc.all;
  foreach (IHTMLElement el in els)
  {
    if(el.tagName == "INPUT")
    {
      HTMLInputElementClass inputElement = el as HTMLInputElementClass;
      if (inputElement.IHTMLInputElement_type != "text" && InputElement.IHTMLInputElement_type != "password")
      {
        inputElement.HTMLButtonElementEvents_Event_onclick += new HTMLButtonElementEvents_onclickEventHandler(buttonElement_HTMLButtonElementEvents_Event_onclick);
      }
    }
  }
}

これは完全に機能します。つまり、IE6はHTMLInputElementClassにキャストするときにエラーをスローするため、DispHTMLInputElementにキャストする必要があります。

public void attachEventHandler(HTMLDocument doc)
{
  IHTMLElementCollection els = doc.all;
  foreach (IHTMLElement el in els)
  {
    if(el.tagName == "INPUT")
    {
      DispHTMLInputElement inputElement = el as DispHTMLInputElement;
      if (inputElement.type != "text" && inputElement.type != "password")
      {
        //attach onclick event handler here
      }
    }
  }
}

問題は、イベントをDispHTMLInputElementオブジェクトにアタッチする方法が見つからないようです。何か案は?

4

1 に答える 1

5

したがって、System_ComObjectからDispHTMLInputElementオブジェクトにキャストすると、mshtml。[events]インターフェイスを操作できることがわかります。したがって、IE6のイベントハンドラーを追加するコードは次のようになります。

public void attachEventHandler(HTMLDocument doc)
{
  IHTMLElementCollection els = doc.all;
  foreach (IHTMLElement el in els)
  {
    if(el.tagName == "INPUT")
    {
      DispHTMLInputElement inputElement = el as DispHTMLInputElement;
      if (inputElement.type != "text" && inputElement.type != "password")
      {
        HTMLButtonElementEvents_Event htmlButtonEvent = inputElement as HTMLButtonElementEvents_Event;
        htmlButtonEvent.onclick += new HTMLButtonElementEvents_onclickEventHandler(buttonElement_HTMLButtonElementEvents_Event_onclick);
      }
    }
  }
 }

ただし、イベントハンドラーに直接インターフェイスすることはできますが、passwaordやtextフィールドなどの一部のタイプを除外したかったため、最初にDispHTMLInputElementにキャストする必要がありました。

于 2009-12-11T09:06:41.657 に答える