Pergunta

Estou ocupado escrevendo um BHO (objeto Helper Helper) em C# e preciso anexar manipuladores de eventos a todos os eventos ONCLICK em elementos de entrada. Não estou usando o webbrowser embutido fornecido pelo Visual Studio, em vez disso, estou lançando uma nova instância do Internet Explorer instalado no PC clientes. O problema entra ao usar versões diferentes do IE.

No IE7 e IE8, posso fazer assim:

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);
      }
    }
  }
}

Isso funciona perfeitamente, o problema é que o IE6 lança um erro ao lançar para o htmlinputElementClass, para que você seja forçado a ser lançado para o 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
      }
    }
  }
}

O problema é que não consigo encontrar uma maneira de anexar o evento ao objeto DisphtmlinputElement. Alguma ideia?

Foi útil?

Solução

Portanto, depois que você for lançado de um sistema System_ComObject para um objeto DisphtMlinputElement, você pode interagir com a interface mshtml. [Events]. Portanto, o código para adicionar um manipulador de eventos para o IE6 será:

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);
      }
    }
  }
 }

No entanto, você pode interface diretamente no manipulador de eventos, mas eu queria excluir alguns tipos como Passwaord e Text Fields; portanto, eu tive que lançar para o DisphtmlinputElement primeiro

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top