首先不建议用C#写BHO(开个新网页还要等CLR加载,性能太差)
真要实现的话,可以用IDispatchEx接口(.Net里封装为IExpando)把window.dialogArguments等对话框显示相关的属性都替换成你的变量。
var expando =(IExpando)document.parentWindow;
expando.RemoveMember(expando.GetMethod("showModalDialog",BindingFlags.Instance | BindingFlags.Public);
expando.AddMethod("showModalDialog",new ShowModalDialogDelegate(this.myshowModalDialog));
expando.RemoveProperty("dialogArguments");
expando.AddProperty("dialogArguments").SetValue(expando,m_htmlDialog.dialogArguments);
......
expando.RemoveProperty("returnValue");
expando.AddProperty("returnValue").SetValue(expando,m_htmlDialog.returnValue);
这里ShowModalDialogDelegate的签名要和IE的一致,myshowModalDialog负责显示一个有浏览器控件的对话框。
在myshowModalDialog显示的那个浏览器控件触发DWebBrowserEvents2::WindowClosing事件时获得对话框网页的window.returnValue的值,并且把这个值传递给用来替换原window.returnValue的那个变量(在上面的代码中是m_htmlDialog.returnValue)。这个返回值设完了myshowModalDialog才可以返回。
IE8以上在IE里要显示模态对话框的时候还要注意先调用AcquireModalDialogLockAndParent,显示完之后调用ReleaseModalDialogLockAndParent。
Visual C++ MVP