2014-03-26 109 views
0

本地对话框我有以下代码接受在web浏览器

void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) 
{ 
    if (current_state == "request_4") 
    { 
     webBrowser1.Invoke(new Action(() => webBrowser1.ScriptErrorsSuppressed = true)); 
     webBrowser1.Invoke(new Action(() => webBrowser1.Refresh(WebBrowserRefreshOption.Completely))); 
     InjectAlertBlocker(); 
    } 
} 
private void InjectAlertBlocker() 
{ 
    HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0]; 
    HtmlElement scriptEl = webBrowser1.Document.CreateElement("script"); 
    IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement; 
    string alertBlocker = "window.alert = function() { }"; 
    element.text = alertBlocker; 
    head.AppendChild(scriptEl); 
} 

我做的请求的刷新是POST请求的web浏览器等等实例问我所有的时间,如果我要“重试”的通过显示对话框窗口来请求或不要求。我怎么能接受它或根本不显示?

抑制错误或InjectAlertBlocker()没有帮助。

我没有真正了解http://www.codeproject.com/Articles/31163/Suppressing-Hosted-WebBrowser-Control-Dialogs这种方法。

我很感激,如果有人可以解释如何使用该解决方案。

回答

0

该代码做的伎俩,但我仍然不喜欢它,因为它需要从我正在使用的当前窗口焦点。

[DllImport("user32.dll", EntryPoint = "FindWindow")] 
private static extern IntPtr FindWindow(string lp1, string lp2); 
[DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)] 
[return: MarshalAs(UnmanagedType.Bool)] 
private static extern bool SetForegroundWindow(IntPtr hWnd); 
[DllImport("user32.dll", SetLastError = true)] 
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle); 
System.Timers.Timer aTimer = new System.Timers.Timer(); 
private void request_refresh() 
{ 
    WebBrowserReadyState state1 = WebBrowserReadyState.Complete; 
    webBrowser1.Invoke(new Action(() => webBrowser1.ScriptErrorsSuppressed = true)); 
    webBrowser1.Invoke(new Action(() => webBrowser1.Refresh(WebBrowserRefreshOption.Completely))); 
    while (state1 != WebBrowserReadyState.Complete) 
    { 
     Application.DoEvents(); 
    } 
    aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent); 
    aTimer.Interval = 10; 
    aTimer.Enabled = true; 
} 
private void OnTimedEvent(object source, ElapsedEventArgs exc) 
{ 
    try 
    { 
     var handle = IntPtr.Zero; 
     handle = FindWindowEx(IntPtr.Zero, handle, "#32770", "Web-browser"); 
     if (!handle.Equals(IntPtr.Zero)) 
     { 
      if (SetForegroundWindow(handle)) 
      { 
       SendKeys.SendWait("{ENTER}"); 
       aTimer.Stop(); 
      } 
     } 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.ToString()); 
    } 
}