转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> C语言系列 >> 正文
自动关闭webBrowser弹出的所有窗口         ★★★

自动关闭webBrowser弹出的所有窗口

作者:闵涛 文章来源:闵涛的学习笔记 点击数:1563 更新时间:2012/8/31 0:24:03
自动关闭webBrowser弹出的所有窗口

在C# winform程序里,webBrowser是一个很好用的,类似浏览器的控件。在模拟post,get请求时,有些时候HttpWebRequest和HttpWebResponse,会不能用,因为有些网站有“防”(加js等)这种请求,必须要浏览器来请求才可以,这样我们就需要把HttpWebRequest的cookie传到WebBrowser里面。


传cookie的话要用到InternetSetCookie这个API函数


  [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
  public static extern bool InternetSetCookie(string lpszUrlName, string lbszCookieName, string lpszCookieData);


引用命名空间:using System.Runtime.InteropServices;


调用示例:InternetSetCookie(“url", "cookiename", "cookievalue");


这样只要设置了cookie,在webBrowser里打开的窗口,会自动加上cookie的。


 


虽然webBrowser很好用,但有些时候,会弹出一些js窗口,还有脚本错误之类的东东。


1、对于脚本错误的话,webBrowser有个ScriptErrorsSuppressed属性,默认是false,不阻止,要想阻止这类错误,只要设成true,就可以了。


 


2、对于js窗口,我用了windows api 函数FindWindow等。


我封装了一个类,在程序中调用就可以实现关闭所有弹出的js窗口。


调用代码:CloseJsWindow.StartCloseWindow();


类代码:


CloseJsWindow

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading;

namespace SUP
{
public class CloseJsWindow
{
[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);

[DllImport(
"user32.dll", EntryPoint = "SetForegroundWindow", SetLastError = true)]
private static extern void SetForegroundWindow(IntPtr hwnd);

private static string windowTitle = "Message from webpage";
private const uint BM_CLICK = 0xF5; //鼠标点击的消息,对于各种消息的数值,大家还是得去API手册

static System.Threading.Timer findTimer;
static AutoResetEvent autoEvent;


/// <summary>
/// 开始监视js弹出的窗口
/// </summary>
public static void StartCloseWindow()
{
autoEvent
= new AutoResetEvent(false); //初始状态设置为非终止
TimerCallback timerDelegate = new TimerCallback(CloseWindow);
findTimer
= new System.Threading.Timer(timerDelegate, autoEvent, 1000, 800); //每0.8秒钟查找一次
}
/// <summary>
/// 停止监视js弹出的窗口
/// </summary>
public static void StopCloseWindow()
{
autoEvent.WaitOne(
10, false); //设置10毫秒,是让程序有一个等待,如果设为0,会一直弹窗口
findTimer.Dispose();
}
private static void CloseWindow(object state)
{
IntPtr hwnd
= FindWindow(null, windowTitle); //查找窗口的句柄
if (hwnd != IntPtr.Zero)
{
IntPtr hwndSure
= FindWindowEx(hwnd, 0, "Button", "确定"); //获取确定按钮的句柄
SendMessage(hwndSure, BM_CLICK, (IntPtr)0, 0); //发送单击消息
}
}
}
}



要想停止的话,只要调用 CloseJsWindow.StopCloseWindow()就行了。


[C语言系列]怎么使用webbrowser获取页面中的button元素。并触…  [C语言系列]C#实现 WebBrowser中新窗口打开链接用默认或者指定…
[C语言系列]c#WebBrowser查找并选择文本  [C语言系列]利用Webbrowser修改里的html代码里全部的"link"替…
[C语言系列]C#清除IE缓存临时文件实现WebBrowser强制刷新  [C语言系列]一个参数解决应用程序中WebBrowser的缓存问题
[C语言系列]WebBrowser在同一个窗口打开网页,禁止在新窗口打…  [Web开发]c# tabcontrol webbrowser 新标签页打开超链接
[C语言系列]C# webBrowser强制在本窗口打开,禁止在新窗口打开  [C语言系列]C# ,webBrowser,登录,数据填充,模拟点击链接,运行…
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台