Domanda

Sono impegnato a scrivere un BHO (oggetto helper browser) in C# e devo allegare i gestori di eventi a tutti gli eventi OnClick su elementi di input. Non sto utilizzando il WebBrowser integrato fornito da Visual Studio, invece sto lanciando una nuova istanza di Internet Explorer installato sul PC client. Il problema arriva quando si utilizza diverse versioni di IE.

In ie7 e ie8 posso farlo in questo modo:

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

Funziona perfettamente, il fatto è che IE6 lancia un errore quando si lancia su HTMlinputerEmentclass, quindi sei costretto a lanciare a DisphtMlinputerEment:

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

Il problema è che non riesco a trovare un modo per allegare l'evento all'oggetto DisphtMlinputer. Qualche idea?

È stato utile?

Soluzione

Quindi si scopre che una volta che hai lanciato da un System_Comobject a un oggetto DisphtMlinputElement, è possibile interagire con l'interfaccia MSHTML. [Eventi]. Quindi il codice per aggiungere un gestore di eventi per IE6 sarà:

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

Puoi comunque interfacciarsi direttamente nel gestore degli eventi, ma volevo escludere alcuni tipi come i campi passwaord e di testo, quindi ho dovuto lanciare prima a disphtmlinputerelement

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top