自动关闭webBrowser弹出的所有窗口
2010-08-18 11:04
393 查看
在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();
类代码:
代码
调用代码
MessageBoxAuto.Show("验证码识别组件加载失败!");
传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();
类代码:
代码
using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; using System.Windows.Forms; using System.Threading; namespace xmypkj { public class MessageBoxAuto { [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)] private static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [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", CharSet = CharSet.Auto)] public static extern int PostMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam); public const int WM_CLOSE = 0x10; private const uint BM_CLICK = 0xF5; //鼠标点击的消息,对于各种消息的数值,大家还是得去API手册 private static string title = "提示信息"; //查找窗口时,标题最好不要为空 static System.Threading.Timer findTimer; static AutoResetEvent autoEvent; public static void Show(string text) { StartCloseMessageBox(3000); //默认3秒关闭 MessageBox.Show(text, title); StopCloseMessageBox(); } public static void Show(string text, string caption) { StartCloseMessageBox(3000); title = caption; MessageBox.Show(text, caption); StopCloseMessageBox(); } public static void Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, int millis) { StartCloseMessageBox(millis); title = caption; MessageBox.Show(text, caption, buttons, icon); StopCloseMessageBox(); } /// <summary> /// 开始监视MessageBox /// </summary> private static void StartCloseMessageBox(int millis) { autoEvent = new AutoResetEvent(false); //初始状态设置为非终止 TimerCallback timerDelegate = new TimerCallback(CloseWindow); findTimer = new System.Threading.Timer(timerDelegate, autoEvent, 2000, millis); } /// <summary> /// 停止监视MessageBox /// </summary> private static void StopCloseMessageBox() { autoEvent.WaitOne(10, false); //设置10毫秒,是让程序有一个等待,如果设为0,会一直弹窗口 findTimer.Dispose(); } /// <summary> /// 发送按钮单击消息关闭窗口 /// </summary> /// <param name="state"></param> private static void CloseWindow(object state) { IntPtr hwnd = FindWindow(null, title); //查找窗口的句柄 if (hwnd != IntPtr.Zero) { PostMessage(hwnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero); } } } }
调用代码
MessageBoxAuto.Show("验证码识别组件加载失败!");
相关文章推荐
- 自动关闭webBrowser弹出的所有窗口
- Delphi_Webbrowser中自动点击Alert弹出窗口中的按钮
- MFC 关闭所有弹出模态对话框,并创建新的对话框(切换/跳转窗口)
- JavaScript实现自动弹出窗口并自动关闭窗口的方法
- 倒计时自动关闭弹出窗口
- mc_PopupBox 弹出可定时自动关闭的消息窗口
- WebBrowser控件应用:弹出新窗体和关闭窗口 (转)
- 为弹出窗口设置n秒后自动关闭
- 如何在关闭窗口的时候,不让浏览器自动弹出确认关闭对话框
- 关于弹出消息窗口的自动关闭
- WPF弹出一个窗口,并自动关闭
- 求高手指教,我有一个会员登陆文件页面,我想在会员点登陆弹出新窗口,然后自动关闭登陆窗口,就象腾讯2007版的官方会员网站http://my.qq.com上面的那个登陆窗口一样
- 自动关闭弹出新闻广告窗口的一个实际例子,在Windows7+Delphi7中占用内存约为932K左右
- WebBrowser控件应用:弹出新窗体和关闭窗口
- 自动弹出窗口,并定时关闭
- Duilib 实现右下角弹出像QQ新闻窗口,3秒后自动关闭(一)
- 求高手指教,我有一个会员登陆文件页面,我想在会员点登陆弹出新窗口,然后自动关闭登陆窗口
- 用javascript实现始终保持打开同一个子窗口以及关闭父窗口同时自动关闭所有子窗口
- jQuery弹出Div窗口居中显示、滚动跟随、关闭按钮以及几秒后自动消失
- javaFX 可控制自动关闭时间的弹出提示窗口