请教webbrowser如何捕捉弹出窗口并操作

行业技术 2013-05-03 10:32:35
各位大侠,本人初学c#,在使用webbrower控件处理网页时,有时候网页会弹出对话框,如让你确定之类的,但我不知道应该如何在等距离中进行处理,比如我要点击弹出对话框的“确定”按钮。请问要如何做,谢谢
...全文
715 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuan0709 2015-12-23
  • 打赏
  • 举报
回复
引用 3 楼 sp1234 的回复:
你可以使用一个Timer,每隔100毫秒就搜索一下。例如假设要关闭那些烦人的异常弹出窗口,可以写

private void timer1_Tick(object sender, EventArgs e)
{
    CloseWin("来自网页的消息", "确定");
    CloseWin("Message from webpage", "确定");
}

[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("user32.dll", EntryPoint = "FindWindowEx", SetLastError = true)]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, uint hwndChildAfter, string lpszClass, string lpszWindow);

[DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true, CharSet = CharSet.Auto)]
private static extern int SendMessage(IntPtr hwnd, uint wMsg, IntPtr wParam, int lParam);

private void CloseWin(string winTitle, string buttonTitle)
{
    IntPtr hwnd = FindWindow(null, winTitle);
    if (hwnd != IntPtr.Zero)
    {
        IntPtr hwndText = FindWindowEx(hwnd, 0, null, "请设置浏览器的安全级别.启用activeX脚本!");
        if (hwndText != IntPtr.Zero)
        {
            IntPtr hwndSure = FindWindowEx(hwnd, 0, "Button", buttonTitle);
            if (hwnd != IntPtr.Zero)
                SendMessage(hwndSure, 0xF5, (IntPtr)0, 0);  //按她
        }
    }
}
你这个是关闭弹出页面还是JS的对话框?
行业技术 2013-05-17
  • 打赏
  • 举报
回复
搞定,散分拉
thoughter 2013-05-05
  • 打赏
  • 举报
回复
q107770540 2013-05-05
  • 打赏
  • 举报
回复
楼上P哥给的不错,可以试试
  • 打赏
  • 举报
回复
你可以使用一个Timer,每隔100毫秒就搜索一下。例如假设要关闭那些烦人的异常弹出窗口,可以写

private void timer1_Tick(object sender, EventArgs e)
{
    CloseWin("来自网页的消息", "确定");
    CloseWin("Message from webpage", "确定");
}

[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("user32.dll", EntryPoint = "FindWindowEx", SetLastError = true)]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, uint hwndChildAfter, string lpszClass, string lpszWindow);

[DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true, CharSet = CharSet.Auto)]
private static extern int SendMessage(IntPtr hwnd, uint wMsg, IntPtr wParam, int lParam);

private void CloseWin(string winTitle, string buttonTitle)
{
    IntPtr hwnd = FindWindow(null, winTitle);
    if (hwnd != IntPtr.Zero)
    {
        IntPtr hwndText = FindWindowEx(hwnd, 0, null, "请设置浏览器的安全级别.启用activeX脚本!");
        if (hwndText != IntPtr.Zero)
        {
            IntPtr hwndSure = FindWindowEx(hwnd, 0, "Button", buttonTitle);
            if (hwnd != IntPtr.Zero)
                SendMessage(hwndSure, 0xF5, (IntPtr)0, 0);  //按她
        }
    }
}
行业技术 2013-05-05
  • 打赏
  • 举报
回复
谢,提供思路
bdmh 2013-05-03
  • 打赏
  • 举报
回复
findwindow找到句柄,sendmessage发送wm_close消息

110,545

社区成员

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

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

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