日期:2014-05-20  浏览次数:21066 次

webBrowser中的问题,BHO、DispId的问题,做过的进来
这2段是从某个国外牛人写的代码中抄袭出来的,可以正常运行

//   COM   Event   Handler   for   HTML   Element   Events
[DispId(0)]
public   void   DefaultMethod()
{
//   obtain   the   event   object   and   ensure   a   context   menu   has   been   applied   to   the   document
HtmlEventObject   eventObject   =   document.parentWindow.@event;
string   eventType   =   eventObject.type;

//   Call   the   custom   Web   Browser   HTML   event  
ContextMenuShow(this,   eventObject);

}   //DefaultMethod


private   void   BrowserDocumentComplete(object   sender,   AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent   e)
{
//   get   access   to   the   HTMLDocument
document   =   (HtmlDocument)this.axWebBrowser1.Document;
//   add   style   sheet   link
string   href   =   Path.GetTempPath()   +   "/StyleMain.css ";
LinkStyleSheet(href);

body   =   (HtmlBody)document.body;

//   COM   Interop   Start
//   once   browsing   has   completed   there   is   the   need   to   setup   some   options
//   need   to   ensure   URLs   are   not   modified   when   html   is   pasted
IOleCommandTarget   target   =   null;
int   hResult   =   HRESULT.S_OK;
//   try   to   obtain   the   command   target   for   the   web   browser   document
try
{
//   cast   the   document   to   a   command   target
target   =   (IOleCommandTarget)document;
//   set   the   appropriate   no   url   fixups   on   paste
hResult   =   target.Exec(ref   CommandGroup.CGID_MSHTML,   (int)CommandId.IDM_NOFIXUPURLSONPASTE,   (int)CommandOption.OLECMDEXECOPT_DONTPROMPTUSER,   ref   EMPTY_PARAMETER,   ref   EMPTY_PARAMETER);
}
//   catch   any   exception   and   map   back   to   the   HRESULT
catch   (Exception   ex)
{
hResult   =   Marshal.GetHRForException(ex);
}
//   test   the   HRESULT   for   a   valid   operation
if   (hResult   ==   HRESULT.S_OK)
{
//   urls   will   not   automatically   be   rebased
rebaseUrlsNeeded   =   false;
}
else
{
//throw   new   HtmlEditorException(string.Format( "Error   executing   NOFIXUPURLSONPASTE:   Result   {0} ",   hResult));
rebaseUrlsNeeded   =   true;
}
//   COM   Interop   End

//   at   this   point   the   document   and   body   has   been   loaded
//   so   define   the   event   handler   as   the   same   class
//document.oncontextmenu   =   this;
((mshtml.DispHTMLDocument)document).oncontextmenu   =   this;
//   这句话是我加的,他只能相应DisoId(0)的方法
((mshtml.DispHTMLDocument)document).ondblclick   =   this;
//   complete