1 使用帮助类访问iframe
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 string windowTitle = "Message from webpage";
private static string WINDOW_SAFE_TITLE = "安全警报";
private static string WINDOW_SAFE_TITLE2 = "Security Alert";
private static string BTN_YES_TITLE = "是(&Y)";
private static string BTN_YES_TITLE2 = "Yes";
private static string WINDOW_CLOSE_TITLE = "Web 浏览器";
private static string WINDOW_CLOSE_TITLE2 = "Windows Internet Explorer";
private static string WINDOW_JSERROR_TITLE = "脚本错误";
private static string WINDOW_JSERROR_TITLE2 = "Internet Explorer 脚本错误";
private static string WINDOW_JSERROR_TITLE3 = "Windows Internet Explorer Script Error";
private static string BTN_NO_TITLE = "否(&N)";
private static string BTN_NO_TITLE2 = "No";
private const uint BM_CLICK = 0xF5; //鼠标点击的消息,对于各种消息的数值,大家还是得去API手册
private const uint WM_CLOSE = 0x0010; //鼠标点击的消息,对于各种消息的数值,大家还是得去API手册
static System.Threading.Timer findTimer = null;
static AutoResetEvent autoEvent = null;
private static string windowTitle = string.Empty;
private static string btnTitle = string.Empty;
/// <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 StartCloseWindow(string wTitle, string bTitle)
{
windowTitle = wTitle;
btnTitle = bTitle;
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()
{
if (autoEvent != null)
{
autoEvent.WaitOne(10, false); //设置10毫秒,是让程序有一个等待,如果设为0,会一直弹窗口
}
if (findTimer != null)
{
findTimer.Dispose();
}
}
private static void CloseWindow(object state)
{
// 处理动态参数窗口部分
if (windowTitle != null && windowTitle.Length > 0 && btnTitle != null && btnTitle.Length > 0)
{
IntPtr hwnd = FindWindow(null, windowTitle); //查找窗口的句柄
if (hwnd != IntPtr.Zero)
{
IntPtr hwndSure = FindWindowEx(hwnd, 0, "Button", btnTitle); //获取确定按钮的句柄
if (hwndSure != IntPtr.Zero)
{
SendMessage(hwndSure, BM_CLICK, (IntPtr)0, 0); //发送单击消息
}
}
}
// 处理安全证书提示框,默认按是
IntPtr hwnd1 = FindWindow(null, WINDOW_SAFE_TITLE); //查找窗口的句柄
if (hwnd1 != IntPtr.Zero)
{
IntPtr hwndSure = FindWindowEx(hwnd1, 0, "Button", BTN_YES_TITLE); //获取确定按钮的句柄
if (hwndSure != IntPtr.Zero)
{
SendMessage(hwndSure, BM_CLICK, (IntPtr)0, 0); //发送单击消息
}
else
{
hwndSure = FindWindowEx(hwnd1, 0, "Button", BTN_YES_TITLE2); //获取确定按钮的句柄
if (hwndSure != IntPtr.Zero)
{
SendMessage(hwndSure, BM_CLICK, (IntPtr)0, 0); //发送单击消息
}
}
}
else
{
hwnd1 = FindWindow(null, WINDOW_SAFE_TITLE2); //查找窗口的句柄
if (hwnd1 != IntPtr.Zero)
{
IntPtr hwndSure = FindWindowEx(hwnd1, 0, "Button", BTN_YES_TITLE); //获取确定按钮的句柄
if (hwndSure != IntPtr.Zero)
{
SendMessage(hwndSure, BM_CLICK, (IntPtr)0, 0); //发送单击消息
}
else
{
hwndSure = FindWindowEx(hwnd1, 0, "Button", BTN_YES_TITLE2); //获取确定按钮的句柄
if (hwndSure != IntPtr.Zero)
{
SendMessage(hwndSure, BM_CLICK, (IntPtr)0, 0); //发送单击消息
}
}
}
}
// 处理IE中js调用window.close()弹出的关闭提示框,默认按否
IntPtr hwnd2 = FindWindow(null, WINDOW_CLOSE_TITLE); //查找窗口的句柄
if (hwnd2 != IntPtr.Zero)
{
IntPtr hwndSure = FindWindowEx(hwnd2, 0, "Button", BTN_NO_TITLE); //获取否按钮的句柄
if (hwndSure != IntPtr.Zero)
{
SendMessage(hwndSure, BM_CLICK, (IntPtr)0, 0); //发送单击消息
}
else
{
hwndSure = FindWindowEx(hwnd2, 0, "Button", BTN_NO_TITLE2); //获取否按钮的句柄
if (hwndSure != IntPtr.Zero)
{
SendMessage(hwndSure, BM_CLICK, (IntPtr)0, 0); //发送单击消息
}
}
}
else
{
hwnd2 = FindWindow(null, WINDOW_CLOSE_TITLE2); //查找窗口的句柄
if (hwnd2 != IntPtr.Zero)
{
IntPtr hwndSure = FindWindowEx(hwnd2, 0, "Button", BTN_NO_TITLE); //获取否按钮的句柄
if (hwndSure != IntPtr.Zero)
{
SendMessage(hwndSure, BM_CLICK, (IntPtr)0, 0); //发送单击消息
}
else
{
hwndSure = FindWindowEx(hwnd2, 0, "Button", BTN_NO_TITLE2); //获取否按钮的句柄
if (hwndSure != IntPtr.Zero)
{
SendMessage(hwndSure, BM_CLICK, (IntPtr)0, 0); //发送单击消息
}
}
}
}
// 处理IE中js调用出错提示框,默认按是
IntPtr hwnd3 = FindWindow(null, WINDOW_JSERROR_TITLE); //查找窗口的句柄
if (hwnd3 != IntPtr.Zero)
{
SendMessage(hwnd3, WM_CLOSE, (IntPtr)0, 0); //发送单击消息
}
else
{
hwnd3 = FindWindow(null, WINDOW_JSERROR_TITLE2); //查找窗口的句柄
if (hwnd3 != IntPtr.Zero)
{
SendMessage(hwnd3, WM_CLOSE, (IntPtr)0, 0); //发送单击消息
}
else
{
hwnd3 = FindWindow(null, WINDOW_JSERROR_TITLE3); //查找窗口的句柄
if (hwnd3 != IntPtr.Zero)
{
SendMessage(hwnd3, WM_CLOSE, (IntPtr)0, 0); //发送单击消息
}
}
}
}
}
2 代码中调用
IHTMLDocument3 frameWin = CorssDomainHelper.GetDocumentFromWindow(doc.frames.item(3)
as IHTMLWindow2);
IHTMLDocument2 fdoc = frameWin.createDocumentFragment();
IHTMLWindow2 parentWindow = fdoc.parentWindow;
parentWindow.execScript(@"
alert('进入');
}
经测试,只能查看,不能修改跨域iframe里面的doc元素