【发布时间】:2011-12-23 20:43:38
【问题描述】:
我已按照以下教程操作:http://www.15seconds.com/issue/040331.htm 制作 BHO,但我似乎不适合我,我有示例中的 Observer 代码,她在其中创建了实际的 BHO,但是当我尝试输入我的 SetSite 它停止了,我怀疑我遇到了异常。
这是我的示例代码,只是将其全部剥离,所以我得到了一个消息框。
[ClassInterfaceAttribute(ClassInterfaceType.None)]
[GuidAttribute("0CD00297-9A19-4698-AEF1-682FBE9FE88D")]
[ProgIdAttribute("Observer.BrowserMonitor")]
public class BrowserMonitor: IObserver, IObjectWithSite
{
// HRESULT values used
const int E_FAIL = unchecked((int)0x80004005);
const int E_NOINTERFACE = unchecked((int)0x80004002);
public BrowserMonitor()
{
}
protected SHDocVw.IWebBrowser2 m_pIWebBrowser2; // the browser class object
public void SetSite(object pUnkSite)
{
System.Windows.Forms.MessageBox.Show(pUnkSite.ToString());
if (pUnkSite != null)
{
m_pIWebBrowser2 = pUnkSite as SHDocVw.IWebBrowser2;
}
}
public void GetSite(ref System.Guid riid, out object ppvSite)
{
System.Windows.Forms.MessageBox.Show("GetSite");
ppvSite = null;
}
protected void DocumentComplete(object pDisp, ref object URL)
{
System.Windows.Forms.MessageBox.Show("DocumentComplete");
}
protected bool ServiceEnabled()
{
return true;
}
protected void Release()
{
System.Windows.Forms.MessageBox.Show("Release");
}
protected void BeforeNavigate2(object pDisp, ref object url, ref object Flags, ref object TargetFrameName,
ref object PostData, ref object Headers, ref bool Cancel)
{
System.Windows.Forms.MessageBox.Show("BeforeNavigate2");
}
protected void OnQuit()
{
try
{
System.Windows.Forms.MessageBox.Show("OnQuit");
}
catch{}
}
protected void NavigateComplete2(object pDisp, ref object URL)
{
System.Windows.Forms.MessageBox.Show("NavigationComplete2");
}
我的问题在于我的 SetSite 方法,如何将类型转换为任何类型的浏览器?如果我现在运行这个示例,我只会得到“GetSite”消息框,如果我删除 SetSite 中的 if 语句,我也会得到 SetSite。有谁知道怎么做?
【问题讨论】:
-
如果我删除“if”,只留下消息框,它会显示消息框。如果我离开“if”结构,我什至不会弹出消息框