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オブジェクトにアタッチする方法が見つからないようです。何か案は?