SHDocVw.WebBrowser的BeforeNavigate2事件无效

kotizheng 2013-11-12 02:36:09
使用webBrowser控件,想监控导航开始事件,大致象下面的样子

WebBrowser m_WebBrowser = new WebBrowser()
...
SHDocVw.WebBrowser wb = (SHDocVw.WebBrowser)m_WebBrowser.ActiveXInstance;
wb.BeforeNavigate2 += wb_BeforeNavigate2;
wb.NavigateComplete2 += wb_NavigateComplete2;
wb.DocumentComplete += wb_DocumentComplete;
wb.Silent = true;

void wb_BeforeNavigate2(object pDisp, ref object URL, ref object Flags, ref object TargetFrameName, ref object PostData, ref object Headers, ref bool Cancel)
{
if (pDisp == wb.Application)
{
m_isCompleted = false;
}
}
。。。
只有 BeforeNavigate2事件无法响应,NavigateComplete2 和DocumentComplete 可以工作的很好。

虽然可以使用m_WebBrowser本身的Naviging事件,时机虽然和BeforeNavigate2事件一样,但没有pDisp,
我想要这个东西来判断是否是主页面的导航,比如比较复杂的页面,含有frame,甚至有js弹出窗口,我只想监控
主页面的事件,而传递的pDisp可以帮我做这个判断,但现在问题就是这个事件无论如何不会被响应,百思不得其解啊!
...全文
211 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
df398286232 2016-05-13
  • 打赏
  • 举报
回复
http://stackoverflow.com/questions/141626/how-can-i-access-postdata-from-webbrowser-navigating-event-handler
    /// <summary>
    /// Fires before navigation occurs in the given object (on either a window or frameset element).
    /// </summary>
    /// <param name="pDisp">Object that evaluates to the top level or frame WebBrowser object corresponding to the navigation.</param>
    /// <param name="url">String expression that evaluates to the URL to which the browser is navigating.</param>
    /// <param name="Flags">Reserved. Set to zero.</param>
    /// <param name="TargetFrameName">String expression that evaluates to the name of the frame in which the resource will be displayed, or Null if no named frame is targeted for the resource.</param>
    /// <param name="PostData">Data to send to the server if the HTTP POST transaction is being used.</param>
    /// <param name="Headers">Value that specifies the additional HTTP headers to send to the server (HTTP URLs only). The headers can specify such things as the action required of the server, the type of data being passed to the server, or a status code.</param>
    /// <param name="Cancel">Boolean value that the container can set to True to cancel the navigation operation, or to False to allow it to proceed.</param>
    private delegate void BeforeNavigate2(object pDisp, ref dynamic url, ref dynamic Flags, ref dynamic TargetFrameName, ref dynamic PostData, ref dynamic Headers, ref bool Cancel);

    private void Form1_Load(object sender, EventArgs e)
    {
        dynamic d = webBrowser1.ActiveXInstance;

        d.BeforeNavigate2 += new BeforeNavigate2((object pDisp,
            ref dynamic url,
            ref dynamic Flags,
            ref dynamic TargetFrameName,
            ref dynamic PostData,
            ref dynamic Headers,
            ref bool Cancel) => {

            // Do some with PostData
        });
    }
wasl2002 2015-01-14
  • 打赏
  • 举报
回复
我是解决方法是引入AXSHDocVw 就是axWebBrowser1这个用的那个 然后 private void Form1_Load(object sender, EventArgs e) {//加上下边这两行 SHDocVw.WebBrowser wb = (SHDocVw.WebBrowser)webBrowser1.ActiveXInstance; wb.BeforeNavigate2 +=new DWebBrowserEvents2_BeforeNavigate2EventHandler(WebBrowser_BeforeNavigate2); } 不管用没用axWebBrowser1这个控件 webBrowser这个控件就可以正常用WebBrowser_BeforeNavigate2这个了
ted731900 2013-12-11
  • 打赏
  • 举报
回复
楼主怎么解决问题的呢?
kotizheng 2013-11-13
  • 打赏
  • 举报
回复
尼玛这扬都没人理啊,国内国外穷搜索,总算有点眉目了,算了自己结贴吧

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧