[英]Detect if there is any Confirm MessageBox opened
我使用輔助形式的WebBrowser對象打開url,並且在加載Web時,獲取並關閉該表單,但是我遇到了一些麻煩。
我有一個8秒的Timer(),並且如果8秒鍾后該窗體仍然存在,則該窗體被強制關閉。
WebBrowser的導航器性能較差,因此在某些網絡中出現“ alert()”提示我需要升級導航器。 當WebBrowser對象加載“ alert()”時,它像MessageBox一樣顯示在我的應用程序中,因此我需要先關閉該消息,然后關閉我的導航器表單。
為此,我這樣做:
主要形式:
int cont = 0;
void t_Tick(object sender, EventArgs e)
{
cont += 1;
if (cont == 8)
{
var formularios = Application.OpenForms;
for (int i = formularios.Count - 1; i >= 0; i--)
{
if (formularios[i] != this)
{
formularios[i].Dispose();
}
}
t.Enabled = false;
}
}
private void button1_Click(object sender, EventArgs e)
{
t.Enabled = true;
p = new FormNavigator();
p.Navigate("URL HERE");
p.ShowDialog();
}
它可以通過“ alert()”函數為我工作,但是某些網站使用“ confirm()”函數,並且使用“確定” /“取消”按鈕打開一個表單...而Application.OpenForms不會檢測到它。
您是否知道從.NET檢測那些“確認”對話框的方法?
問候。
更新資料
我的WebBrowser對象中具有以下屬性:
navigator.ScriptErrorsSuppressed = true;
navigator.AllowWebBrowserDrop = true;
navigator.IsWebBrowserContextMenu = false;
navigator.WebBrowserShortcutsEnabled = false;
我已經像這樣處理了NewWindow事件:
private void navivator_NewWindow(object sender, CancelEventArgs e)
{
e.Cancel = true;
}
AFAIK沒有內置函數可以執行此操作,但是您可以使用此功能禁止所有對話框消息
//隱藏其所有源自基礎ActiveX控件的對話框,而不僅僅是腳本錯誤
webBrowser1.ScriptErrorsSuppressed = true;
可以通過實現IDocHostShowUI來完成
int IDocHostShowUI.ShowMessage(IntPtr hwnd, string lpstrText, string lpstrCaption, int dwType, string lpstrHelpFile, int dwHelpContext, out int lpResult)
{
// return handled
// S_OK Host displayed its UI. MSHTML does not display its message box.
// S_FALSE Host did not display its UI. MSHTML displays its message box.
//
if (this.CloseOpenDialogs == true)
{
lpResult = 0;
return S_OK;
}
else
{
lpResult = 0;
return S_FALSE;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.